Increase line/row spacing with kableExtra
Asked Answered
C

5

11

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

Codeclination answered 15/12, 2018 at 14:29 Comment(0)
A
18

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

Amaro answered 15/12, 2018 at 15:52 Comment(0)
C
11

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.

Chickabiddy answered 13/8, 2019 at 0:47 Comment(0)
A
3

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

Archiepiscopate answered 16/7, 2019 at 9:6 Comment(0)
M
2

The padding argument can do that

row_spec(1:nrow(yourdata),
           extra_css = "padding: 10px")
Meyer answered 2/12, 2022 at 10:33 Comment(0)
P
1

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...)
```
Possess answered 7/3, 2021 at 14:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.