27

如何使用 kable 函数格式化 pdf 中的表格?因为我的输出表格宽度超过了pdf的宽度。这是一个例子:

---
output: pdf_document
---

```{r}
df <- cbind(mtcars[1:5,], mtcars[1:5,])
knitr::kable(df)
```

在此处输入图像描述

4

2 回答 2

38

一种选择是kable_stylingkableExtra包中使用。该选项latex_options="scale_down"将适合纸张边距内的表格。有关所有格式选项的详细示例,请参阅小插图

---
output: pdf_document
---

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

```{r}
kable(cbind(mtcars[1:5,], mtcars[1:5,]))
```

```{r}
kable(cbind(mtcars[1:5,], mtcars[1:5,]), format="latex", booktabs=TRUE) %>% 
  kable_styling(latex_options="scale_down")
```

在此处输入图像描述

于 2018-03-01T07:49:40.027 回答
8

Note if you are using longtable, this answer says,

since longtable doesn't support resizebox, you cannot use the "scale_down" option in latex_options.

However, they suggested using font_size, e.g.,

kable(df, "latex", longtable = T, booktabs = T) %>%
  kable_styling(font_size = 7)
于 2020-09-29T17:18:04.137 回答