Scale kable table to fit page width

China☆狼群 提交于 2020-02-19 09:39:12

问题


How do you format a table in pdf using kable function? Because my output table width exceeds the width of the pdf. Here is an example:

---
output: pdf_document
---

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


回答1:


One option is to use kable_styling from the kableExtra package. The option latex_options="scale_down" will fit the table within the paper margins. See the vignette for detailed examples on all of the formatting options.

---
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")
```



来源:https://stackoverflow.com/questions/49044753/scale-kable-table-to-fit-page-width

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!