当从 Rmarkdown 打印 pdf 时,表的大小太大

When printing pdf from Rmarkdown size of tables too big

我已尝试查看其他帖子,但没有找到任何有用的东西

基本上,当我将使用 Rmarkdown 获得的 html 文件打印为 pdf 时,表格最终变得非常庞大,在尝试了几个代码后我无法真正调整它们的大小,如下例

page 1

此外,当使用 export_summs 时,线条之间的间距宽得离谱,我也无法调整它的大小

page 2

是否有调整表格大小的通用方法? 类似于 {r out.width="50%", out.height="50%"} ?

谢谢, 安德烈

如果您能提供一个小型的、可重现的示例,将会很有帮助。我已尽力在此处重现您的条件:

---
title: "PDF Table | Example"
author: "Alexander P. Christensen"
date: "2/4/2022"
output:
  pdf_document:
    extra_dependencies: ["adjustbox"]
---

{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

## Load packages
{r load packages, echo = TRUE, eval = TRUE, comment = NA, message = FALSE, warning = FALSE}
library(jtools)
library(knitr)
library(kableExtra)

## Example Table
{r example, echo = TRUE, eval = TRUE, comment = NA, message = FALSE, warning = FALSE}
kable(
  head(iris), booktabs = TRUE, linesep = ""
)

## Example Smaller Table (shrinks vertically)
\renewcommand{\arraystretch}{.5} 
{r example_smaller, echo = TRUE, eval = TRUE, comment = NA, message = FALSE, warning = FALSE}
kable(
  head(iris), booktabs = TRUE, linesep = ""
) %>%
  kable_styling(latex_options = "hold_position")

\newpage

## Example Even Smaller Table (shrinks vertically more)
\renewcommand{\arraystretch}{.25}
{r example_even_smaller, echo = TRUE, eval = TRUE, comment = NA, message = FALSE, warning = FALSE}
kable(
  head(iris), booktabs = TRUE, linesep = ""
) %>%
  kable_styling(latex_options = "hold_position")

## Obtain LaTeX output for adjustbox (next example)
{r example_latex, echo = TRUE, eval = FALSE, comment = NA, message = FALSE, warning = FALSE}
kable(
  head(iris), booktabs = TRUE, linesep = "", format = "latex"
) %>%
  kable_styling(latex_options = "hold_position")

This code will output:

\begin{table}[!h]
\begin{tabular}{rrrrl}
\toprule
Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\
\midrule
5.1 & 3.5 & 1.4 & 0.2 & setosa\
4.9 & 3.0 & 1.4 & 0.2 & setosa\
4.7 & 3.2 & 1.3 & 0.2 & setosa\
4.6 & 3.1 & 1.5 & 0.2 & setosa\
5.0 & 3.6 & 1.4 & 0.2 & setosa\
5.4 & 3.9 & 1.7 & 0.4 & setosa\
\bottomrule
\end{tabular}
\end{table}

## Adjust Full Table
\renewcommand{\arraystretch}{1}
\begin{table}[!h]
\begin{adjustbox}{width = 3in, totalheight = 3in, center}
\begin{tabular}{rrrrl}
\toprule
Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\
\midrule
5.1 & 3.5 & 1.4 & 0.2 & setosa\
4.9 & 3.0 & 1.4 & 0.2 & setosa\
4.7 & 3.2 & 1.3 & 0.2 & setosa\
4.6 & 3.1 & 1.5 & 0.2 & setosa\
5.0 & 3.6 & 1.4 & 0.2 & setosa\
5.4 & 3.9 & 1.7 & 0.4 & setosa\
\bottomrule
\end{tabular}
\end{adjustbox}
\end{table}

\renewcommand{\arraystretch}{.5} 将垂直调整您的 table 并且可以放置在任何 kable() 代码之前。我假设这对 jtools::export_summs.

是一样的

\begin{adjustbox}{width = 3in, totalheight = 3in, center} 会自动将 table 调整到您想要的尺寸。

使用 adjustbox 需要更多的步骤。首先,您需要使用额外的依赖项设置 YAML:

output:
  pdf_document:
    extra_dependencies: ["adjustbox"]

其次,您需要使用 format = "latex" 选项从 kable() 输出 LaTeX table:

kable(
  head(iris), booktabs = TRUE, linesep = "", format = "latex"
) %>%
  kable_styling(latex_options = "hold_position")

最后,您需要插入 table 块来替换下面代码中的 ...

\begin{table}[!h]
\begin{adjustbox}{width = 3in, totalheight = 3in, center}
\begin{tabular}{rrrrl}
\toprule

...

\end{tabular}
\end{adjustbox}
\end{table}

widthtotalheight 更改为您想要的尺寸