RStudio offers the opportunity to build MS Word documents from R Markdown files (.Rmd). However, since formatting options in Markdown are very limited, there is no ‘native’ Markdown code to insert page breaks in the final MS Word output file.

In this blog post I explain, how to define page breaks in the RMarkdown document that will be kept in the final MS Word document (.docx). My post is based on Richard Layton’s article Happy collaboration with Rmd to docx which explains how to create a MS Word .docx template in order to modify the document design of a MS Word file created from a .Rmd-file in RStudio.

The MS Word template

In the first step, we create a MS Word template called ‘mystyles.docx’ (How to). This file must be saved in the same directory as the R Markdown file. For the following modifications we have to open this file with MS Word or a similar application. Since I don’t have an English version of MS Word, I will be using WPS Writer for Linux in this blog post.

Modify style ‘Heading 5’

In the next step, we modify a predefined style. However, after modifying a predefined style, we cannot use it anymore in the originally intended way. Thus, we must choose a style hardly needed for any other purpose. In this blog post, we use the Heading 5 style.

To modify this style, we select the ‘Home‘ ribbon tab and click the Styles window launcher in the Styles group (lower right corner, highlighted with red circle).

pic01

We select ‘Heading 5’ in the Word document. In the Styles window, we scroll down until we find the style already assigned to the text we selected. In our case, the assigned style is ‘Heading 5’.

heading 5

The following modifications must be made in the Modify Style menu:

pic03

pic04

After these tweaks, the ‘Heading 5’ style will no longer format a heading of level 5. Instead it will insert a very small and white (and, thus, invisible) line followed by a page break.

The RMarkdown document

In the RMarkdown document, a few specifications must be made.

The YAML header

RMarkdown documents contain a metadata section called YAML header. In this header, we specify the output format (word_document) and the name of the MS Word template (mystyles.docx).

---
title: 'Title'
date: "`r format(Sys.time(), '%d. %B %Y')`"
output: 
    word_document:
      reference_docx: mystyles.docx
---

The Markdown code ##### being originally reserved to format header 5 will be used to insert page breaks in the final .docx document. Since we modified the font color to ‘white’ in the MS Word template, the specification after the Markdown code (Page Break) will not appear in the final document.

The following example shows how to flag a page break between two paragraphs in a .Rmd file.

Example: Markdown code to insert a page break

Text after page break. Text after page break. Text after page break. Text after page break. 
Text after page break. Text after page break. Text after page break. Text after page break. 
Text after page break. Text after page break. Text after page break. Text after page break. 
Text after page break. Text after page break.

##### Page Break

Text after page break. Text after page break. Text after page break. Text after page break. 
Text after page break. Text after page break. Text after page break. Text after page break. 
Text after page break. Text after page break. Text after page break. Text after page break. 
Text after page break. Text after page break.

Download

My MS Word template may be downloaded here.

I hope you find this post useful and If you have any question please post a comment below. You are welcome to visit my personal blog Scripts and Statistics for more R tutorials.