kableExtra icon indicating copy to clipboard operation
kableExtra copied to clipboard

toprule in kbl() creates hline under pack_rows

Open robert702 opened this issue 3 years ago • 1 comments

When specifying a top rule in kbl, and additional line comes under a header introduced by pack_rows

This is the reproducible example:

library(kableExtra)
kbl(mtcars[1:10, 1:6], format= "latex", caption = "Group Rows", booktabs = T, toprule = "\\hline \\hline") %>%
  kable_styling() %>%
  pack_rows("Group 1", 4, 7) %>%
  pack_rows("Group 2", 8, 10)

This is a great package, the most flexible I've worked with. Hopefully there is a quick fix to what appears to be a bug.

robert702 avatar Sep 24 '21 01:09 robert702

A possible solution to produce the output you are looking for:

library(kableExtra)
kbl(mtcars[1:10, 1:6], format= "latex", caption = "Group Rows", booktabs = T, 
    toprule = "\\toprule[0.4pt] \\toprule[0.4pt]") %>%
  kable_styling() %>%
  pack_rows("Group 1", 4, 7) %>%
  pack_rows("Group 2", 8, 10)

The reason this works is that the pack_rows function uses the Latex input to determine if the table uses booktabs or not. It does this by checking for the \toprule command, which is defined by the booktabs Latex package. Since you are changing \toprule to \hline \hline it determines that booktabs is not being used and inserts the offending lines after the group text. The above solution uses the \toprule command in place of \hline and then changes the line thickness to 0.4pt to match that used by \hline.

To actually change this behavior I suppose that the magic_mirror function (which determines if the table uses booktabs or not) could be changed to also check for other commands defined by booktabs, \midrule and \bottomrule, however I'm unsure of what knock on effects this could have and it would still be an issue if the user were to change these as well.

estraiton avatar Dec 02 '21 14:12 estraiton