7.1 Custom Word templates
You can apply the styles defined in a Word template document to new Word documents generated from R Markdown. Such a template document is also called a “style reference document”. The key is that you have to create this template document from Pandoc first, and change the style definitions in it later. Then pass the path of this template to the
reference_docx option of
As we just mentioned, the document
template.docx has to be generated from Pandoc. You can create this template from an arbitrary R Markdown document with the
word_document output format (the actual content of this document does not matter). Then open the
.docx file, and edit the styles.
Figure 7.1 shows that you can open the “Styles” window from the “HOME” tab in Word. When you move the cursor to a specific element in the document, an item in the styles list will be highlighted. If you want to modify the styles of this type of elements, you can click the dropdown menu on the highlighted item, and you will see a dialog box like Figure 7.2.
After you finish modifying the styles, you can save the document (with a filename that will not be accidentally overwritten), and use it as the template for future Word documents. When Pandoc renders a new Word document with a reference document (template), it will read the styles in the template and apply them to the new document.
You may watch a short video at https://vimeo.com/110804387, or read the article at https://rmarkdown.rstudio.com/articles_docx.html for more detailed instructions on how to create a Word template with custom styles. You may also check out the Stack Overflow post at https://stackoverflow.com/q/37671868/559676 on how to change the style of tables (the method is the same, and the key is that you need to find the correct style name).