3

I've encountered something weird while using the longtable package together with \numberwithin{table}{section} or \counterwithin{table}{section}: if a longtable is the first table in a section, it will not be referenced correctly (with \ref{}); instead the reference will point to the last first-in-a-section longtable in the document. Note that the error is carried forward to other packages which rely on longtable such as tabu (longtabu).

While searching for a solution, I found duplicate entries for the first-in-a-section longtables in the aux-file, which is why I am suspecting this to be a bug. However, my LaTeX knowledge is limited and I am happy to be proven wrong.

Here's my MWE:

\documentclass{article}
\usepackage{amsmath} 
\numberwithin{table}{section}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage[hidelinks]{hyperref}

\begin{document}
Reference to Table \ref{tab:Label1}\\
Reference to Table \ref{tab:Label2}\\

\clearpage
\section{}

\begin{longtable}{ll}
\caption{Caption.}
\label{tab:Label1}\\
\toprule 
\multicolumn{2}{l}{\textbf{Header}}  \\
\midrule 
\endfirsthead
\multicolumn{2}{c}{\begin{footnotesize}\tablename\ \thetable\ -- \textit{Continued from previous page}\end{footnotesize}} \\
\toprule
\multicolumn{2}{l}{\textbf{Header}}  \\
\midrule
\endhead
\bottomrule
\multicolumn{2}{r}{\begin{footnotesize}\textit{Continued on next page}\end{footnotesize}} \\
\endfoot
\bottomrule
\endlastfoot
some variable & some explaination\\
\midrule
some variable & some explaination \\
\end{longtable}

\clearpage
\section{}

\begin{longtable}{ll}
\caption{Caption.}
\label{tab:Label2}\\
\toprule 
\multicolumn{2}{l}{\textbf{Header}}  \\
\midrule 
\endfirsthead
\multicolumn{2}{c}{\begin{footnotesize}\tablename\ \thetable\ -- \textit{Continued from previous page}\end{footnotesize}} \\
\toprule
\multicolumn{2}{l}{\textbf{Header}}  \\
\midrule
\endhead
\bottomrule
\multicolumn{2}{r}{\begin{footnotesize}\textit{Continued on next page}\end{footnotesize}} \\
\endfoot
\bottomrule
\endlastfoot
some variable & some explanation\\
\midrule
some variable & some explanation \\
\end{longtable}

\end{document}

On my computer, both references point to the latter table.

  • Do your computers replicate the error?
  • Does anybody have an idea what could cause this error?
  • Would anybody know a workaround?
3

As a general rule, settings should be made after loading packages (there are some exceptions); this is a case: since hyperref modifies the macros that manage counters, your \numberwithin given before the package is loaded leads to the issue.

\documentclass{article}
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage[hidelinks]{hyperref}

\numberwithin{table}{section}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.