Is there a way to increase the line spacing with kableExtra for a pdf output in r-markdown or bookdown?
library(knitr)
library(kableExtra)
kable(
head(iris, 5), caption = 'Iris Table',
booktabs = TRUE) %>%
kable_styling(latex_options = "striped")
Building on CL.'s answer here you could also use kable
's linesep
argument with '\addlinespace' (or similar arguments from Latex' booktabs
). Like so:
linesep = "\\addlinespace"
Your example:
kable(head(iris, 5),
"latex",
caption = 'Iris Table',
booktabs = T,
linesep = "\\addlinespace") %>%
kable_styling(latex_options = "striped")
I think that \arraystretch
changes line spacing for the entire table including headers, notes etc. whereas linesep
controls only the line spaces for the table body. That way you also wouldn't have to introduce custom Latex code into your Rmarkdown document.
You can just do it using the LaTeX command \arraystretch
:
---
output: pdf_document
---
```{r setup, include=FALSE}
library(kableExtra)
library(tidyverse)
```
\renewcommand{\arraystretch}{2}
```{r, echo=FALSE}
library(knitr)
library(kableExtra)
kable(head(iris, 5), caption = 'Iris Table',booktabs = TRUE) %>%
kable_styling(latex_options = "striped")
```
Notice that all following tables would use the same spacing. But you could reset it using \renewcommand{\arraystretch}{1}
Adding to Martin's answer, you can also put the tag \renewcommand{\arraystretch}{2} into the save_kable function like so (in case you, like me, just want to export a pdf table without using R Markdown):
save_kable(tableName, file="FileName.pdf", latex_header_includes = c("\\renewcommand{\\arraystretch}{2}"))
This is just an addition to Martin Schmelzer answer (I'm new to stackoverflow and not allowed to comment). Thing is you can add the array stretch within the chunck. Comes in hand when there are multiple tables in one chunck.
```{r, echo=FALSE}
#array stretch increases row height
cat("\\renewcommand{\\arraystretch}{2} \n")
#This is the table
kable(head(iris, 5), caption = 'Iris Table',booktabs = TRUE) %>%
kable_styling(latex_options = "striped")
#array stretch sets row height back
cat("\\renewcommand{\\arraystretch}{1} \n")
kable(....another table in chunck that is unaffected...)
```
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With