![](/img/trans.png)
[英]How to supress page numbers when rendering a R script into a pdf document using rmarkdown::render()
[英]How to keep all rows of a table on the same page in RMarkdown when rendering a PDF file?
如果可能,LaTex会将表的所有行保留在同一页面上。 但是,我发现,如果我将RMarkdown文档呈现为PDF文件,如果表格靠近页面末尾,则表格可能会跨越两页。 这对我来说很奇怪,因为我相信RMarkdown文件在生成PDF文件之前实际上已转换为LaTex文件。
---
title : "Table"
output :
pdf_document
---
# Section 1
# Section 2
# Section 3
# Section 4
# Section 5
# Section 6
# Section 7
# Section 8
# Section 9
# Section 10
# Section 11
# Section 12
# Section 13
Column 1 | Column 2 |
------------- | -------------|
1) Cell | Cell |
2) Cell | Cell |
3) Cell | Cell |
4) Cell | Cell |
5) Cell | Cell |
6) Cell | Cell |
7) Cell | Cell |
8) Cell | Cell |
9) Cell | Cell |
10) Cell | Cell |
11) Cell | Cell |
12) Cell | Cell |
13) Cell | Cell |
14) Cell | Cell |
15) Cell | Cell |
16) Cell | Cell |
17) Cell | Cell |
18) Cell | Cell |
如果将其保存在temp.Rmd
,然后通过render("temp.Rmd", output_file="temp.pdf")
转换为PDF文件,则前十二行显示在第一页上,其余行显示在第2页上:
是否有可能要求render(或pandoc?)在必要时在表之前添加额外的行,以便表的所有行都出现在同一页面上?
正如评论中所建议的那样,问题是pandoc的默认LaTeX模板使用longtable
(普通的LaTeX表不会拆分页面)。 如果您不想创建自己的模板,只需修改默认模板即可。
您可以使用knitr
生成正常的Markdown文件。 然后,您可以使用pandoc通过另一个LaTeX模板生成PDF / TeX文件
pandoc --template=mytemplate.xex -o myfile.pdf myfile.md
设置新模板的最简单方法是修改默认模板,您可以将pandoc转储到控制台:
pandoc --print-default-template=latex
然后你需要将\\usepackage{longtable,booktabs}
更改为\\usepackage{booktabs}
。
如果您使用的是OS X或Linux,则可以使用sed
和输出重定向直接生成没有longtable
的模板:
pandoc --print-default-template=latex | sed 's/longtable,//' > mytemplate.tex
如果您是从RStudio执行此操作,那么最简单的选项可能只是更改默认模板。 (最近发布的RStudio捆绑包pandoc所以使用与system pandoc不同的东西。)如果你查看“R Markdown”构建/状态窗口,你会看到类似这样的东西:
output file: rmarkdown.knit.md
/Applications/RStudio.app/Contents/MacOS/pandoc/pandoc rmarkdown.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output rmarkdown.pdf --template /Library/Frameworks/R.framework/Versions/3.0/Resources/library/rmarkdown/rmd/latex/default.tex --highlight-style tango --latex-engine /usr/texbin/pdflatex --variable 'geometry:margin=1in'
Output created: rmarkdown.pdf
(我在Mac上,在Windows或Linux上做过这个例子,这看起来会有所不同。)模板在命令中列出,然后你可以修改如上所述。 这当然会改变通过RStudio生成的所有文档的行为。 据我所知,目前还没有公开的选项可以更改所使用的模板,但这可能会随着文档模板似乎成为最近版本中的活动工作区而发生变化。
编辑(2016-05-05):
看来,使用longtable
被硬编码在最近的版本pandoc的,所以去除longtable
由前导会产生一些错误。 你可以通过使用过滤器解决这个问题。
保存链接的python脚本和
将--filter path/to/filter.py
标志添加到您的pandoc调用中。
为额外的pandoc args修改你的YAML块:
---
title : "Table"
pandoc_args : --filter path/to/filter.py
output :
pdf_document
---
如上面的链接所示,这将生成普通的LaTeX表,这意味着不支持表中的脚注。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.