gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:43:31 26 4
gpt4 key购买 nike

如果可能,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文件,前十二行出现在第一页,其余的行出现在第二页:

A table on two pages

如果需要,是否可以要求渲染(或 pandoc?)在表格前添加额外的行,以便表格的所有行都显示在同一页面上?

最佳答案

正如评论中所建议的,问题是 pandoc 的默认 LaTeX 模板使用 longtable (正常的 LaTeX 表不会在页面上拆分)。如果您不想创建自己的模板,则可以修改默认值。

Vanilla 潘多克

您可以使用 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 bundle pandoc 的最新版本,因此使用与系统 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):

好像是用 longtablehard coded在最新版本的 pandoc 中,因此删除 longtable从序言会产生一些错误。你可以通过使用 a filter 来解决这个问题。 .

保存链接的python脚本并

Vanilla 潘多克

添加 --filter path/to/filter.py标记您的 pandoc 调用。

工作室

修改你的 YAML 块以获得额外的 pandoc 参数:
---
title : "Table"
pandoc_args : --filter path/to/filter.py
output :
pdf_document
---

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

关于r - 渲染PDF文件时,如何在RMarkdown中将表格的所有行保持在同一页面上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27702019/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com