Space after every five rows in kable output (with booktabs option) in R Markdown document

后端 未结 2 1606
遥遥无期
遥遥无期 2021-01-31 02:56

I am using knitr::kable() to render tables as part of an R Markdown document (that itself is part of a bookdown project). In parti

相关标签:
2条回答
  • 2021-01-31 03:27

    Based on the example above I was interested in controlling the separation. That works nicely with the following helper function. This makes it possible to control the locations of the line separation.

    linesep<-function(x,y=character()){
      if(!length(x))
        return(y)
      linesep(x[-length(x)], c(rep('',x[length(x)]-1),'\\addlinespace',y))  
    }
    knitr::kable(
      head(iris, 20), caption = 'Here is a nice table!',
      booktabs = TRUE,
      linesep = linesep(c(3,2,1,1,3,5,4,1))
    )
    

    0 讨论(0)
  • 2021-01-31 03:44

    The reason why the row height is not always equal is that by default, kable inserts a \addlinespace every 5 rows when booktabs is specified as TRUE, as is shown here:

    linesep = if (booktabs) c('', '', '', '', '\\addlinespace') else '\\hline'
    

    To alter this, add linesep = "" as an argument to kable().

    knitr::kable(
      head(iris, 20), caption = 'Here is a nice table!',
      booktabs = TRUE,
      linesep = ""
    )
    

    See Get rid of \addlinespace in kable for more details.

    It is also worth saying that you can play around with this option if you want to change the style. For example linesep = c("", "", "", "\\hline") would add a horizontal line every four spaces.

    0 讨论(0)
提交回复
热议问题