'Is there a way to bold part of a character string being passed to add_header_lines() when using the flextable package for R
I'm creating a couple of tables for a Word document using the flextable package, which I'm loving. However, I'm having a little trouble bolding part of the text in my table title. For example, I want the title to read, "Table 1. The rest of my table title." instead of "Table 1. The rest of my table title."
I found this documentation, and after some trial and error I finally got the result I wanted. But it feels like there may be a more straightforward way to get it. Here is my current solution:
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."
),
)
Here is a screenshot of my result:
Solution 1:[1]
Below a solution that enable to do what you want but also to use auto-numbering in Word so that if updated the numbers and references will be updated.
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")
This is basically what do flextable::set_caption
(but with no ability to format the text as you want)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |