Chapter 7 Formatting properties

There are four main functions to know, the one to manage character or text styles, the one for paragraph styles, the one for border styles, the one for table cell styles.

This set of functions will allow a lot of things, the insertion of paragraphs with composite formatting (for example, “hello world” formatted as “hello world”), with centered content, with a border at the bottom, etc.

These functions are to be used in several functions from {officer} but also {flextable}, {mschart} and {officedown}.

7.1 Text formatting properties

Use fp_text() to create text formatting properties. It let you specify font name color, size, font name and many other properties:

  • color
  • font.size
  • bold
  • italic
  • underlined
  • font.family
  • cs.family
  • eastasia.family
  • hansi.family
  • vertical.align
  • shading.color

It can be used (this list is not exhaustive):

  • as the value of the prop argument of the ftext() function.
  • as the value of the pr_t argument of the flextable::style() function.
  • as the value of the props argument of the flextable::hyperlink_text() function.
properties1 <- fp_text(color = "#006699", bold = TRUE, font.size = 50)
properties2 <- fp_text(color = "#C32900", bold = TRUE, font.size = 50)
ftext1 <- ftext("hello ", properties1)
ftext2 <- ftext("World", properties2)
paragraph <- fpar(ftext1, ftext2)

read_docx() %>% 
  body_add_fpar(paragraph) %>% 
  print(target = "reports/example_ftext_1.docx")

  • to customize labels in charts created with package {mschart}

Another function is existing, fp_text_lite(). It’s a function that does only define formats for the parameters that have been specified by the user. As a result, all non specified values are inheriting from the default values. This is very handy to avoid setting values for each parameters of fp_text().

properties1 <- fp_text_lite(color = "#006699", bold = TRUE)
ftext1 <- ftext("hello ", properties1)
ftext2 <- ftext("World", properties1)
paragraph <- fpar(ftext1, ", how are you ", ftext2, "?")

read_docx() %>% 
  body_add_fpar(paragraph) %>% 
  print(target = "reports/example_ftext_2.docx")

7.2 Paragraph formatting properties

Use fp_par() to create paragraph formatting properties. It let you specify text alignement, padding, border settings:

  • text.align
  • padding
  • line_spacing
  • border
  • padding.bottom
  • padding.top
  • padding.left
  • padding.right
  • border.bottom
  • border.left
  • border.top
  • border.right
  • shading.color
  • keep_with_next

It can be used in different use cases:

  • as the value of the pr_p argument of the fpar() function.
  • as the value of the pr_p argument of the flextable::style() function.
paragraph_1 <- fpar(ftext1, ftext2, fp_p = fp_par(text.align = "center"))
paragraph_2 <- fpar(ftext1, ftext2, 
                    fp_p = fp_par(
                      padding = 5, 
                      line_spacing = 2,
                      text.align = "right"))
paragraph_3 <- fpar(ftext1, ftext2, fp_p = fp_par(text.align = "right"))

read_docx() %>% 
  body_add_fpar(paragraph_1) %>% 
  body_add_fpar(paragraph_2) %>% 
  body_add_fpar(paragraph_3) %>% 
  print(target = "reports/example_fp_par_1.docx")

With package {officedown} you also can add an object made with fp_par in a paragraph and the paragraph will be formatted with the defined properties:

---
output: officedown::rdocx_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.cap = TRUE)
library(officedown)
library(officer)
center_par <- fp_par(
  text.align = "center", 
  padding = 10, 
  border = fp_border(color = "pink", width = 5)
)
```

I am a mad pac-man.`r center_par`

7.3 Border formatting properties

Use fp_border() to create a border formatting properties. It let you specify color, line style and line color of the border:

  • color
  • style
  • width

It can be used in different use cases:

  • as the value of the border argument of the fp_par() function.
  • as the value of the border argument of the flextable::hline() function.
paragraph_3 <- fpar(ftext1, ftext2,
  fp_p = fp_par(
    text.align = "right",
    border.top = fp_border(
      width = 5,
      color = "#C32900"
    )
  )
)

read_docx() %>%
  body_add_fpar(paragraph_1) %>%
  body_add_fpar(paragraph_2) %>%
  body_add_fpar(paragraph_3) %>%
  print(target = "reports/example_fp_par_2.docx")

7.4 Cell formatting properties

Use fp_cell() to create a cell formatting properties. It let you specify background color, borders and several other properties:

  • color
  • style
  • width

It is only used in package {flextable}.