35

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

enter image description here

Michael Harper
  • 14,721
  • 2
  • 60
  • 84
Bustergun
  • 977
  • 3
  • 11
  • 17
  • Possible duplicate of [Automatically adjust LaTeX table width to fit pdf using knitr and Rstudio](https://stackoverflow.com/questions/16507191/automatically-adjust-latex-table-width-to-fit-pdf-using-knitr-and-rstudio) – Michael Harper Mar 01 '18 at 10:57

2 Answers2

49

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

enter image description here

eipi10
  • 91,525
  • 24
  • 209
  • 285
  • Hi, why is there an error when I try to the code into pdf. pandoc.exe: Error producing PDF Error: pandoc document conversion failed with error 43 In addition: Warning message: running command '"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS Model_v101A_Validation_NewTables_v7.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identi – Bustergun Mar 01 '18 at 07:58
  • You haven't provided a reproducible example, so it's difficult to determine what's causing the error. – eipi10 Mar 01 '18 at 07:59
  • Oh I already saw my error. I did not include the r setup. Thanks. – Bustergun Mar 01 '18 at 08:04
  • Hi, could you explain the purpose of "%>%"? I am new to R. – Dylan Russell Sep 15 '18 at 03:14
  • It's a "pipe" operator. It pipes the result of the first function into the second one. Take a look this [`dplyr` tutorial](https://cran.r-project.org/web/packages/dplyr/vignettes/dplyr.html) to see common use case of pipes for data manipulation in R. – eipi10 Sep 16 '18 at 00:17
  • Hi, this solution works good but makes the figure to move to the top of the page in the output, independently of its original place. How can that be avoided? @eipi10 – Drubio Nov 05 '18 at 12:01
  • 1
    Inside `kable_styling` add `hold_position=TRUE`. – eipi10 Nov 29 '19 at 16:30
  • is there a way to just set a margin limit and have the table not extend beyond that limit? – user35131 Aug 19 '21 at 19:25
  • What if you have so long a text footnote that extends outside the table after `scale_down`? It seems like `scale_down` does not apply to `footnote`. – Jia Gao Aug 25 '22 at 14:43
13

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)
dfrankow
  • 20,191
  • 41
  • 152
  • 214