Increase line/row spacing with kableExtra

≡放荡痞女 提交于 2020-06-12 04:35:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!