[英]Is there a way to bold part of a character string being passed to add_header_lines() when using the flextable package for R
我正在使用我喜欢的 flextable package 为 Word 文档创建几个表格。 但是,我在表格标题中加粗部分文本时遇到了一些麻烦。 例如,我希望标题为“表 1。我的表标题的 rest”。 而不是“表 1。我的表标题的 rest。”
我找到了这个文档,经过反复试验,我终于得到了我想要的结果。 但感觉可能有更直接的方法来获得它。 这是我目前的解决方案:
library(dplyr)
library(flextable)
mtcars_ft <- flextable(head(mtcars)) %>%
# Add a blank title line to top of table
add_header_lines("") %>%
# Use compose to bold "Table #."
compose(
i = 1, part = "header",
value = as_paragraph(
as_chunk("Table 1. ", props = fp_text(bold = TRUE)),
"Here is my example mtcars ft."
),
)
这是我的结果的屏幕截图:
下面是一个解决方案,可以做你想做的事,但也可以在 Word 中使用自动编号,这样如果更新,数字和引用也会更新。
library(officer)
library(flextable)
mtcars_ft <- flextable(head(mtcars)) %>% set_table_properties(layout = "autofit")
bold_face <- fp_text(bold = TRUE, font.size = 11)
fpar_ <- fpar(
run_autonum(seq_id = 'tab',
bkm = 'a_bkm', pre_label = "Table ",
prop = bold_face),
"Here is my example mtcars ft." )
read_docx() %>%
body_add_fpar(fpar_, style = "centered") %>%
body_add_flextable(mtcars_ft) %>%
print(target = "example.docx")
这基本上就是flextable::set_caption
(但无法根据需要格式化文本)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.