Table caption and label in LaTeX (caption, label)
\caption{text} inside a table environment prints "Table 1: text" and numbers the table. A \label after the caption gives the table a name, and \ref prints its number anywhere in the document.
Preamble
The snippets on this page need these lines before \begin{document}:
\usepackage{booktabs}
\usepackage{caption}Add a caption and refer to the table
The label name is free; a prefix such as tab: keeps tables apart from figures and sections. The ~ before \ref keeps "Table" and the number on the same line. Compile twice so that the number appears.
\begin{table}
\centering
\begin{tabular}{lr}
\toprule
Item & Price \\
\midrule
Apple & 0.50 \\
Banana & 0.25 \\
\bottomrule
\end{tabular}
\caption{Fruit prices in euros}
\label{tab:prices}
\end{table}
Table~\ref{tab:prices} lists the prices.Put the caption above the table
Write \caption before the tabular. The standard classes put their 10 pt of space above the caption, so a caption above the table almost touches it. With the caption package loaded, the space goes between caption and table.
\begin{table}
\centering
\caption{Fruit prices in euros}
\label{tab:prices-above}
\begin{tabular}{lr}
\toprule
Item & Price \\
\midrule
Apple & 0.50 \\
Banana & 0.25 \\
\bottomrule
\end{tabular}
\end{table}Give the list of tables a shorter text
The optional argument of \caption is the text that \listoftables prints. The text in braces stays with the table.
\listoftables
\begin{table}
\centering
\caption[Fruit prices]{Fruit prices in euros, collected at three markets in May}
\begin{tabular}{lr}
\toprule
Item & Price \\
\midrule
Apple & 0.50 \\
\bottomrule
\end{tabular}
\end{table}Caption without a float or without a number
Both commands come from the caption package. \captionof{table}{text} numbers a table that is not inside a table environment, for example in a minipage. \caption*{text} prints a caption without label and number, and without an entry in the list of tables.
\begin{center}
\captionof{table}{Fruit prices in euros}
\label{tab:prices-here}
\begin{tabular}{lr}
\toprule
Item & Price \\
\midrule
Apple & 0.50 \\
\bottomrule
\end{tabular}
\end{center}Pitfalls
\labelhas to come after\caption. A label before the caption takes the number of the current section instead of the table number.- After the first compile run
\refprints??and the log shows Reference `sec:methods' on page 1 undefined. The second run fills in the number. \captionoutside a float stops with \caption outside float. Use\captionof{table}{...}there.
Compiled with pdfLaTeX from TeX Live 2023.
Related pages
- Package
caption - How-to Cross-references in LaTeX
- How-to List of figures and list of tables in LaTeX
- How-to Figure caption and label in LaTeX
- Error \caption outside float
- Package
booktabs