Jean-Alexandre Patteet
Jean-Alexandre Patteet

Reputation: 159

referencing latex table in quarto

I have the following table in my quarto document and I would like to reference it automatically in my text.

Quarto understands that this is my second table and is able to name it Table 2 in the caption but I can't find a way to reference this table automatically in my text.

I have this line in my YAML to make the table work.

---
format: pdf
header-includes:
  - \usepackage{multirow} 
---


```{=latex}
\begin{table}[]
\caption{Summary of the different variabilities when comparing marks from same or different tools and zones}
\label{Variabilities}
\begin{center}
\begin{tabular}{ll|cc|}
\cline{3-4}
                                                     &                    & \multicolumn{2}{c|}{\textbf{Tool}}                                           \\ \cline{3-4} 
                                                     &                    & \multicolumn{1}{l|}{\textit{Same}} & \multicolumn{1}{l|}{\textit{Different}} \\ \hline
\multicolumn{1}{|c|}{\multirow{2}{*}{\textbf{Zone}}} & \textit{Same}      & \multicolumn{1}{c|}{W}             & B                                       \\ \cline{2-4} 
\multicolumn{1}{|c|}{}                               & \textit{Different} & \multicolumn{1}{c|}{B}             & B                                       \\ \hline
\end{tabular}
\end{center}
\end{table}
```

If anyone knows how to give this table a label that I can call in the text for referencing.

HERE IS THE ANSWER TO THIS

Two more lines have to be introduced to the YAML in order to use the package cleveref

---
format: pdf
header-includes:
  - \usepackage{multirow}
  - \usepackage{hyperref}
  - \usepackage[capitalise,noabbrev]{cleveref}
---

The way the table is written does not change. To reference the table in the text as "Table X", you have to write

\ref{Variabilities}

(because I have \label{Variabilities} in my table).

Thanks @samcarter_is_at_topanswers.xyz for your help

Upvotes: 3

Views: 1810

Answers (3)

Fran
Fran

Reputation: 264

Because the tabular is in LaTeX for a LaTeX/pdf output, it does not harm to have a cross-reference in LaTeX with \ref{} too, but this is, in general, the wrong way to make cross-references in Quarto, because will work only for latex/pdf outputs.

The table float, caption and label could be in quarto syntax and the use of the quarto cross-reference with a #tbl-yourlabel label and a @tbl-yourlabel. This is not only a simpler syntax, but also compatible with other format outputs, although this only is an advantage if the tabular inside is also in markdown or generated by R (otherwise it will be omitted).


enter image description here


---
format: 
  html: default
  pdf: default
---

See the @tbl-Variabilities.

:::{#tbl-Variabilities tbl-pos="h"}

 
```{=latex}

\begin{tabular}{ll|cc|}
a & b & c & d \\  
\end{tabular}

```

Summary of the different variabilities when comparing marks from same or different tools and zones

:::

Upvotes: 0

Jean-Alexandre Patteet
Jean-Alexandre Patteet

Reputation: 159

Two more lines have to be introduced to the YAML in order to use the package cleveref

---
format: pdf
header-includes:
  - \usepackage{multirow}
  - \usepackage{hyperref}
  - \usepackage[capitalise,noabbrev]{cleveref}
---

The way the table is written does not change. To reference the table in the text as "Table X", you have to write

\ref{Variabilities}

(because I have \label{Variabilities} in my table).

Thanks @samcarter_is_at_topanswers.xyz for your help

Upvotes: 2

You can use the \cref{Variabilities} from the cleveref package:

\documentclass{article}

\usepackage{multirow}
\usepackage[capitalise,noabbrev]{cleveref}

\begin{document}

\begin{table}[]
\caption{Summary of the different variabilities when comparing marks from same or different tools and zones}
\label{Variabilities}
\begin{center}
\begin{tabular}{ll|cc|}
\cline{3-4}
 &                    & \multicolumn{2}{c|}{\textbf{Tool}}                                           \\ \cline{3-4} 
 &                    & \multicolumn{1}{l|}{\textit{Same}} & \multicolumn{1}{l|}{\textit{Different}} \\ \hline
\multicolumn{1}{|c|}{\multirow{2}{*}{\textbf{Zone}}} & \textit{Same}      & \multicolumn{1}{c|}{W}             & B                                       \\ \cline{2-4} 
\multicolumn{1}{|c|}{}                               & \textit{Different} & \multicolumn{1}{c|}{B}             & B                                       \\ \hline
\end{tabular}
\end{center}
\end{table}

\cref{Variabilities}


\end{document}

Upvotes: 3

Related Questions