Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align group_tt columns with multicolumn for tabular theme #379

Closed
wants to merge 1 commit into from

Conversation

jsr-p
Copy link
Contributor

@jsr-p jsr-p commented Nov 10, 2024

This PR sets the columns created by group_tt into a multicolumn environment
to center the names above the columns that they span. This is done by
tabularray by default but not when using tabular.

The resulting table after the fix is the last table here:
image

Together with #378 the resulting table is:
image

Code:

df <- data.frame(
  has_bun = c(1, 1, 0, 0),
  has_cheese = c(1, 0, 1, 0),
  no_bun = c(0, 1, 1, 1),
  no_cheese = c(0, 1, 0, 1),
  full_meal = c(1, 0, 1, 1),
  veggie_compatible = c(1, 0, 1, 1)
)


df |>
  tt() |>
  setNames(c(
    "Bun",
    "Cheese",
    "No Bun",
    "No Cheese",
    "Meal",
    "Veggie"
  )) |>
  style_tt(
    align = "cccccc"
  ) |>
  theme_tt("tabular") |>
  group_tt(
    j = list("Burger Components" = 1:2, "Omission" = 3:4, "Meal Options" = 5:6)
  ) |>
  group_tt(
    j = list("Overall" = 1:6)
  ) |>
  save_tt("burger_table.tex", overwrite = TRUE)

latex code:

\begin{tabular}{llllll}
\hline
\multicolumn{6}{c}{Overall} \\ \cmidrule(lr){1-6}
\multicolumn{2}{c}{Burger Components} & \multicolumn{2}{c}{Omission} & \multicolumn{2}{c}{Meal Options} \\ \cmidrule(lr){1-2}\cmidrule(lr){3-4}\cmidrule(lr){5-6}
Bun & Cheese & No Bun & No Cheese & Meal & Veggie \\ \hline
1 & 1 & 0 & 0 & 1 & 1 \\
1 & 0 & 1 & 1 & 0 & 0 \\
0 & 1 & 1 & 0 & 1 & 1 \\
0 & 0 & 1 & 1 & 1 & 1 \\
\hline
\end{tabular}

@vincentarelbundock
Copy link
Owner

Thanks for this!

One problem is that currently, this will change all LaTeX tables. This is undesirable, because we normally want to use the colspan feature of tabularray, which is much "cleaner" than modifying individual cells.

My guess is that there has to be a conditional check to make sure we are in tabular theme, like here: https://github.com/vincentarelbundock/tinytable/pull/379/files#diff-22fb54c6e87c73db96b6bf05312fa0c45d0f6580669a1e88435c4e38348f15eaR22

Currently, this check is a bit hard to do, because theme_tabular() does not actually modify the table@theme string. Frankly, I don't remember why I left default there. It might be fine to change the theme, but this should be tested. See https://github.com/vincentarelbundock/tinytable/blob/main/R/theme_tabular.R#L7

Finally, I would really, really like a change like this to be in the finalize function of the theme_tabular.R file. If we can be clever and find a way to put that code in there, it would be awesome. Modifying group_tabularray(), which is generic code for all themes, should only be a last recourse.

@jsr-p
Copy link
Contributor Author

jsr-p commented Nov 10, 2024

Thanks a lot for the feedback 😄
I will need to think about how best to adjust my PR to address the points you mentioned

@vincentarelbundock
Copy link
Owner

Closing this PR to keep the repository clean, and because it would require a major rewrite, as describe above.

Also, if someone wants to implement another version of this, I think we should first have a discussion about goals and scope. I recognize that the tabular environment is nice because it is a lightweight and basic LaTeX environment, but there are good reasons why I chose tabularray. It really keeps the code clean and simple. One of the design principles is to not modify the content of cells or wrap text in \multicolumn{} or \emph{}, using the tabularray styling commands instead.

I feel it is a bit of a slippery slope to re-implement styling and structural features "by hand".

Moreover, it is possible to have multiple "minimal" tabularray environments, with multicolumn, and to include them side-by-side in a single environment:

https://tex.stackexchange.com/questions/671696/how-to-position-two-tabularray-tables-side-by-side-in-such-a-way-that-their-top

@jsr-p jsr-p deleted the center-tabular-group_tt branch December 13, 2024 19:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants