代码之家  ›  专栏  ›  技术社区  ›  Pete

使用kableExtra增加行/行间距

  •  1
  • Pete  · 技术社区  · 7 年前

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

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  10
  •   Martin Schmelzer    7 年前

    您只需使用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}

    enter image description here

        2
  •  7
  •   jono3030    7 年前

    基于CL.的答案 here kable 的 linesep 带有“\addlinespace”的参数(或来自Latex的类似参数) booktabs ).像这样:

    linesep = "\\addlinespace"

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

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

        3
  •  0
  •   StanW    7 年前

    除了Martin的答案之外,您还可以像这样将标记\renewcommand{\arraystretch}{2}放入save_kable函数中(如果您和我一样只想导出pdf表而不使用R标记):

    save_kable(tableName, file="FileName.pdf", latex_header_includes = c("\\renewcommand{\\arraystretch}{2}"))
    
        4
  •  0
  •   Sander van Delden    5 年前

    这只是对Martin Schmelzer答案的补充(我是stackoverflow新手,不允许发表评论)。问题是您可以在chunck中添加数组拉伸。当一个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...)
    ```