Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a page break in word document generated by RStudio & markdown

I writing a Word document with R markdown in R Studio. I can get many things, but at the moment I am not figuring out how can I get a page break. I have found solutions but only for rendered latex / pdf document that it is not my case.

like image 562
Giorgio Spedicato Avatar asked Jul 10 '14 08:07

Giorgio Spedicato


People also ask

How do I export from R Studio to Word?

Basically just copy and paste your code into pretty R, and copy and paste the output (not the html) into the open document. Show activity on this post. After you copy from the Rstudio Console window and paste into a Word document, you need to highlight all the the just copied text and change the font into Courier New.

How do I paste an R script into Word?

How can I copy the output of the R console into a word document? Select what you want in the console and copy. Then in Word paste using Keep Text Only. (Right click to get the paste options.)

How do I insert a page break in markdown?

There are three ways to insert a thematic break in Markdown, using three or more asterisks * , hyphens - or underscores _ , possibly with whitespace in between them. They all result in the same HTML (or PDF) output, which can then be used to create page breaks.

How do you knit a Word document in R studio?

In RStudio, click the Knit Word button. A Word document should appear. Save this Word file under a new name (for example, word-styles-reference-01. docx) in the same directory as the R Markdown file.


2 Answers

Added: To insert a page break, please use \newpage for formats including LaTeX, HTML, Word, and ODT.

https://bookdown.org/yihui/rmarkdown-cookbook/pagebreaks.html

Paragraph before page break.  \newpage  First paragraph on a new page. 

Previously: There is a way by using a fifth-level header block (#####) and a docx template defined in YAML.

After creating headingfive.docx in Microsoft Word, you select Modify Style of the Heading 5, and then select Page break before in the Line and Page Breaks tab and save the headingfive.docx file.

Page break before

--- title: 'Making page break using fifth-level header block' output:    word_document:     reference_docx: headingfive.docx --- 

In your Rmd document, you define reference_docx in the YAML header, and now you can use the page-breaking #####.

Please see below.

https://www.r-bloggers.com/r-markdown-how-to-insert-page-breaks-in-a-ms-word-document/

like image 71
Sungpil Han Avatar answered Sep 20 '22 11:09

Sungpil Han


With the help of John MacFarlane and others on the pandoc google group, I put together a filter that does this. Please see: https://groups.google.com/forum/#!topic/pandoc-discuss/FzLrhk0vVbU In short, the filter needs to look for something to replace with the openxml for pagebreak. In this case \newpage is being replaced with <w:p><w:r><w:br w:type=\"page\"/></w:r></w:p> This allows for a single latex markup to be interpreted for both pdf and word output. Joel

like image 29
JAllen Avatar answered Sep 17 '22 11:09

JAllen