haozhu233 / kableExtra

Construct Complex Table with knitr::kable() + pipe.

Home Page:https://haozhu233.github.io/kableExtra/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

add_header_above allow editing of toprule and cmidrule

pdmayer opened this issue · comments

In the main kbl function it is possible to edit: toprule, midrule, bottomrule and linesep to make horizontal lines thicker or thinner.

However when add_header_above is included in the kable workflow there is no equivalent to edit toprule nor cmidrule.

It would be great to have the same capacity to edit toprule and cmidrule with add_header_above.

See stack overflow question which triggered this issue: https://stackoverflow.com/questions/77647118/how-to-add-horizontal-lines-in-kable-and-the-bottom-line-being-thicker-and-orang/77648429#77648429

A MRE to illustrate the problem:

The call to toprule throws the text "4pt" into the first column of the header row.

    kbl(mtcars[1:3, 1:4],
        caption="kable vary line thickness",
        booktabs = TRUE,
        toprule = "\\toprule[4pt]",
        midrule = "\\midrule[3pt]",
        bottomrule = "\\bottomrule[5pt]",
        linesep = "\\midrule[2pt]") |>
    add_header_above(c("", "Group 1" = 2, "Group 2" = 2))

This looks like the same kind of issue as #796. It might be already fixed; I'll take a look.

There may be two issues: a) the ability to edit toprule and cmidrule in add_header_above and b) dealing with the rogue text from using the toprule argument with a width dimension in the call to kbl()?

Yes, those are separate issues. The rogue text was a bug that showed up in several places; I think I have all of them. I'll commit soon, but leave this issue open for the feature request to modify the rules that are added by add_header_above().

I took a look at the feature request. I won't try to address it, for two reasons:

  1. The \toprule setting isn't part of the added header, it's the toprule from the original kbl() call, which has already been fixed.
  2. The \cmidrule setting is very fragile; this is a LaTex issue. You could set the rule width using \cmidrule[4pt]..., and that would work, though the user interface to specify that would be a bit confusing: "\cmidrule[4pt]" is not legal LaTeX, the rest of the arguments are required.
    But then you might expect to also be able to set the colour as your example did for the other rules. As this post describes, that would require different handling for the first rule from the others, and the others would need a custom macro to fix things up. I think that if you really want to do that, you should do it by hand, it's not something kableExtra should attempt.

So I'm going to close this issue now.