问题
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")
回答1:
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}
回答2:
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.
回答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}"))
来源:https://stackoverflow.com/questions/53794142/increase-line-row-spacing-with-kableextra