# table number is off in cross-references

I have a longtable and a regular table that were generated by a statistics package. I am inserting then into an article like this:

\input{../Tables/sum_stats.tex}
\label{sum_stats}


and then referring to it with:

Table \ref{sum_stats} displays summary statistics for our sample.


While all the tables are numbered correctly, the numbering in the references is off by 3, so table 1 becomes table 4 in the text. I tried resetting the table counter with \setcounter{table}{0} after \begin{document}, but that did not fix the problem.

The tables are all:

\begin{table}[htbp]\centering
\def\sym#1{\ifmmode^{#1}\else$$^{#1}$$\fi}
\caption{XXXX}
\begin{tabular}{l*{10}{c}}
\hline\hline
&\multicolumn{1}{c}{(1)}&\multicolumn{1}{c}{(2)}&\multicolumn{1}{c}{(3)}&\multicolumn{1}{c}{(4)}&\multicolumn{1}{c}{(5)}&\multicolumn{1}{c}{(6)}&\multicolumn{1}{c}{(7)}&\multicolumn{1}{c}{(8)}&\multicolumn{1}{c}{(9)}&\multicolumn{1}{c}{(10)}\\
&\multicolumn{1}{c}{AME}&\multicolumn{1}{c}{Contrast}&\multicolumn{1}{c}{AME}&\multicolumn{1}{c}{Contrast}&\multicolumn{1}{c}{AME}&\multicolumn{1}{c}{Contrast}&\multicolumn{1}{c}{AME}&\multicolumn{1}{c}{Contrast}&\multicolumn{1}{c}{AME}&\multicolumn{1}{c}{Contrast}\\
\hline
....
\hline\hline
\end{tabular}
\end{table}

• There is a caption inside the table. But it is made inside an environment (table), so the local value that captions prepares for the label to grab is, as Mico mentions lost. You will get whatever the value is outside the table environment. Always keep your labels close to what they are referring to. – daleif Jan 8 '15 at 20:34

If your external files all have the same form, just add them a command

\begin{table}[htbp]\centering
\def\sym#1{\ifmmode^{#1}\else$$^{#1}$$\fi}
\caption{XXXX}\label{\thistablelabel}% <------------
\begin{tabular}{l*{10}{c}}
\hline\hline
&\multicolumn{1}{c}{(1)}&\multicolumn{1}{c}{(2)}&\multicolumn{1}{c}{(3)}&\multicolumn{1}{c}{(4)}&\multicolumn{1}{c}{(5)}&\multicolumn{1}{c}{(6)}&\multicolumn{1}{c}{(7)}&\multicolumn{1}{c}{(8)}&\multicolumn{1}{c}{(9)}&\multicolumn{1}{c}{(10)}\\
&\multicolumn{1}{c}{AME}&\multicolumn{1}{c}{Contrast}&\multicolumn{1}{c}{AME}&\multicolumn{1}{c}{Contrast}&\multicolumn{1}{c}{AME}&\multicolumn{1}{c}{Contrast}&\multicolumn{1}{c}{AME}&\multicolumn{1}{c}{Contrast}&\multicolumn{1}{c}{AME}&\multicolumn{1}{c}{Contrast}\\
\hline
....
\hline\hline
\end{tabular}
\end{table}


and define in your document preamble

\newcommand{\tableinput}[2]{%
\def\thistablelabel{#2}\input{#1}%
}


Then call

\tableinput{../Tables/sum_stats.tex}{sum_stats}


instead of using \input{../Tables/sum_stats.tex}\label{sum_stats} which is incorrect because the label is set after the table environment has been closed.

The suggested changes will set the label at the proper place.