Scale kable table to fit page width

后端 未结 2 1256
一个人的身影
一个人的身影 2021-02-07 02:36

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
-         


        
2条回答
  •  时光取名叫无心
    2021-02-07 03:09

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

提交回复
热议问题