6

Consider this script:

\documentclass{report}
\usepackage{longtable}
\begin{document}
See \ref{tab:results}~tab and \ref{tab:moreresults}~tab.
\begin{table}[htp]
\centering
\caption{Results}
\label{tab:results}
\begin{longtable}{lr}
A & B \\
\end{longtable}
\end{table}

\begin{table}[htp]
\centering
\caption{More results}
\label{tab:moreresults}
\begin{longtable}{lr}
C & D \\
\end{longtable}
\end{table}
\end{document}

The output is:

enter image description here

It seems LaTeX has created a ghost table 2. What is wrong?

moewe
  • 175,683
Viesturs
  • 7,895
  • 5
    A longtable comes with its own table environment and therefore should not be wrapped into \begin{table}...\end{table}. See https://tex.stackexchange.com/q/219138/35864 on how to use it with a caption. – moewe Feb 18 '19 at 11:06

1 Answers1

5

longtable need not be (and as you discovered should not be) nested inside a table environment.

To quote the page 1 of the excellent documentation

The longtable package defines a new environment, longtable, which has most of the features of the tabular environment, but produces tables which may be broken by TeX's standard page-breaking algorithm. It also shares some features with the table environment. In particular it uses the same counter, table, and has a similar \caption command. Also, the standard \listoftables command lists tables produced by either the table or longtable environments.

That means that for most intents and purposes longtable has its own table environment already built in.

If a longtable is nested in a table the table counter is stepped up twice (and maybe other even worse things happen...).

\documentclass{report}
\usepackage{longtable}
\begin{document}
See \ref{tab:results}~tab and \ref{tab:moreresults}~tab.

\begin{longtable}{lr}
\caption{Results}\label{tab:results}\\*
A & B \\
\end{longtable}

\begin{longtable}{lr}
\caption{More results}\label{tab:moreresults}\\*
C & D \\
\end{longtable}
\end{document}

should do the right thing.

The screenshot shows two tables "Table 1" and "Table 2"

That means that longtables can't easily float, but that is probably for the better.

Note the \\* (or \\) after the \caption, which are usually not needed in a table. See also how to have a caption on top of longtable?.

edit Changed to \\* after \caption in light of page split between longtable caption and table

moewe
  • 175,683