如果可能,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 |
Run Code Online (Sandbox Code Playgroud)
如果将其保存,temp.Rmd然后转换为PDF文件render("temp.Rmd", output_file="temp.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
Run Code Online (Sandbox Code Playgroud)
然后,你需要更改行\usepackage{longtable,booktabs}来\usepackage{booktabs}.
如果您使用的是OS X或Linux,那么您可以使用sed并输出重定向来直接生成模板,而无需longtable:
pandoc --print-default-template=latex | sed 's/longtable,//' > mytemplate.tex
Run Code Online (Sandbox Code Playgroud)
如果您是从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
Run Code Online (Sandbox Code Playgroud)
(我在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
---
Run Code Online (Sandbox Code Playgroud)
如上面的链接所示,这将生成普通的LaTeX表,这意味着不支持表中的脚注.
| 归档时间: |
|
| 查看次数: |
4494 次 |
| 最近记录: |