簡體   English   中英

在渲染PDF文件時,如何在RMarkdown中將表的所有行保留在同一頁面上?

[英]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執行此操作,那么最簡單的選項可能只是更改默認模板。 (最近發布的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調用中。

RStudio

為額外的pandoc args修改你的YAML塊:

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

如上面的鏈接所示,這將生成普通的LaTeX表,這意味着不支持表中的腳注。

最干凈的方法是在表格之前添加\\pagebreak\\newpage\\pagebreak ),但如果您正在編輯將移動表格位置的文本,則這是非智能的。 我想這樣做的階段就是當你完成編輯文檔並在測試輸出之后(檢查丑陋的斷點),就在生成最終輸出之前。

對相關問題的答案已經在SO上。 此外, 顯然 \\pagebreak是:

實際上是一個LaTeX命令,而不是Markdown命令,但大多數...降價到pdf引擎...使用LaTex並將接受它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM