7.3 Style individual elements

Due to the simplicity of Markdown, you can apply some global styles to the Word document (see Section 7.1), but it is not straightforward to style individual elements, such as changing the color of a word, or centering a paragraph.

Continuing his effort on making it easier to work with Office documents in R, David Gohel started to develop the officedown package (https://github.com/davidgohel/officedown) in 2018, which aims to bring some officer (https://github.com/davidgohel/officer) features into R Markdown. As of this writing, this package has not been published on CRAN yet, and you need to install it from GitHub:


After the package is installed, you need to load it in your R Markdown document, e.g.,

```{r, setup, include=FALSE}

The officedown package allows you to style specific elements for the Word output. It has provided two types of syntax, and you can choose to use either one:

  1. You can call functions of this package in inline R expressions, e.g., `r chunk_styled_text("officedown is great", color = "red", bold = TRUE)`.

  2. Or you can write the text in special HTML comments, e.g., <!---CHUNK_TEXT{str: "officedown is great", color: "red", bold: true}--->.

The two ways are equivalent—the latter way essentially calls a corresponding function in officedown, e.g., when officedown detects the tag CHUNK_TEXT in the comment, it calls the function chunk_styled_text() with the parameters specified in the curly brackets in the comment. You may see the GitHub repository of officedown for the available tags and functions.

Other things you may do with officedown include:

  • Insert page breaks.

  • Assign style names to text.

  • Put content in a multi-column layout.

  • Change paragraph settings.

  • Insert a table of contents.

  • Embed another Word document in the current document.

  • Change the orientation of a section (landscape or portrait).