diff --git a/R/group_tabularray.R b/R/group_tabularray.R index 188ad571..a2a971de 100644 --- a/R/group_tabularray.R +++ b/R/group_tabularray.R @@ -20,8 +20,8 @@ group_tabularray_col <- function(x, j, ihead, ...) { out <- strsplit(x@table_string, split = "\\n")[[1]] header <- rep("", ncol(x)) - for (n in names(j)) { - header[min(j[[n]])] <- n + for (idx in seq_along(j)) { + header[min(j[[idx]])] <- names(j)[idx] } header <- paste(header, collapse = " & ") diff --git a/inst/tinytest/_tinysnapshot/group_tt-issue362_duplicate_colum_labels.tex b/inst/tinytest/_tinysnapshot/group_tt-issue362_duplicate_colum_labels.tex new file mode 100644 index 00000000..adc3b0fe --- /dev/null +++ b/inst/tinytest/_tinysnapshot/group_tt-issue362_duplicate_colum_labels.tex @@ -0,0 +1,16 @@ +\begin{table} +\centering +\begin{tblr}[ %% tabularray outer open +] %% tabularray outer close +{ %% tabularray inner open +colspec={Q[]Q[]Q[]Q[]}, +cell{1}{1}={c=2,}{halign=c,}, +cell{1}{3}={c=2,}{halign=c,}, +} %% tabularray inner close +\toprule +a & & a & \\ \cmidrule[lr]{1-2}\cmidrule[lr]{3-4} +sub1 & sub2 & sub1 & sub2 \\ \midrule %% TinyTableHeader +1 & 2 & 3 & 4 \\ +\bottomrule +\end{tblr} +\end{table} diff --git a/inst/tinytest/test-group_tt.R b/inst/tinytest/test-group_tt.R index a05dcbdc..626019d6 100644 --- a/inst/tinytest/test-group_tt.R +++ b/inst/tinytest/test-group_tt.R @@ -29,6 +29,7 @@ tab <- tt(x) |> group_tt(j = list("a" = 2:3, "b" = 4:5)) |> group_tt(j = list("c" = 1:2, "d" = 3:5)) |> group_tt(j = list("e" = 1:3, "f" = 4)) +tab@output <- "latex" expect_snapshot_print(tab, label = "group_tt-3level.tex") options(tinytable_print_output = NULL) @@ -103,3 +104,12 @@ for (o in c("latex", "typst", "markdown", "html")) { options(tinytable_print_output = NULL) } + + +# Issue #362: group_tt with duplicate column group labels in latex +tab <- data.frame(sub1 = 1, sub2 = 2, sub1 = 3, sub2 = 4, check.names = FALSE) |> + tt() |> + group_tt(j = list(a = 1:2, a = 3:4)) +tab@output <- "latex" +expect_snapshot_print(tab, "group_tt-issue362_duplicate_colum_labels.tex") +