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

Unable to place tables in custom crossref environments in Quarto PDFs #293

Closed andrewheiss closed 3 months ago

andrewheiss commented 3 months ago

In Quarto, it's possible to create a custom crossref type for things like appendix tables. Here's a minimal reprex:

---
title: "Example"
format:
  pdf: default
  html: default
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
---

See @apptbl-testing

::: {#apptbl-testing}

```{r}
library(tinytable)

tt(mtcars[1:5,])

Caption goes here.

:::


{tinytable} works fine with custom crossrefs when rendering to HTML:

<img width="832" alt="image" src="https://github.com/vincentarelbundock/tinytable/assets/73663/ba8adca3-b0b4-402d-b885-db8aa329adb6">

&nbsp;

But when rendering to PDF, it causes an error:

ERROR: compilation failed- error LaTeX Error: Not in outer par mode.

See the LaTeX manual or LaTeX Companion for explanation. Type H for immediate help. ...

l.240 \centering

see testing.log for more information.


The line 240 it's complaining about is the `\centering` immediately after `\begin{table}`, which apparently isn't allowed there since it's already in a `\centering{}` environment right after the caption definition?

```latex
\begin{apptbl}

\caption{\label{apptbl-testing}Caption goes here.}

\centering{

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(tinytable)}

\FunctionTok{tt}\NormalTok{(mtcars[}\DecValTok{1}\SpecialCharTok{:}\DecValTok{5}\NormalTok{,])}
\end{Highlighting}
\end{Shaded}

\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[]},
}                     %% tabularray inner close
\toprule
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}

}

\end{apptbl}%
vincentarelbundock commented 3 months ago

I don't have as much time to diagnose because vacation, but I don't think the problem is \centering. To illustrate, we can use the finalize argument of style_tt() to regex that bit out of the table, and it still breaks. In contrast, when using a raw tabular environment (using the latest tinytable 0.3.0.20), things work fine.

So there seems to be a bad interaction between whatever fancy thing Quarto does and the tblr environment from tabularray. Not sure yet what it is...

Example:

```{r}
library(tinytable)

packageVersion("tinytable")

# Good
tt(head(mtcars)) |> theme_tt("tabular") 

# Bad
clean <- function(x) {
    if (x@output == "latex") {
        x@table_string <- gsub("\\\\centering", "", x@table_string)
    }
    x
}
tt(head(mtcars)) |> style_tt(finalize = clean)

Requires tinytable from Github as of this morning.

vincentarelbundock commented 3 months ago

OK, I tracked down the problem, added a new feature to leverage as workaround, and documented all of this on the FAQ page:

https://vincentarelbundock.github.io/tinytable/vignettes/faq.html#quarto

Thanks for the report. This is an interesting use-case. I wasn't aware we could do things like that.

andrewheiss commented 2 months ago

Great, thanks so much!