From bb5f18cd3a194678c537971ae3c678a62ecc595a Mon Sep 17 00:00:00 2001 From: jsr-p Date: Sun, 10 Nov 2024 13:04:41 +0100 Subject: [PATCH] Align group_tt columns with multicolumn for tabular theme --- R/group_tabularray.R | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/R/group_tabularray.R b/R/group_tabularray.R index a2a971de..7a95f0f4 100644 --- a/R/group_tabularray.R +++ b/R/group_tabularray.R @@ -16,14 +16,28 @@ group_tabularray_col <- function(x, j, ihead, ...) { if (is.null(j)) { return(x) } - + out <- strsplit(x@table_string, split = "\\n")[[1]] - header <- rep("", ncol(x)) - for (idx in seq_along(j)) { - header[min(j[[idx]])] <- names(j)[idx] + if ((x@theme[[1]] == "default") && (x@output == "latex")) { + # Tabular theme; use `multicolumn` to center text + header <- paste(mapply( + function(name, x) { + sprintf( + "\\multicolumn{%s}{c}{%s}", + max(x) - min(x) + 1, + name + ) + }, + names(j), j + ), collapse = " & ") + } else { + header <- rep("", ncol(x)) + for (idx in seq_along(j)) { + header[min(j[[idx]])] <- names(j)[idx] + } + header <- paste(header, collapse = " & ") } - header <- paste(header, collapse = " & ") # \toprule -> \midrule midr <- sapply(j, function(x) sprintf("\\cmidrule[lr]{%s-%s}", min(x), max(x)))