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

custom table label conflict with gt table #11653

Open
mingsu opened this issue Dec 10, 2024 · 7 comments
Open

custom table label conflict with gt table #11653

mingsu opened this issue Dec 10, 2024 · 7 comments
Assignees
Labels
bug Something isn't working tables Issues with Tables including the gt integration upstream Bug is in upstream library
Milestone

Comments

@mingsu
Copy link

mingsu commented Dec 10, 2024

Bug description

I can not generate pdf format with custom table table using gt table.

Steps to reproduce

```qmd
---
crossref:
  custom:
    - kind: float
      key: stb
      reference-prefix: "Table S"
      space-before-numbering: false
      caption-prefix: "Table S"
      latex-env: stb
      latex-list-of-file-extension: lost
      latex-list-of-description: Supplementary Table
format: pdf
---

::: {#stb-demo}

```{r}
data.frame(x = 1:3, y = 1:3) |>
gt::gt()

```

Caption

:::
```

Expected behavior

No response

Actual behavior

r$> nvim.interlace.rmd("demo.qmd", rmddir = "/Users/ming", envir = .GlobalEnv)


processing file: demo.qmd
1/3                  
2/3 [unnamed-chunk-1]
3/3                  
output file: demo.knit.md

pandoc 
  to: latex
  output-file: demo.tex
  standalone: true
  pdf-engine: xelatex
  variables:
    graphics: true
    tables: true
  default-image-extension: pdf
  
metadata
  documentclass: scrartcl
  classoption:
    - DIV=11
    - numbers=noendperiod
  papersize: letter
  header-includes:
    - \KOMAoption{captions}{tableheading}
  block-headings: true
  crossref:
    custom:
      - kind: float
        key: stb
        reference-prefix: Table S
        space-before-numbering: false
        caption-prefix: Table S
        latex-env: stb
        latex-list-of-file-extension: lost
        latex-list-of-description: Supplementary Table
  

Rendering PDF
running xelatex - 1
  This is XeTeX, Version 3.141592653-2.6-0.999996 (TeX Live 2024) (preloaded format=xelatex)
   restricted \write18 enabled.
  entering extended mode
  
updating tlmgr

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

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.229 \fontsize
               {12.0pt}{14.4pt}\selectfont 

see demo.log for more information.
Error in `quarto::quarto_render()`:
✖ Error running quarto cli.
Caused by error:
! System command 'quarto' failed
Run `rlang::last_trace()` to see where the error occurred.

r$>

Your environment

MacOS:

  • chip Apple M1 Max
  • Sequoia 15.2 (24C98)
    IDE: NVim
nvim --version
NVIM v0.10.0
Build type: Release
LuaJIT 2.1.1713484068
Run "nvim -V1 -v" for more info
r$> sessionInfo()
R version 4.3.3 (2024-02-29)
Platform: aarch64-apple-darwin23.2.0 (64-bit)
Running under: macOS 15.2

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions
/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /opt/homebrew/Cellar/r/4.3.3/lib/R/lib/libRlapack.dylib;
  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-
8

time zone: Asia/Shanghai
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] quarto_1.4.4   rmarkdown_2.29 nvimcom_0.9.58

loaded via a namespace (and not attached):
 [1] digest_0.6.37     R6_2.5.1          later_1.4.1      
 [4] fastmap_1.2.0     xfun_0.49         knitr_1.49       
 [7] htmltools_0.5.8.1 ps_1.8.0          cli_3.6.3        
[10] processx_3.8.4    compiler_4.3.3    rstudioapi_0.17.1
[13] tools_4.3.3       evaluate_1.0.1    yaml_2.3.10      
[16] Rcpp_1.0.13-1     rlang_1.1.4       jsonlite_1.8.9   
[19] colorout_1.3-0.2 

r$>

Quarto check output

> quarto check
Quarto 1.7.1
[✓] Checking environment information...
      Quarto cache location: /Users/ming/Library/Caches/quarto
[✓] Checking versions of quarto binary dependencies...
      Pandoc version 3.4.0: OK
      Dart Sass version 1.70.0: OK
      Deno version 1.46.3: OK
      Typst version 0.11.0: OK
[✓] Checking versions of quarto dependencies......OK
[✓] Checking Quarto installation......OK
      Version: 1.7.1
      Path: /Applications/quarto/bin

[✓] Checking tools....................OK
      TinyTeX: v2024.10
      Chromium: 869685

[✓] Checking LaTeX....................OK
      Using: TinyTex
      Path: /Users/ming/Library/TinyTeX/bin/universal-darwin
      Version: 2024

[✓] Checking basic markdown render....OK

[✓] Checking Python 3 installation....OK
      Version: 3.12.4 (Conda)
      Path: /Users/ming/.config/miniconda3/bin/python
      Jupyter: 5.7.2
      Kernels: python3

(|) Checking Jupyter engine render....Traceback (most recent call last):
  File "/Applications/quarto/share/jupyter/jupyter.py", line 21, in <module>
    from notebook import notebook_execute, RestartKernel
  File "/Applications/quarto/share/jupyter/notebook.py", line 20, in <module>
    from nbclient import NotebookClient
ModuleNotFoundError: No module named 'nbclient'
[✓] Checking Jupyter engine render....OK

@mingsu mingsu added the bug Something isn't working label Dec 10, 2024
@cscheid
Copy link
Collaborator

cscheid commented Dec 10, 2024

Hm. I can't repro:

Image

@cscheid cscheid added the needs-repro Issues that are blocked until reporter provides an adequate reproduction label Dec 10, 2024
@cderv
Copy link
Collaborator

cderv commented Dec 10, 2024

@cscheid Which version of gt are you using ?
with

> packageVersion("gt")
[1] ‘0.11.1

I can repo

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

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

l.229 \fontsize
               {12.0pt}{14.4pt}\selectfont
````

Intermediate .md is 
````
::: {#stb-demo}



::: {.cell}

```{.r .cell-code}
data.frame(x = 1:3, y = 1:3) |>
gt::gt()
```

::: {.cell-output-display}
\begin{table}
\fontsize{12.0pt}{14.4pt}\selectfont
\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}rr}
\toprule
x & y \\ 
\midrule\addlinespace[2.5pt]
1 & 1 \\ 
2 & 2 \\ 
3 & 3 \\ 
\bottomrule
\end{tabular*}
\end{table}

:::
:::



Caption

:::

Another problem with gt package not emitting the right latex ? I remember something about begin{table} not being supported in float maybe ?

@mingsu Workaround : Use gt to produce HTML table that Quarto will parse and know how to write LaTeX table

---
crossref:
  custom:
    - kind: float
      key: stb
      reference-prefix: "Table S"
      space-before-numbering: false
      caption-prefix: "Table S"
      latex-env: stb
      latex-list-of-file-extension: lost
      latex-list-of-description: Supplementary Table
format: pdf
keep-md: true
---

::: {#stb-demo}

```{r}
data.frame(x = 1:3, y = 1:3) |>
gt::gt() |> gt::as_raw_html()
```

Caption

:::

````

![Image](https://github.com/user-attachments/assets/518faa53-c129-4f15-b6b1-08db376cf326)

@cderv cderv added the tables Issues with Tables including the gt integration label Dec 10, 2024
@cscheid
Copy link
Collaborator

cscheid commented Dec 10, 2024

Hm, I'm using an older version:

> packageVersion("gt")
[1] ‘0.10.1.9000’

My keep-*: true outputs also don't match.

keep-md: true

::: {#stb-demo}



::: {.cell}

```{.r .cell-code}
data.frame(x = 1:3, y = 1:3) |>
gt::gt()
```

::: {.cell-output-display}
\begingroup
\fontsize{12.0pt}{14.4pt}\selectfont
\begin{longtable}{rr}
\toprule
x & y \\ 
\midrule\addlinespace[2.5pt]
1 & 1 \\ 
2 & 2 \\ 
3 & 3 \\ 
\bottomrule
\end{longtable}
\endgroup

:::
:::



Caption

:::

keep-tex: true

\begin{document}


\begin{stb}

\centering{

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{data.frame}\NormalTok{(}\AttributeTok{x =} \DecValTok{1}\SpecialCharTok{:}\DecValTok{3}\NormalTok{, }\AttributeTok{y =} \DecValTok{1}\SpecialCharTok{:}\DecValTok{3}\NormalTok{) }\SpecialCharTok{|\textgreater{}}
\NormalTok{gt}\SpecialCharTok{::}\FunctionTok{gt}\NormalTok{()}
\end{Highlighting}
\end{Shaded}

\begingroup
\fontsize{12.0pt}{14.4pt}\selectfont
\begin{longtable*}{rr}
\toprule
x & y \\
\midrule\addlinespace[2.5pt]
1 & 1 \\
2 & 2 \\
3 & 3 \\
\bottomrule
\end{longtable*}
\endgroup

}

\caption{\label{stb-demo}Caption}

\end{stb}%




\end{document}

But I think that means this is a gt bug, not a Quarto bug.

@cderv
Copy link
Collaborator

cderv commented Dec 10, 2024

In latest version there was some change regarding latex output:
https://github.com/rstudio/gt/blob/master/NEWS.md#improvements-to-the-latex-output-format

@rich-iannone did you test the new version with Quarto ?

I think this is something too look into in gt first so that in Quarto context, gt does produce a valid output.

@rich-iannone
Copy link
Collaborator

Thanks all, there seems to be a few gt regressions w.r.t. Quarto so I'm going to get some fixes in place and release shortly thereafter.

@cderv cderv added upstream Bug is in upstream library and removed needs-repro Issues that are blocked until reporter provides an adequate reproduction labels Dec 11, 2024
@cderv cderv added this to the Future milestone Dec 11, 2024
@rich-iannone
Copy link
Collaborator

I can reproduce. As a short-term workaround, you can restore the previous behavior by using the latex.use_longtable=TRUE option in tab_options(). So this document renders successfully:

---
crossref:
  custom:
    - kind: float
      key: stb
      reference-prefix: "Table S"
      space-before-numbering: false
      caption-prefix: "Table S"
      latex-env: stb
      latex-list-of-file-extension: lost
      latex-list-of-description: Supplementary Table
format: pdf
---

::: {#stb-demo}

```{r}
library(gt)

data.frame(x = 1:3, y = 1:3) |>
  gt() |>
  tab_options(latex.use_longtable = TRUE)
```

Caption

:::

and the PDF output is this:

Image

There was a push to move to a floating table (away from longtable) in v0.11.1 (see https://github.com/rstudio/gt/releases/tag/v0.11.1 for changes). Tests have been performed with these changes but not with crossref YAML options.

@cderv could we meet soon and talk about how floating tables could work well with custom crossrefs?

@cscheid
Copy link
Collaborator

cscheid commented Dec 11, 2024

@rich-iannone the trick is GT shouldn't generally be emitting table; that is a crossref environment that uses a hardcoded table counter. So a GT table emitted in that way will always bump a specific counter (and no other). This also prevents GT tables from appearing inside Figure elements (for example).

table is the floating environment that controls crossreferences etc. tabular is the environment that builds the actual table. Libraries commonly emit both of them, but that's a mistake. Similarly, if you're emitting longtable, what you probably want is longtable* instead. longtable bumps the table counter in a hardcoded way, like the table environment.

Quarto needs to be in charge of the floating environment, and I don't think there's any reason for GT to emit longtable or table environments, because GT generally won't know which environment is appropriate. The reason longtable works right now is that Quarto literally will attempt to find \begin{longtable} in a raw block and will replace it with \begin{longtable*} to work around upstream libraries that do too much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working tables Issues with Tables including the gt integration upstream Bug is in upstream library
Projects
None yet
Development

No branches or pull requests

4 participants