Chapter 8 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’.
8.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
propargument of theftext()function. - as the value of the
pr_targument of theflextable::style()function. - as the value of the
propsargument of theflextable::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 = "static/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 = "static/reports/example_ftext_2.docx")
8.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
- word_style
It can be used in different use cases:
- as the value of the
pr_pargument of thefpar()function. - as the value of the
pr_pargument of theflextable::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 = "static/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`

8.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
borderargument of thefp_par()function. - as the value of the
borderargument of theflextable::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 = "static/reports/example_fp_par_2.docx")