quarto-dev / quarto-cli

Open-source scientific and technical publishing system built on Pandoc.
https://quarto.org
Other
3.95k stars 326 forks source link

Compilation error when using italic text in LaTeX table caption #10593

Open aaronrudkin opened 2 months ago

aaronrudkin commented 2 months ago

Bug description

If I attempt to use LaTeX formatting commands (e.g. \textit{italic text}) inside a table caption and the table has a cross-ref, the document will not compile. Removing the italic command compiles successfully. I assume this is true of any arbitrary LaTeX comand. The reason this is important is because I want to set arbitrary styling for the caption, and in particular I want to be able to have a "title" in the caption, which I do via italic/bold text.

Steps to reproduce

---
title: "Reprex for Italic Table issue"
format: pdf
---

Below I cross-ref @tbl-example

\begingroup
\centering
\begin{table}
   \begin{tabular}{lr}
      \tabularnewline \midrule \midrule
      Text Here & 123 \\
      Text Here & 456 \\
      \midrule \midrule
   \end{tabular}
   \caption{\label{tbl-example} \textit{Italic} A caption goes here}
\end{table}
\par\endgroup

Behavior

When Quarto re-writes the table to hook up the cross-ref, it generates the following invalid intermediate LaTeX which will not compile. You can see that the italic text gets pulled up into the caption, but the stuff following the } closing curly brace does not, leaving it stranded under the tabular environment, and causing a compilation error because the caption now wraps all the way around the table.

\begin{table}

\caption{\label{tbl-example} \textit{Italic}

\centering{

   \begin{tabular}{lr}
      \tabularnewline \midrule \midrule
      Text Here & 123 \\
      Text Here & 456 \\
      \midrule \midrule
   \end{tabular}
    A caption goes here}

}

\end{table}%

Your environment

Quarto check output

Quarto 1.4.553
[✓] Checking versions of quarto binary dependencies...
      Pandoc version 3.1.11: OK
      Dart Sass version 1.69.5: OK
      Deno version 1.37.2: OK
[✓] Checking versions of quarto dependencies......OK
[✓] Checking Quarto installation......OK
      Version: 1.4.553
      Path: /Applications/RStudio.app/Contents/Resources/app/quarto/bin

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

[✓] Checking LaTeX....................OK
      Using: Installation From Path
      Path: /Library/TeX/texbin
      Version: 2024

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

[✓] Checking Python 3 installation....OK
      Version: 3.12.4
      Path: /opt/homebrew/opt/python@3.12/bin/python3.12
      Jupyter: (None)

      Jupyter is not available in this Python installation.
      Install with python3 -m pip install jupyter

[✓] 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.25

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

maja:writing aaron$ 
mcanouil commented 2 months ago

You should not use bare LaTeX code in the middle of Markdown.

See https://quarto.org/docs/authoring/markdown-basics.html#raw-content.

mcanouil commented 2 months ago

This being said, you should use Quarto cross-reference system instead of overtaking it using LaTeX.

---
title: "Reprex for Italic Table issue"
format: pdf
---

Below I cross-ref @tbl-example

::: {#tbl-example}
```{=latex}
\begin{tabular}{lr}
  \tabularnewline \midrule \midrule
  Text Here & 123 \\
  Text Here & 456 \\
  \midrule \midrule
\end{tabular}

Italic A caption goes here :::



Finally, when working with LaTeX and encountering LaTeX error, use `keep-tex: true` in your YAML front matter to investigate the issue.
mcanouil commented 2 months ago

FYI, this is the same kind of issues as of:

aaronrudkin commented 2 months ago

This is being used in a pipeline wherein the LaTeX is being generated programmatically by outside code and included using an include directive e.g. {{< include ../tables/table_goes_here.tex >}}. The LaTeX appearing in "the middle of Markdown" is an artifact of the reprex. It would be possible to write a shim that wraps the LaTeX output in an additional layer to build markdown around it, but honestly not ideal. I did keep the intermediate TeX file, which generates the TeX in my "Behavior" section and is how I diagnosed that.

It does seem to me like this is a bug with how the cross-ref rewrites the LaTeX table. If it's a wontfix for the same reason as the linked issue, then fine, but I figured I'd report it nonetheless.

mcanouil commented 2 months ago

but I figured I'd report it nonetheless.

Thanks for that! We do appreciate the report.

If it's a wontfix for the same reason as the linked issue

I can't answer that myself.

Regarding the include, I suggest (which should work and put some safety in parsing the LaTeX code):

```{=latex}
{{< include ../tables/table_goes_here.tex >}}
cscheid commented 2 months ago

Quarto 1.4.553

I'll note that this is an old version of Quarto, and it might be worth confirming that the issue persists in 1.5.

mcanouil commented 2 months ago

@cscheid The code lead to a crash using the latest development version. Same thing as in the other issue. The brackets from the italic command causes the parsing issue in Quarto cross-ref codepath.

\begin{table}

\caption{\label{tbl-example} \textit{Italic}

\centering{

   \begin{tabular}{lr}
      \tabularnewline \midrule \midrule
      Text Here & 123 \\
      Text Here & 456 \\
      \midrule \midrule
   \end{tabular}
    A caption goes here}

}

\end{table}%