<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/r-markdown-how-to-insert-page-breaks-in-a-ms-word-document/"></div></div><div><a data-url="https://www.r-bloggers.com/r-markdown-how-to-insert-page-breaks-in-a-ms-word-document/" data-counturl="https://www.r-bloggers.com/r-markdown-how-to-insert-page-breaks-in-a-ms-word-document/" data-count="horizontal" data-text="R Markdown: How to insert page breaks in a MS Word document" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://datascienceplus.com/r-markdown-how-to-insert-page-breaks-in-a-ms-word-document/"> DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>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.</p>
<p>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 <a target="_blank" rel="nofollow" href="http://rmarkdown.rstudio.com/articles_docx.html">Happy collaboration with Rmd to docx</a> 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.</p>
<h2>The MS Word template</h2>
<p>In the first step, we create a MS Word template called ‘mystyles.docx’ (<a target="_blank" rel="nofollow" href="http://rmarkdown.rstudio.com/articles_docx.html">How to</a>). 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 <a target="_blank" rel="nofollow" href="http://wps-community.org/downloads">WPS Writer</a> for Linux in this blog post.</p>
<h3>Modify style ‘Heading 5’</h3>
<p>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 <em>Heading 5</em> style.</p>
<p>To modify this style, we select the ‘<strong>Home</strong>‘ ribbon tab and click the Styles window launcher in the Styles group (lower right corner, highlighted with red circle).</p>
<p><a title="" data-rel="lightbox-0" target="_blank" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/08/pic01.png"><img sizes="(max-width: 900px) 100vw, 900px" alt="pic01" class="wp-image-4513" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/08/pic01.png?resize=456%2C140" data-recalc-dims="1"></a></p>
<p>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’. </p>
<p><a title="" data-rel="lightbox-1" target="_blank" href="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/08/pic02.png"><img sizes="(max-width: 448px) 100vw, 448px" alt="heading 5" class="wp-image-4501" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/08/pic02-448x490.png?resize=448%2C490" data-recalc-dims="1"></a> </p>
<p>The following modifications must be made in the <em>Modify Style</em> menu:</p>
<ul>
<li>Set the font color to ‘white’ (rather than ‘Automatic’).</li>
<li>Select the smallest font size (8 rather than 11).</li>
<li>Select ‘Page break before’ in the ‘Line and Page Breaks’ tab.</li>
</ul>
<p><a title="" data-rel="lightbox-2" target="_blank" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/08/pic03.png"><img sizes="(max-width: 490px) 100vw, 490px" alt="pic03" class="wp-image-4512" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/08/pic03-490x314.png?resize=456%2C292" data-recalc-dims="1"></a></p>
<ul>
<li>Set the line spacing to ‘Exactly’ and ‘1 pt’ in the ‘Indents and Spacing’ tab.</li>
</ul>
<p><a title="" data-rel="lightbox-3" target="_blank" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/08/pic04.png"><img sizes="(max-width: 367px) 100vw, 367px" alt="pic04" class="wp-image-4510" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/08/pic04-367x490.png?resize=367%2C490" data-recalc-dims="1"></a></p>
<p>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.</p>
<h2>The RMarkdown document</h2>
<p>In the RMarkdown document, a few specifications must be made.</p>
<h3>The YAML header</h3>
<p>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 (<em>mystyles.docx</em>).</p>
<pre>
---
title: 'Title'
date: &quot;`r format(Sys.time(), '%d&amp;period; %B %Y')`&quot;
output: 
    word_document:
      reference_docx: mystyles.docx
---
</pre>
<p>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 (<code>Page Break</code>) will not appear in the final document.</p>
<p>The following example shows how to flag a page break between two paragraphs in a .Rmd file.</p>
<p><strong>Example: Markdown code to insert a page break</strong></p>
<pre><em>Text before page break. Text before page break. Text before page break. Text before page break. Text before page break. Text before page break. Text before page break. Text before page break. Text before page break. Text before page break. Text before page break. Text before page break. Text before page break. Text before page break.</em>

##### Page Break

<em>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.</em></pre>
<h3>Download</h3>
<p>My MS Word template may be downloaded <a target="_blank" rel="nofollow" href="https://dl.dropboxusercontent.com/u/95397254/mystyles.docx">here</a>.</p>
<p>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 <a target="_blank" rel="nofollow" href="https://scriptsandstatistics.wordpress.com/">Scripts and Statistics</a> for more R tutorials.</p>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p>
<ol>
<li><a title="Permanent Link toWorking on Data-Warehouse (SQL) with R" target="_blank" rel="bookmark" href="http://datascienceplus.com/working-on-data-warehouse-sql-with-r/">Working on Data-Warehouse (SQL) with R</a></li>
<li><a title="Permanent Link toImplementing Apriori Algorithm in R" target="_blank" rel="bookmark" href="http://datascienceplus.com/implementing-apriori-algorithm-in-r/">Implementing Apriori Algorithm in R</a></li>
<li><a title="Permanent Link toHandling missing data with MICE package; a simple approach" target="_blank" rel="bookmark" href="http://datascienceplus.com/handling-missing-data-with-mice-package-a-simple-approach/">Handling missing data with MICE package; a simple approach</a></li>
<li><a title="Permanent Link toBest packages for data manipulation in R" target="_blank" rel="bookmark" href="http://datascienceplus.com/best-packages-for-data-manipulation-in-r/">Best packages for data manipulation in R</a></li>
<li><a title="Permanent Link toIdentify, describe, plot, and remove the outliers from the dataset" target="_blank" rel="bookmark" href="http://datascienceplus.com/identify-describe-plot-and-removing-the-outliers-from-the-dataset/">Identify, describe, plot, and remove the outliers from the dataset</a></li>
</ol></strong>
<div>
<a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=HTbpLDwVqRM:raHIlA8x0yA:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/datascienceplus?i=HTbpLDwVqRM:raHIlA8x0yA:D7DqB2pKExk" border="0"></a> <a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=HTbpLDwVqRM:raHIlA8x0yA:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=qj6IDK7rITs" border="0"></a> <a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=HTbpLDwVqRM:raHIlA8x0yA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=yIl2AUoC8zA" border="0"></a>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/r-markdown-how-to-insert-page-breaks-in-a-ms-word-document/"></div></div><div><a data-url="https://www.r-bloggers.com/r-markdown-how-to-insert-page-breaks-in-a-ms-word-document/" data-counturl="https://www.r-bloggers.com/r-markdown-how-to-insert-page-breaks-in-a-ms-word-document/" data-count="vertical" data-text="R Markdown: How to insert page breaks in a MS Word document" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://datascienceplus.com/r-markdown-how-to-insert-page-breaks-in-a-ms-word-document/"> DataScience+</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/7b-2GONyiBU"></a></div>