2

The code below shows in the caption "table 1", which is correct, but in the text reference, it shows "table 2". How can I fix that?

\documentclass[]{article}

\begin{document}
\section{Description}
\section{Measurements}
\label{sec:measx}
Table \ref{tab:meas}
\begin{table}[ht]
\label{tab:meas}
\begin{tabular}{|c|c|}
\hline
A   & B \\ \hline
\end{tabular}
\caption {Measurements}
\end{table}

\end{document}

enter image description here

Fabio
  • 343
  • 4
    You need to put \label in or after \caption. –  Aug 24 '18 at 04:15
  • Moving \label to the line after \caption makes no difference. Moving \label inside \caption{\label{} Measurements} does not make any difference either. Removing the previous \section works. It seems to mixup \section and \table enumerations. – Fabio Aug 24 '18 at 04:23
  • 1
    you have to compile two times until the label will be correct. –  Aug 24 '18 at 04:35
  • Yes, I am aware two compilations are needed. It does not work. Tried both on overleaf.com| and cygwin latex. https://www.overleaf.com/18835735vkgdyqsjfwdk – Fabio Aug 24 '18 at 06:02
  • 1
    I was moving below \caption the wrong label (the \label{sec:measx} one). It work, thanks. – Fabio Aug 24 '18 at 06:13

2 Answers2

3

Too long for a comment. If I compile

\documentclass[]{article}

\begin{document}
\section{Description}
\section{Measurements}
\label{sec:measx}
Table \ref{tab:meas}
\begin{table}[ht]

\begin{tabular}{|c|c|}
\hline
A   & B \\ \hline
\end{tabular}
\caption {Measurements\label{tab:meas}}
\end{table}

\end{document}

on my TeXLive2018 distribution, I get

enter image description here

If you do not get that result, you have either not compiled twice or there is something really wrong with your TeX installation.

3

you also got the same result as in marmot answer with the following mwe (note, table label is after table caption):

\documentclass{article}

\begin{document}
\section{Description}
\section{Measurements}
\label{sec:measx}

Table \ref{tab:meas}

\begin{table}[ht]
\begin{tabular}{|c|c|}
\hline
A   & B \\ \hline
\end{tabular}
\caption {Measurements}
\label{tab:meas} % <---
\end{table}

\end{document}

tested in overleaf:

enter image description here

Zarko
  • 296,517