在渲染PDF文件时,如何在RMarkdown中将表的所有行保留在同一页面上?

sfc*_*ung 9 r r-markdown

如果可能,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?)在必要时在表之前添加额外的行,以便表的所有行都出现在同一页面上?

Liv*_*ius 7

正如评论中所建议的那样,问题是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执行此操作,那么最简单的选项可能只是更改默认模板.(最近发布的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调用中.

RStudio

为额外的pandoc args修改你的YAML块:

---
title       : "Table"
pandoc_args : --filter path/to/filter.py
output      : 
    pdf_document
---
Run Code Online (Sandbox Code Playgroud)

如上面的链接所示,这将生成普通的LaTeX表,这意味着不支持表中的脚注.

  • 这是一个不错的主意,但似乎Pandoc将使用`longtable`环境来创建表,所以如果我不加载`longtable`包我会得到这个错误:````pandoc:从TeX源生成PDF时出错.!LaTeX错误:环境longtable undefined.有关说明,请参阅LaTeX手册或LaTeX Companion.输入H <return>以获得即时帮助....... l.481\begin {longtable}``` (3认同)

Dav*_*itt 5

最简洁的方法是在表格之前添加一个分页符(\newpage\pagebreak),尽管如果您正在编辑会移动表格位置的文本,这是不明智的。我想执行此操作的阶段将是在您完成文档编辑和测试输出之后(以检查是否有丑陋的中断),就在生成最终输出之前。

这个相关问题的答案已经在SO上。此外,显然 \pagebreak是:

实际上是一个 LaTeX 命令,而不是 Markdown 命令,但是大多数……markdown-to-pdf 引擎……使用 LaTex 并且会接受它。