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

3

51

I am using knitr::kable() to render tables as part of an R Markdown document (that itself is part of a bookdown project). In particular, the booktabs option (through setting the booktabs argument to equal TRUE) renders the table in a nice-looking way. However, I'd like for there not to be a space after every five rows.

Here, for example, is the code and how the table in the bookdown demo appears when rendered as a PDF:

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

iris table with booktabs

I'd like for the space that aappears after every five rows to not be included, but I cannot seem to find a setting in knitr::kable() that does this.

Nasho answered 27/2, 2018 at 18:29 Comment(0)
G
91

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 = ""
)

enter image description here

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.

Gyre answered 27/2, 2018 at 22:21 Comment(2)
Thanks, that did the trick. I think the reason why I didn't find that Q&A was that I wasn't searching for \addlinespace.Nasho
Knowing the right search term is always the hardest part :)Gyre
O
10

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))
)

enter image description here

Oftentimes answered 22/1, 2021 at 11:30 Comment(0)
T
1

Bart's answer failed to work for me on my remote server when using prettyNum() on the table and passing the output to kableExtra::kable_styling(), where it just added an column full of NAs and no linespaces. It seems to work fine on my local machine, so I can't reproduce the error. Anyway, this alternative solution works for me (but only when passing the table to kableExtra for some reason):

linesep <- function(table, groups) {
 sep_indx <- rep("", nrow(table))
 sep_indx[cumsum(groups)] <- '\\addlinespace'
 return(sep_indx)
}

knitr::kable(
  head(iris, 20),
  caption = 'Here is a nice table!',
  booktabs = TRUE,
  linesep = linesep(head(iris, 20), c(3, 2, 1, 1, 3, 5, 4, 1))
) %>% kableExtra::kable_styling(latex_options = c("scale_down", "HOLD_position"))

example

Truett answered 28/1, 2022 at 9:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.