简体   繁体   English

如何在 RStudio & Markdown 生成的 Word 文档中添加分页符

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

I writing a Word document with R markdown in R Studio.我在 R Studio 中用 R markdown 编写了一个 Word 文档。 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.我找到了解决方案,但仅适用于渲染的乳胶/pdf 文档,这不是我的情况。

Added: To insert a page break, please use \\newpage for formats including LaTeX, HTML, Word, and ODT.补充:要插入分页\\newpage ,请使用\\newpage用于格式,包括 LaTeX、HTML、Word 和 ODT。

https://bookdown.org/yihui/rmarkdown-cookbook/pagebreaks.html 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.以前:有一种方法是使用 YAML 中定义的第五级标题块 ( ##### ) 和 docx 模板。

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.在Microsoft Word 中创建headingfive.docx后,您选择Modify Style Heading 5 Modify Style ,然后在headingfive.docx Line and Page Breaks选项卡中选择Line and Page Breaks Page break before并保存headingfive.docx文件。

分页之前

---
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 ##### .在您的Rmd文档中,您在 YAML 标头中定义了reference_docx ,现在您可以使用分页#####

Please see below.请参阅下文。

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

With the help of John MacFarlane and others on the pandoc google group, I put together a filter that does this.在 John MacFarlane 和 pandoc google group 其他人的帮助下,我组合了一个过滤器来执行此操作。 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.请参阅: https ://groups.google.com/forum/#!topic/pandoc-discuss/FzLrhk0vVbU 简而言之,过滤器需要寻找一些东西来替换 openxml 以进行分页。 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.在这种情况下, \\newpage被替换为<w:p><w:r><w:br w:type=\\"page\\"/></w:r></w:p>这允许要为 pdf 和 word 输出解释的单个乳胶标记。 Joel乔尔

What you are trying to do is force a "page break" or "new page" in a word document generated with Pandoc.您要做的是在用 Pandoc 生成的 Word 文档中强制“分页”或“新页面”。 I have found a way to do this in my environment but I'm not sure it will work in every environment.我找到了一种在我的环境中执行此操作的方法,但我不确定它是否适用于所有环境。

My environment: * R-studio / Pandoc / MS-WORD starting with an "*.Rmd" file and generating a DOCX file.我的环境: * R-studio / Pandoc / MS-WORD 以“*.Rmd”文件开头并生成 DOCX 文件。

In my RMD file the key idea is that i've created what acts like a TEMPLATE document (MyFormattingDocument.docx) and in that word document I tweak the STYLES for things like "Heading 1" and/or "Heading 2" and or "footnote" or whatever other predefined styles I want to tweak.在我的 RMD 文件中,关键思想是我创建了类似于模板文档 (MyFormattingDocument.docx) 的内容,在该 Word 文档中,我针对“标题 1”和/或“标题 2”和或“脚注”或任何其他我想要调整的预定义样式。

(SEE THIS: http://rmarkdown.rstudio.com/word_document_format.html#style-reference ) for explanation of style reference and how to set the header information in your RMD file to specify a reference document. (参见: http : //rmarkdown.rstudio.com/word_document_format.html#style-reference )了解样式参考以及如何在 RMD 文件中设置标题信息以指定参考文档。

SOOOO in my case... i tweak the "Heading 1" style in WORD to include a forced "Page Break Before" in the Paragraph formatting for "Heading 1". SOOOO 在我的情况下......我调整了 WORD 中的“标题 1”样式,以在“标题 1”的段落格式中包含强制的“分页前”。 Exactly how you force every "Heading 1" to always "Page Break" is different in different versions of Microsoft WORD but if you follow the WORD documentation and modify the "Heading 1" style THEN every "Heading 1" will always have a pagebreak before it.在不同版本的 Microsoft WORD 中,您强制每个“标题 1”始终为“分页符”的确切方式是不同的,但是如果您遵循 WORD 文档并修改“标题 1”样式,那么每个“标题 1”之前总会有一个分页符它。

THEN... you save this template file in the some directory you're working from with the RMD file... and it is USED AS a template.然后……您将此模板文件保存在您正在使用 RMD 文件的某个目录中……并将其用作模板。 THE CONTENTS of the file are ignored.... so don't worry... you can put sample text in this file and test that the formatting all works.... THE CONTENTS ARE IGNORED but the STYLES are USED in the new word document which will be built by the RMD file so.... then every "Heading 1" will have a break before it.文件的内容被忽略......所以不用担心......你可以在这个文件中放置示例文本并测试格式是否全部有效......内容被忽略但样式在新的将由 RMD 文件构建的 word 文档因此.... 那么每个“标题 1”之前都会有一个中断。

NOTE: You could obviously do the same with ANY style that has a one-to-one mapping from PANDOC MARKUP so you could instead just make all "Heading 3" or whatever.... just look at see in your RMD created DOCX what "STYLE" is being applied and then tweak that style even if you need to insert some "fake" lines with essentially blank content just for the purpose of forcing a style to appear in the DOCX注意:您显然可以对任何具有 PANDOC MARKUP 一对一映射的样式执行相同的操作,因此您可以只制作所有“标题 3”或其他任何内容......只需看看在您的 RMD 中创建的 DOCX 是什么正在应用“样式”,然后调整该样式,即使您需要插入一些具有基本空白内容的“假”行,只是为了强制样式出现在 DOCX 中

Here is an R script that can be used as a pandoc filter to replace LaTeX breaks ( \\pagebreak ) with word breaks, per @JAllen's answer above.这是一个 R 脚本,可用作 pandoc 过滤器,根据上面@Jallen 的回答,用分词符替换 LaTeX 中断符( \\pagebreak )。 With this you don't need to compile a pandoc script.有了这个,你不需要编译 pandoc 脚本。 Since you are working in R Markdown I assume one has R available in the system.由于您在 R Markdown 中工作,因此我假设系统中有 R 可用。

#!/usr/bin/env Rscript

json_in <- file('stdin', 'r')
lat_newp <- '{"t":"RawBlock","c":["latex","\\\\newpage"]}'
doc_newp <- '{"t":"RawBlock","c":["openxml","<w:p><w:r><w:br w:type=\\"page\\"/></w:r></w:p>"]}'
ast <- paste(readLines(json_in, warn=FALSE), collapse="\n")
ast <- gsub(lat_newp, doc_newp, ast, fixed=TRUE)
write(ast, "")

Save this as page-break-filter.R or something like that and make it executable by running chmod +x page-break-filter.R in the terminal.将其保存为page-break-filter.R或类似的内容,并通过在终端中运行chmod +x page-break-filter.R使其可执行。

Then include this filter the R Markdown YAML like so:然后将此过滤器包含在 R Markdown YAML 中,如下所示:

---
title: "Title
author: "Author"
output:  
  word_document:
    pandoc_args: [
      "--filter", "/path/to/page-break-filter.R"
    ]
---

You can use the R package worded .您可以使用 R 包worded This avoids the need for a template word file.这避免了对模板 word 文件的需要。 See https://github.com/davidgohel/worded .请参阅https://github.com/davidgohel/worded

The output parameter needs to be set to worded::rdocx_document and you need to call library(worded) . output参数需要设置为worded::rdocx_document并且您需要调用library(worded) worded::rdocx_document library(worded)

---
date: "2018-03-27"
author: "David Gohel"
title: "Document title"
output: 
  worded::rdocx_document
---

```{r setup, include=FALSE}
library(worded)
```

You can then add <!---CHUNK_PAGEBREAK---> to your document whenever you want a page break.然后,您可以在需要分页<!---CHUNK_PAGEBREAK---><!---CHUNK_PAGEBREAK--->添加到您的文档中。

The package allows various word formatting options using a similar mechanism.该包允许使用类似机制的各种文字格式选项。

When updating to R 4.0.0, the <!---CHUNK_PAGEBREAK---> solution was not working any more for me.更新到 R 4.0.0 时, <!---CHUNK_PAGEBREAK--->解决方案对我来说不再起作用。

Instead I could use the run_pagebreak() function from the officer package, still in combination with the officedown package:相反,我可以使用officer包中的run_pagebreak()函数,仍然与officedown包结合使用:

---
output: word_document
---

```{r settings}
library(officedown)
library(officer)
```

Hello world on page 1

`r run_pagebreak()`

Hello world on page 2

R Markdown 1.16 introduced a new feature which allows to insert a page break by adding a paragraph that contains only the commands \\pagebreak or \\newpage : R Markdown 1.16 引入了一项新功能,允许通过添加仅包含命令\\pagebreak\\newpage的段落来插入\\pagebreak

Paragraph before page break.

\pagebreak

First paragraph on a new page.

See also the pagebreaks section in the R Markdown cookbook.另请参阅 R Markdown 手册中的 分页符部分。

It is not an automated solution.它不是一个自动化的解决方案。 But I have been adding the text '#####page break' to my markdown document.但是我一直在将文本“#####page break”添加到我的降价文档中。 Then in MS Word using find-replace to replace the text "page break" with "^m" (manual page break).然后在 MS Word 中使用查找替换将文本“分页符”替换为“^m”(手动分页符)。

Sungpil's article was close, but didn't quite work. Sungpil 的文章很接近,但不太奏效。 This was the best solution I found for this: https://scriptsandstatistics.wordpress.com/2015/12/18/rmarkdown-how-to-inserts-page-breaks-in-a-ms-word-document/这是我为此找到的最佳解决方案: https : //scriptsandstatistics.wordpress.com/2015/12/18/rmarkdown-how-to-inserts-page-breaks-in-a-ms-word-document/

Even better, the author included the Word template to make this work.更好的是,作者包含了 Word 模板来完成这项工作。 The R-blogger's link to his template is broken, and the header is formatted wrong. R-blogger 指向其模板的链接已损坏,并且标题格式错误。 Some notes I took:我做的一些笔记:

1) You might need to include the whole path to the word template in your Rmd header, like so: 1) 您可能需要在 Rmd 标头中包含单词模板的整个路径,如下所示:

output: 
    word_document:
      reference_docx: C:/workspace/myproject/mystyles.docx

2) The template at the link above changed some of the default style settings so you'll need to change them back 2) 上面链接中的模板更改了一些默认样式设置,因此您需要将它们更改回来

My solution is not very robust but can work for some of us.我的解决方案不是很健壮,但可以为我们中的一些人工作。 Assuming you need a page break before each level 1 title in your word document, I defined this in the format template used in the yaml field reference_docx: .假设您需要在 word 文档中的每个级别 1 标题之前分页,我在 yaml 字段reference_docx:使用的格式模板中定义了这一点。 In this document you modify the Heading 1 format (or equivalent) to insert a page break before the Title.在本文档中,您修改标题 1 格式(或等效格式)以在标题前插入分页符。 Do not forget to start your template with the first docx rendered with knitr (pandoc) in RStudio.不要忘记使用 RStudio 中使用 knitr (pandoc) 渲染的第一个 docx 来启动您的模板。

Ok, I found this in the markdown docs.好的,我在 markdown 文档中找到了这个。

Horizontal Rule / Page Break水平规则/分页符

Three or more asterisks *** or dashes --- .三个或更多星号***或破折号---

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM