简体   繁体   English

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

[英]How to keep all rows of a table on the same page in RMarkdown when rendering a PDF file?

LaTex will keep all rows of a table on the same page if possible. 如果可能,LaTex会将表的所有行保留在同一页面上。 However, I found that, if I render a RMarkdown document into a PDF file, a table may span two pages if it is near the end of a page. 但是,我发现,如果我将RMarkdown文档呈现为PDF文件,如果表格靠近页面末尾,则表格可能会跨越两页。 This is odd to me because I believe the RMarkdown file is actually converted to a LaTex file before generating the PDF file. 这对我来说很奇怪,因为我相信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     |

If this is saved in temp.Rmd and then converted to a PDF file by render("temp.Rmd", output_file="temp.pdf") , the first twelve rows appear on page one and the remaining rows appear on page 2: 如果将其保存在temp.Rmd ,然后通过render("temp.Rmd", output_file="temp.pdf")转换为PDF文件,则前十二行显示在第一页上,其余行显示在第2页上:

两页的表格

Is it possible to ask render (or pandoc?) to add additional lines before a table if necessary such that all rows of a table will appear on the same page? 是否有可能要求render(或pandoc?)在必要时在表之前添加额外的行,以便表的所有行都出现在同一页面上?

As was suggested in the comments, the problem is that the default LaTeX template for pandoc uses longtable (normal LaTeX tables don't split over pages). 正如评论中所建议的那样,问题是pandoc的默认LaTeX模板使用longtable (普通的LaTeX表不会拆分页面)。 If you don't feel up to creating your own template, you can just modify the default. 如果您不想创建自己的模板,只需修改默认模板即可。

Vanilla Pandoc 香草潘多克

You can use knitr to produce a normal Markdown file. 您可以使用knitr生成正常的Markdown文件。 Then, you can use pandoc to produce the PDF/TeX file using another LaTeX template via 然后,您可以使用pandoc通过另一个LaTeX模板生成PDF / TeX文件

pandoc --template=mytemplate.xex -o myfile.pdf myfile.md

The easiest way to set up a new template is by modifying the default one, which you can get pandoc to dump to the console for you: 设置新模板的最简单方法是修改默认模板,您可以将pandoc转储到控制台:

pandoc --print-default-template=latex

Then you need to change the line \\usepackage{longtable,booktabs} to \\usepackage{booktabs} . 然后你需要将\\usepackage{longtable,booktabs}更改为\\usepackage{booktabs}

If you're on OS X or Linux, then you can use sed and output redirection to directly generate a template without longtable : 如果您使用的是OS X或Linux,则可以使用sed和输出重定向直接生成没有longtable的模板:

pandoc --print-default-template=latex | sed 's/longtable,//' > mytemplate.tex

RStudio RStudio

If you're doing this from RStudio, then the easiest option is probably to just change the default template. 如果您是从RStudio执行此操作,那么最简单的选项可能只是更改默认模板。 (Recent releases of RStudio bundle pandoc and so use things differently than system pandoc.) If you look in the "R Markdown" build/status window, you'll see something like this: (最近发布的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

(I did this example on a Mac, on Windows or Linux, this will look different.) The template is listed there in the command, which you can then modify as above. (我在Mac上,在Windows或Linux上做过这个例子,这看起来会有所不同。)模板在命令中列出,然后你可以修改如上所述。 This will of course change the behavior for all documents produced via RStudio. 这当然会改变通过RStudio生成的所有文档的行为。 To my knowledge, there's currently no publicly facing option to change the template used, but this may change as document templates seem to be an area of active work in recent releases. 据我所知,目前还没有公开的选项可以更改所使用的模板,但这可能会随着文档模板似乎成为最近版本中的活动工作区而发生变化。

EDIT (2016-05-05): 编辑(2016-05-05):

It seems that the use of longtable is hard coded in the recent versions of pandoc, so removing longtable from the preamble will generate some errors. 看来,使用longtable硬编码在最近的版本pandoc的,所以去除longtable由前导会产生一些错误。 You can get around this by using a filter . 你可以通过使用过滤器解决这个问题。

Save the linked python script and 保存链接的python脚本和

Vanilla Pandoc 香草潘多克

add the --filter path/to/filter.py flag to your your pandoc invocation. --filter path/to/filter.py标志添加到您的pandoc调用中。

RStudio RStudio

modify your YAML block for the extra pandoc args: 为额外的pandoc args修改你的YAML块:

---
title       : "Table"
pandoc_args : --filter path/to/filter.py
output      : 
    pdf_document
---

As noted in the link above, this will produce plain LaTeX tables, which means no support for footnotes in tables. 如上面的链接所示,这将生成普通的LaTeX表,这意味着不支持表中的脚注。

The cleanest way would be to add a page break ( \\newpage or \\pagebreak ) before the table, although this is unintelligent if you're editing text that would move the position of the table. 最干净的方法是在表格之前添加\\pagebreak\\newpage\\pagebreak ),但如果您正在编辑将移动表格位置的文本,则这是非智能的。 I guess the stage to do this would be when you're finished editing the document and after a test output (to check for ugly breaks), right before generating the final output. 我想这样做的阶段就是当你完成编辑文档并在测试输出之后(检查丑陋的断点),就在生成最终输出之前。

This answer to a related question is already on SO. 对相关问题的答案已经在SO上。 Also, apparently \\pagebreak is: 此外, 显然 \\pagebreak是:

actually a LaTeX command, rather than a Markdown one, but most … markdown-to-pdf engines … use LaTex and will accept it. 实际上是一个LaTeX命令,而不是Markdown命令,但大多数...降价到pdf引擎...使用LaTex并将接受它。

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

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