An online community for showcasing R & Python tutorials. It operates as a networking platform for data scientists to promote their talent and get hired. Our mission is to empower data scientists by bridging the gap between talent and opportunity.
Data Management

# R Markdown: How to insert page breaks in a MS Word document

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.

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).

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’.

• Set the font color to ‘white’ (rather than ‘Automatic’).
• Select the smallest font size (8 rather than 11).
• Select ‘Page break before’ in the ‘Line and Page Breaks’ tab.

• Set the line spacing to ‘Exactly’ and ‘1 pt’ in the ‘Indents and Spacing’ tab.

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.

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&period; %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.