Dav*_*vid 5 pdf ms-word page-break knitr r-markdown
我正在尝试为我的数据分析开发一个 rmarkdown 报告,该报告可以在 word_document 和 pdf_document 中编织。Bookdown 对于字幕和自动编号非常有效(https://bookdown.org/yihui/bookdown/)。剩下的唯一主要问题是如何进行对两者都适用的分页符。
对于pdf,我使用tinytex 的xelatex,\newpage效果很好。对于 Word,我使用第 5 节分页符并自定义样式(包括分页符和白色字体)。
我可以使用“编辑”>“查找...”和“全部替换”,但由于我仍在开发报告,并且需要经常测试两种格式的输出看起来都很棒。
有什么办法我可以:
谢谢!
以下是 R Markdown 文件的重现示例:
---
title: "Untitled"
author: "Me"
date: "November 15, 2018"
output:
pdf_document: default
word_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Some text.
I want a page break after this.
\newpage
##### page break
This should be the first sentence of the new page.
Some more text.
Run Code Online (Sandbox Code Playgroud)
非常感谢塔勒布的回答。正如建议的那样,我使用了您对这篇文章的回答:/sf/answers/3649200481/。
步骤1:创建一个txt文件,代码如下:
--- Return a block element causing a page break in the given format.
local function newpage(format)
if format == 'docx' then
local pagebreak = '<w:p><w:r><w:br w:type="page"/></w:r></w:p>'
return pandoc.RawBlock('openxml', pagebreak)
elseif format:match 'html.*' then
return pandoc.RawBlock('html', '<div style=""></div>')
elseif format:match '(la)?tex' then
return pandoc.RawBlock('tex', '\\newpage{}')
elseif format:match 'epub' then
local pagebreak = '<p style="page-break-after: always;"> </p>'
return pandoc.RawBlock('html', pagebreak)
else
-- fall back to insert a form feed character
return pandoc.Para{pandoc.Str '\f'}
end
end
-- Filter function called on each RawBlock element.
function RawBlock (el)
-- check that the block is TeX or LaTeX and contains only \newpage or
-- \newpage{} if el.format:match '(la)?tex' and content:match
-- '\\newpage(%{%})?' then
if el.text:match '\\newpage' then
-- use format-specific pagebreak marker. FORMAT is set by pandoc to
-- the targeted output format.
return newpage(FORMAT)
end
-- otherwise, leave the block unchanged
return nil
end
Run Code Online (Sandbox Code Playgroud)
步骤 2:将文件另存为 page-break.lua 与我的 R Markdown 文件位于同一目录中。
步骤 3:添加链接作为 pandoc 参数。
这是可重现的示例(R Markdown 文件)更正的:
---
title: "Untitled"
author: "Me"
date: "November 15, 2018"
output:
pdf_document: default
word_document:
pandoc_args:
'--lua-filter=page-break.lua'
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Some text.
I want a page break after this.
\newpage
This should be the first sentence of the new page.
Some more text.
Run Code Online (Sandbox Code Playgroud)
请注意,这可能不适用于目录,但我不使用 lua 过滤器与 pdf 和 word _document ,之后直接在 Word 中添加目录非常容易。另外,上面的链接中有一个解决该问题的链接。
| 归档时间: |
|
| 查看次数: |
10278 次 |
| 最近记录: |