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

Incorrect modification of \begin{table} environment with labeled table in LaTeX #12085

Closed
jkrumbiegel opened this issue Feb 14, 2025 · 11 comments · Fixed by #12103
Closed

Incorrect modification of \begin{table} environment with labeled table in LaTeX #12085

jkrumbiegel opened this issue Feb 14, 2025 · 11 comments · Fixed by #12103
Assignees
Labels
bug Something isn't working crossref latex LaTeX engines related libraries and technologies tables Issues with Tables including the gt integration
Milestone

Comments

@jkrumbiegel
Copy link
Contributor

Bug description

SummaryTables.jl can create latex output and I noticed that tables don't look quite right when using quarto's table label feature. It seems like quarto tries to splice its own code into the \begin{table} environment but misses the [!ht] parameters, which are then left dangling, leading to this kind of output:

Image

Steps to reproduce

This doc just returns some raw latex with the correct MIME type, it should work with any similar latex output:

---
engine: julia

format:
    pdf:
        include-in-header:
            - text: |
                \usepackage{threeparttable}

keep-tex: true
---

```{julia}
#| label: tbl-test
struct Latex
    s::String
end

Base.show(io::IO, m::MIME"text/latex", l::Latex) = print(io, l.s)

Latex(raw"""
\begin{table}[!ht]
\setlength\tabcolsep{0pt}
\centering
\begin{threeparttable}
\begin{tabular}{@{\extracolsep{2ex}}*{5}{ccccc}}
\toprule
$\theta 1$ & $\theta 1$ & $\theta 1$ & $\theta 1$ & $\theta 1$ \\
\midrule
$\theta 2$ & $\theta 2$ & $\theta 2$ & $\theta 2$ & $\theta 2$ \\
$\theta 3$ & $\theta 3$ & $\theta 3$ & $\theta 3$ & $\theta 3$ \\
$\theta 4$ & $\theta 4$ & $\theta 4$ & $\theta 4$ & $\theta 4$ \\
$\theta 5$ & $\theta 5$ & $\theta 5$ & $\theta 5$ & $\theta 5$ \\
\bottomrule
\end{tabular}
\end{threeparttable}
\end{table}
""")
```

Actual behavior

The .tex file output contains this section, notice how some code was spliced into the \begin{table} environment, leaving the [!ht] on its own line:

\begin{table}

\caption{\label{tbl-test}}

\centering{

[!ht]
\setlength\tabcolsep{0pt}
\centering
\begin{threeparttable}
\begin{tabular}{@{\extracolsep{2ex}}*{5}{ccccc}}
\toprule
$\theta 1$ & $\theta 1$ & $\theta 1$ & $\theta 1$ & $\theta 1$ \\
\midrule
$\theta 2$ & $\theta 2$ & $\theta 2$ & $\theta 2$ & $\theta 2$ \\
$\theta 3$ & $\theta 3$ & $\theta 3$ & $\theta 3$ & $\theta 3$ \\
$\theta 4$ & $\theta 4$ & $\theta 4$ & $\theta 4$ & $\theta 4$ \\
$\theta 5$ & $\theta 5$ & $\theta 5$ & $\theta 5$ & $\theta 5$ \\
\bottomrule
\end{tabular}
\end{threeparttable}

}

\end{table}%

Expected behavior

No response

Your environment

No response

Quarto check output

quarto check
Quarto 1.6.32
[✓] Checking environment information...
      Quarto cache location: /Users/krumbiegel/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.6.32
      Path: /Applications/quarto/bin

[✓] Checking tools....................OK
      TinyTeX: v2025.01
      Chromium: (not installed)

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

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

[✓] Checking Python 3 installation....OK
      Version: 3.12.2 (Conda)
      Path: /Users/krumbiegel/miniconda3/bin/python
      Jupyter: 5.5.0
      Kernels: julia-1.11, julia-1.10, julia-1.8, julia-1.7, python3

[✓] Checking Jupyter engine render....OK

[✓] Checking R installation...........OK
      Version: 4.3.2
      Path: /Library/Frameworks/R.framework/Resources
      LibPaths:
        - /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library
      knitr: 1.45
      rmarkdown: 2.26

[✓] Checking Knitr engine render......OK
@jkrumbiegel jkrumbiegel added the bug Something isn't working label Feb 14, 2025
@mcanouil
Copy link
Collaborator

This is a known issue.

Third parties should not emit table environments as Quarto Cross-ref feature needs to add its own.
Quarto tries to be smart and parse the code produced by the user.

@mcanouil
Copy link
Collaborator

mcanouil commented Feb 14, 2025

Note that this particular case was fixed in the "parser":

It will be part of the next-prerelease (1.7.14).

@mcanouil mcanouil added duplicate This issue or pull request already exists and removed bug Something isn't working labels Feb 14, 2025
@mcanouil
Copy link
Collaborator

mcanouil commented Feb 14, 2025

FYI, there is a discussion about allowing users to explicitly tell Quarto: "I am outputting raw LaTeX without cross-reference, and I want Quarto to do nothing"1

Footnotes

  1. If you intend to use Quarto cross-reference (i.e., tbl- ID prefix), you should not emit table environments.

@jkrumbiegel
Copy link
Contributor Author

Note that this particular case was fixed in the "parser":

I see, thank you, could not locate that issue when I tried looking for preexisting ones. I get that quarto needs to have control of \begin{table} but I think it's ok as long as we don't do more than define the inner table structure. Latex is just not 100% composable here, so some compromises need to be made.

It would be hard or impossible to convert the structures SummaryTables wants to create in markdown, I will have another look into this though.

@mcanouil
Copy link
Collaborator

It would be hard or impossible to convert the structures SummaryTables wants to create in markdown, I will have another look into this though.

That's mainly why (I believe), Quarto tries to "fix" the tables but as it involves regexes, etc. and that LaTeX syntax is not very homogenous, it can't work all the time.
The simplest way to ensure Quarto cross-reference can work, is to not use the surrounding environment when possible.

@cderv
Copy link
Collaborator

cderv commented Feb 17, 2025

Note that this particular case was fixed in the "parser":

It will be part of the next-prerelease (1.7.14).

It seems this is not fixed when trying to reproduce with this julia example. So something missed somehow in #11878

I'll see how to adapt.

@cderv cderv reopened this Feb 17, 2025
@cderv cderv added bug Something isn't working latex LaTeX engines related libraries and technologies and removed duplicate This issue or pull request already exists labels Feb 17, 2025
@cderv cderv self-assigned this Feb 17, 2025
@cderv cderv added crossref tables Issues with Tables including the gt integration labels Feb 17, 2025
@cderv
Copy link
Collaborator

cderv commented Feb 17, 2025

I get that quarto needs to have control of \begin{table} but I think it's ok as long as we don't do more than define the inner table structure

Just so this is clear, if you do not emit \begin{table}, it works. You can still set !ht using tbl-pos

---
engine: julia
format:
  pdf:
    include-in-header:
      - text: |
         \usepackage{threeparttable}
keep-tex: true
keep-md: true
---

```{julia}
#| label: tbl-test
#| tbl-pos: "!ht"
struct Latex
    s::String
end

Base.show(io::IO, m::MIME"text/latex", l::Latex) = print(io, l.s)

Latex(raw"""
\setlength\tabcolsep{0pt}
\centering
\begin{threeparttable}
\begin{tabular}{@{\extracolsep{2ex}}*{5}{ccccc}}
\toprule
$\theta 1$ & $\theta 1$ & $\theta 1$ & $\theta 1$ & $\theta 1$ \\
\midrule
$\theta 2$ & $\theta 2$ & $\theta 2$ & $\theta 2$ & $\theta 2$ \\
$\theta 3$ & $\theta 3$ & $\theta 3$ & $\theta 3$ & $\theta 3$ \\
$\theta 4$ & $\theta 4$ & $\theta 4$ & $\theta 4$ & $\theta 4$ \\
$\theta 5$ & $\theta 5$ & $\theta 5$ & $\theta 5$ & $\theta 5$ \\
\bottomrule
\end{tabular}
\end{threeparttable}
""")
```

When \begin{table} is found, Quarto should try to remove it, and will not keep the option. So here I consider the [!ht] leftover the issue, but it would still not use your table asis while having Quarto cross ref working. You would need to not use Quarto cross ref to have the table passed asis

@jkrumbiegel
Copy link
Contributor Author

jkrumbiegel commented Feb 17, 2025

I meant that it should be okay to have a table with \begin{table}, knowing that quarto chops that off and does some outer adjustments, as long as the inner content of the table is sufficiently simple so it doesn't interfere with quarto's transformations.

So here I consider the [!ht] leftover the issue

Yes, me too, although @mcanouil pointed to this already being solved. I haven't tried on prerelease, yet, though.

Sorry, missed your earlier post saying it was not fixed.

@cderv
Copy link
Collaborator

cderv commented Feb 17, 2025

#12103 should fix it. This problem is a follow up on the one I fixed in #11878 - it was not complete because we do same processing but in a different code path for when the raw latex table is from a computated cell output. (meaning inside a .cell Div, with label tbl- in the Div itself.)

knowing that quarto chops that off and does some outer adjustments, as long as the inner content of the table is sufficiently simple so it doesn't interfere with quarto's transformations.

Results is the following for your example

\begin{table}

\caption{\label{tbl-test}}

\centering{

\setlength\tabcolsep{0pt}
\centering
\begin{threeparttable}
\begin{tabular}{@{\extracolsep{2ex}}*{5}{ccccc}}
\toprule
$\theta 1$ & $\theta 1$ & $\theta 1$ & $\theta 1$ & $\theta 1$ \\
\midrule
$\theta 2$ & $\theta 2$ & $\theta 2$ & $\theta 2$ & $\theta 2$ \\
$\theta 3$ & $\theta 3$ & $\theta 3$ & $\theta 3$ & $\theta 3$ \\
$\theta 4$ & $\theta 4$ & $\theta 4$ & $\theta 4$ & $\theta 4$ \\
$\theta 5$ & $\theta 5$ & $\theta 5$ & $\theta 5$ & $\theta 5$ \\
\bottomrule
\end{tabular}
\end{threeparttable}

}

\end{table}%

@cderv
Copy link
Collaborator

cderv commented Feb 18, 2025

SummaryTables.jl can create latex output and I noticed that tables don't look quite right when using quarto's table label feature.

@jkrumbiegel I did not test specifically for SummaryTables.jl output, but the issue you found is now fixed in dev version.

@cderv cderv added this to the v1.7 milestone Feb 18, 2025
@jkrumbiegel
Copy link
Contributor Author

@cderv thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working crossref latex LaTeX engines related libraries and technologies tables Issues with Tables including the gt integration
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants