Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase line/row spacing with kableExtra

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

enter image description here

like image 228
Pete Avatar asked Dec 15 '18 14:12

Pete


Video Answer


4 Answers

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.

like image 181
jono3030 Avatar answered Nov 15 '22 18:11

jono3030


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}

enter image description here

like image 42
Martin Schmelzer Avatar answered Nov 15 '22 18:11

Martin Schmelzer


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}"))
like image 25
StanW Avatar answered Nov 15 '22 18:11

StanW


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...)
```
like image 43
Sander van Delden Avatar answered Nov 15 '22 18:11

Sander van Delden