使用 kableExtra 增加行/行间距

问题描述 投票:0回答:5

有没有办法使用 kableExtra 增加 r-markdown 或 bookdown 中 pdf 输出的行间距?

library(knitr)
library(kableExtra)
kable(
  head(iris, 5), caption = 'Iris Table',
  booktabs = TRUE) %>%
  kable_styling(latex_options = "striped")

r r-markdown bookdown kable kableextra
5个回答
16
投票

您可以使用 LaTeX 命令来完成此操作

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

请注意,以下所有表格都将使用相同的间距。但您可以使用

\renewcommand{\arraystretch}{1}

重置它


11
投票

基于 CL.'s 答案here,您还可以使用

kable
linesep
参数与“ddlinespace”(或来自 Latex'
booktabs
的类似参数)。像这样:

linesep = "\\addlinespace"

你的例子:

kable(head(iris, 5),
  "latex",
  caption = 'Iris Table',
  booktabs = T,
  linesep = "\\addlinespace") %>%
  kable_styling(latex_options = "striped")

我认为

\arraystretch
更改整个表格的行间距,包括标题、注释等,而
linesep
仅控制表格主体的行间距。这样您也不必将自定义 Latex 代码引入到您的 Rmarkdown 文档中。


2
投票

添加到马丁的答案中,您还可以添加标签 enewcommand{ rraystretch}{2} 进入 save_kable 函数,如下所示(如果您像我一样,只想导出 pdf 表而不使用 R Markdown):

    save_kable(tableName, 
      file="FileName.pdf", 
      latex_header_includes = c("\\renewcommand{\\arraystretch}{2}"))


1
投票

这只是 Martin Schmelzer 答案的补充(我是 stackoverflow 的新手,不允许发表评论)。问题是你可以在块中添加数组拉伸。当一个块中有多个表时就派上用场了。

```{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...)
```

1
投票

padding
论证可以做到这一点

row_spec(1:nrow(yourdata),
           extra_css = "padding: 10px")
© www.soinside.com 2019 - 2024. All rights reserved.