quarto-dev / quarto-cli

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

Hyphen in LaTeX label messes up table formatting #9833

Open turbanisch opened 3 months ago

turbanisch commented 3 months ago

Bug description

When I use a package to generat LaTeX markup for a table and embed it "asis" in a Quarto document, the table formatting is messed up as soon as I use a hyphen in the label.

Steps to reproduce

Compile the following .qmd file:

---
title: "Untitled"
format: 
  pdf:
    keep-tex: true
    include-in-header:
      - text: |
          \usepackage{threeparttable}
---

```{r}
#| results: asis
#| echo: false
library(fixest)

mod <- feols(log(Euros) ~ dist_km | Year, 
             data = trade)

etable(
  mod,
  tex = TRUE,
  label = "tbl-test"
)

### Expected behavior

The code above produces the correct LaTeX markup that should end up verbatim in the compiled document (or rather the .tex file):

\begin{table}[htbp] \caption{\label{tbl-test} no title} \centering \begin{tabular}{lc} \tabularnewline \midrule \midrule Dependent Variable: & log(Euros)\
Model: & (1)\
\midrule \emph{Variables}\ dist_km & -0.0018$^{}$\
& ($1.02\times 10^{-5}$)\
\midrule \emph{Fixed-effects}\ Year & Yes\
\midrule \emph{Fit statistics}\ Observations & 38,325\
R$^2$ & 0.16039\
Within R$^2$ & 0.15963\
\midrule \midrule \multicolumn{2}{l}{\emph{Clustered (Year) standard-errors in parentheses}}\ \multicolumn{2}{l}{\emph{Signif. Codes:
: 0.01, *: 0.05, : 0.1}}\ \end{tabular} \end{table}


### Actual behavior

The markup for the table is messed up in the .tex file created by Quarto/Pandoc:

\begin{table}

\caption{\label{tbl-test} no title}

\centering{

[htbp]

\centering \begin{tabular}{lc} \tabularnewline \midrule \midrule Dependent Variable: & log(Euros)\
Model: & (1)\
\midrule \emph{Variables}\ dist_km & -0.0018$^{}$\
& ($1.02\times 10^{-5}$)\
\midrule \emph{Fixed-effects}\ Year & Yes\
\midrule \emph{Fit statistics}\ Observations & 38,325\
R$^2$ & 0.16039\
Within R$^2$ & 0.15963\
\midrule \midrule \multicolumn{2}{l}{\emph{Clustered (Year) standard-errors in parentheses}}\ \multicolumn{2}{l}{\emph{Signif. Codes:
: 0.01, *: 0.05, : 0.1}}\ \end{tabular}

}

\end{table}%


In this example, [htbp] ends up showing in the PDF:

<img width="511" alt="image" src="https://github.com/quarto-dev/quarto-cli/assets/51969412/6e5f814a-0538-44ba-ba83-8769f8d846ee">

### Your environment

- IDE: RStudio Version 2024.04.1+748 (2024.04.1+748)
- OS: MacOS 14.5 (23F79)

### Quarto check output

```bash
Quarto 1.4.552
[✓] 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.552
      Path: /Applications/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.2
      Path: /Users/felix/.pyenv/versions/3.12.2/bin/python3
      Jupyter: 5.7.2
      Kernels: python3

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

[✓] Checking R installation...........OK
      Version: 4.4.0
      Path: /Library/Frameworks/R.framework/Resources
      LibPaths:
        - /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library
      knitr: 1.46
      rmarkdown: 2.27

[✓] Checking Knitr engine render......OK
cscheid commented 3 months ago

The problem here is that when you use tbl-* as an identifier, Quarto needs to parse your LaTeX code (so that we can support things like control over caption placement, and so that you can use markdown-native references, like @tbl-*). Quarto is clearly getting confused about the specific LaTeX code being emitted by etable, and that is definitely a bug on our side!

We need to allow users to disable this processing, but unfortunately we haven't implemented that yet.

turbanisch commented 3 months ago

Thanks for looking into it! At least there is an easy workaround in this case by simply avoiding the hyphen; it just took me a while to figure out what was happening. It doesn't seem to be tbl-* in particular that causes the issue, some-label causes it as well.

cscheid commented 3 months ago

It doesn't seem to be tbl-* in particular that causes the issue, some-label causes it as well.

Yes. That happens because in Quarto, table elements can be referenced as Tables, Figures (or other kinds) depending on that prefix.

hughjonesd commented 2 months ago

As of 1.5.45, if you use e.g. "tbltest" rather than "tbl-test" as a label, the table looks OK in PDF, but there is no \label in the TeX, so references don't work.