vincentarelbundock / tinytable

Simple and Customizable Tables in `R`
https://vincentarelbundock.github.io/tinytable
GNU General Public License v3.0
196 stars 16 forks source link

`\cmidrule[lr]` requires `[]` in `tabularray` but `()` otherwise #307

Closed andrewheiss closed 2 months ago

andrewheiss commented 2 months ago

Related to #293 and #301 and the new theme_tt("tabular", style = "tabularray") for working with custom cross reference sections, when using group_tt(j = ...) and theme_tt("tabular", style = "tabularray"), there's a LaTeX error: "Missing number, treated as zero."

This works:

---
title: "Testing"
format:
  pdf:
    keep-tex: true
---

```{r}
library(tinytable)

tt(mtcars[1:5, ]) |>
  group_tt(j = list("Things here" = 2:3))

…and it emits this LaTeX:

```latex
\begin{table}
\centering
\begin{tblr}[         %% tabularray outer open
]                     %% tabularray outer close
{                     %% tabularray inner open
colspec={Q[]Q[]Q[]Q[]Q[]Q[]Q[]Q[]Q[]Q[]Q[]},
cell{1}{2}={c=2,}{halign=c,},
}                     %% tabularray inner close
\toprule
& Things here &  &  &  &  &  &  &  &  &  \\ \cmidrule[lr]{2-3}
mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb \\ \midrule %% TinyTableHeader
21.0 & 6 & 160 & 110 & 3.90 & 2.620 & 16.46 & 0 & 1 & 4 & 4 \\
21.0 & 6 & 160 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4 & 4 \\
22.8 & 4 & 108 &  93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 & 1 \\
21.4 & 6 & 258 & 110 & 3.08 & 3.215 & 19.44 & 1 & 0 & 3 & 1 \\
18.7 & 8 & 360 & 175 & 3.15 & 3.440 & 17.02 & 0 & 0 & 3 & 2 \\
\bottomrule
\end{tblr}
\end{table}

This doesn't work:

---
title: "Testing"
format:
  pdf:
    keep-tex: true
---

```{r}
library(tinytable)

tt(mtcars[1:5, ]) |>
  group_tt(j = list("Things here" = 2:3)) |>
  theme_tt("tabular", style = "tabularray")

…and it emits this LaTeX, with no tabularray features:

```latex
\begin{tabular}{lllllllllll}
\hline
& Things here &  &  &  &  &  &  &  &  &  \\ \cmidrule[lr]{2-3}
mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb \\ \hline
21.0 & 6 & 160 & 110 & 3.90 & 2.620 & 16.46 & 0 & 1 & 4 & 4 \\
21.0 & 6 & 160 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4 & 4 \\
22.8 & 4 & 108 &  93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 & 1 \\
21.4 & 6 & 258 & 110 & 3.08 & 3.215 & 19.44 & 1 & 0 & 3 & 1 \\
18.7 & 8 & 360 & 175 & 3.15 & 3.440 & 17.02 & 0 & 0 & 3 & 2 \\
\hline
\end{tabular}

…and with this error:

compilation failed- error
Missing number, treated as zero.
<to be read again> 
                   l
l.224 ...  &  &  &  &  &  &  \\ \cmidrule[lr]{2-3}
vincentarelbundock commented 2 months ago

Well that was an annoying one to diagnose. Turns out that tabularray modifies the booktabs syntax and requires square brackets rather than parentheses. This is what I was doing all along, but when we run theme_tt("tabular"), we revert back to standard booktabs and need to sub [] for ().

Should be fixed on Github now.

vincentarelbundock commented 2 months ago

Screenshot from 2024-07-15 10-52-38

andrewheiss commented 2 months ago

Wow that's exceptionally obscure!

8x290f

andrewheiss commented 2 months ago

Oh wait, maybe this is related. I thought that theme_tt("tabular", style = "tabularray") would still emit a tabularray-based table bc of the styleargument, but

tt(mtcars[1:5, ]) |>
  group_tt(j = list("Things here" = 2:3)) |>
  style_tt(
    tabularray_inner = "cells={font=\\footnotesize}"
  ) |> 
  theme_tt("tabular", style = "tabularray")

emits a plain old tabular:

\begin{tabular}{lllllllllll}
\hline
& Things here &  &  &  &  &  &  &  &  &  \\ \cmidrule(lr){2-3}
mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb \\ \hline
21.0 & 6 & 160 & 110 & 3.90 & 2.620 & 16.46 & 0 & 1 & 4 & 4 \\
21.0 & 6 & 160 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4 & 4 \\
22.8 & 4 & 108 &  93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 & 1 \\
21.4 & 6 & 258 & 110 & 3.08 & 3.215 & 19.44 & 1 & 0 & 3 & 1 \\
18.7 & 8 & 360 & 175 & 3.15 & 3.440 & 17.02 & 0 & 0 & 3 & 2 \\
\hline
\end{tabular}

while this

tt(mtcars[1:5, ]) |>
  group_tt(j = list("Things here" = 2:3)) |>
  style_tt(
    tabularray_inner = "cells={font=\\footnotesize}"
  ) 

emits the tabularray tblr environment:

\begin{table}
\centering
\begin{tblr}[         %% tabularray outer open
]                     %% tabularray outer close
{                     %% tabularray inner open
colspec={Q[]Q[]Q[]Q[]Q[]Q[]Q[]Q[]Q[]Q[]Q[]},
cell{1}{2}={c=2,}{halign=c,},
cells={font=\footnotesize},
}                     %% tabularray inner close
\toprule
& Things here &  &  &  &  &  &  &  &  &  \\ \cmidrule[lr]{2-3}
mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb \\ \midrule %% TinyTableHeader
21.0 & 6 & 160 & 110 & 3.90 & 2.620 & 16.46 & 0 & 1 & 4 & 4 \\
21.0 & 6 & 160 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4 & 4 \\
22.8 & 4 & 108 &  93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 & 1 \\
21.4 & 6 & 258 & 110 & 3.08 & 3.215 & 19.44 & 1 & 0 & 3 & 1 \\
18.7 & 8 & 360 & 175 & 3.15 & 3.440 & 17.02 & 0 & 0 & 3 & 2 \\
\bottomrule
\end{tblr}
\end{table}

But I might be doing something wrong

andrewheiss commented 2 months ago

Like, this is probably a separate issue, but my ultimate dream use case is to do something like this to get tabularray features and Quarto cross-referencing abilities both in and out of the custom crossref environment:

---
title: "Testing"
format:
  pdf:
    keep-tex: true
crossref:
  custom:
    - kind: float
      key: apptbl
      latex-env: apptbl
      reference-prefix: Table A
      space-before-numbering: false
      latex-list-of-description: Appendix Table
apptbl-cap-location: top
---

@tbl-test has footnote-sized text and isn't in the appendix

```{r}
#| label: tbl-test
#| tbl-cap: Regular table
library(tinytable)

tt(mtcars[1:5, ]) |>
  group_tt(j = list("Things here" = 2:3)) |>
  style_tt(
    tabularray_inner = "cells={font=\\footnotesize}"
  )

@apptbl-testing should also have footnote-sized text but use appendix numbering

::: {#apptbl-testing}

tt(mtcars[1:5, ]) |>
  group_tt(j = list("Things here" = 2:3)) |>
  style_tt(
    tabularray_inner = "cells={font=\\footnotesize}"
  ) |>
  theme_tt("tabular", style = "tabularray")

Appendix table.

:::



Currently the first table works:

<img width="601" alt="image" src="https://github.com/user-attachments/assets/1ac9f283-b939-47e7-ac13-2088a5af6498">

But the second table doesn't use tabularray:

<img width="666" alt="image" src="https://github.com/user-attachments/assets/9f4e0ba0-e049-4ab6-84e9-c5406a82e8fb">
vincentarelbundock commented 2 months ago

@andrewheiss love those power user feature requests!

There was definitely a bug.

I think your fancy example should work as expected now.

andrewheiss commented 2 months ago

It works!!

image
vincentarelbundock commented 2 months ago

Love to see this!