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

Include font_size argument for grouped headings for kableExtra::pack_row() function

pdmayer opened this issue · comments

With kableExtra::row_spec it is simple to modify font size for selected rows.

However, there is not an equivalent argument for the grouped headings when using kableExtra::pack_row().

I have been unable to find a workround:
Inserting font_size = 14 into pack_row() gives and unused argument error.
I've had a play around with the extra_latex_after argument, but unsurprisingly this affects the following row rather than the group heading row.
This question on SO is unanswered:
https://stackoverflow.com/questions/77106424/how-to-adjust-font-size-and-alignment-for-kableextra-table-grouping-headers-when

So I think this is an enhancement or feature request. MRE below.

library(kableExtra)

df1 <- data.frame(grp = rep(c("Group 1", "Group 2"), each = 2),
                  txt = rep("blah, blah, blah", 4),
                  val = 1:4)

kbl(df1[, -1],
    booktabs = TRUE,
    col.names = c("Text", "Value")) |> 
  pack_rows(index = table(df1$grp), font_size = 14) |> 
  row_spec(1, font_size = 14)

# this code obviously fails due to `font_size = 14` in `pack_rows` but indicates the issue and a possible resolultion.

PS. Thanks for the package which I use for most of my reporting work - its great.

Since adding this issue, I have hit apon a workround for pdfs:

library(kableExtra)

df1 <- data.frame(grp = rep(c("Group 1", "Group 2"), each = 2),
txt = rep("blah, blah, blah", 4),
val = 1:4) |>
dplyr::mutate(grp = paste0("\\begin{LARGE}", grp, "\\end{LARGE}"))

kbl(df1[, -1],
booktabs = TRUE,
col.names = c("Text", "Value")) |>
pack_rows(index = table(df1$grp),
escape = FALSE)

I would use 4 escape in the mutate function

library(kableExtra)

df1 <- data.frame(grp = rep(c("Group 1", "Group 2"), each = 2),
txt = rep("blah, blah, blah", 4),
val = 1:4) |>
dplyr::mutate(grp = paste0("\\\\begin{LARGE}", grp, "\\\\end{LARGE}"))

kbl(df1[, -1],
booktabs = TRUE,
col.names = c("Text", "Value")) |>
pack_rows(index = table(df1$grp),
escape = FALSE)