如何使用 kable 函数格式化 pdf 中的表格?因为我的输出表格宽度超过了pdf的宽度。这是一个例子:
---
output: pdf_document
---
```{r}
df <- cbind(mtcars[1:5,], mtcars[1:5,])
knitr::kable(df)
```
如何使用 kable 函数格式化 pdf 中的表格?因为我的输出表格宽度超过了pdf的宽度。这是一个例子:
---
output: pdf_document
---
```{r}
df <- cbind(mtcars[1:5,], mtcars[1:5,])
knitr::kable(df)
```
一种选择是kable_styling
从kableExtra
包中使用。该选项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")
```
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)