1

This question already has an answer here:

I have a big problem with referencing my tables. Below is how I do it. The pdf seems to know which table I refer to (the reference links me to the right table), but instead of a number it writes "??".

Table \ref{medaltable} summarizes the results

\begin{table}
\label{medaltable}
\caption{The summary of the performance of the algorithms in terms of the number of    nodes activated during the information diffusion}
\center
\begin{tabular}{ | l | c | c | c | c | }
\toprule
\textit{algorithm} & \textit{first} & \textit{second} & \textit{third} & \textit{total}     \\ \hline
GR & 4 & 4 & 2 & 22  \\
SHALG & 4  & 3 & 4 & 22  \\ 
CUST2 & 4 & 4 & 1 & 21 \\ 
\bottomrule
\end{tabular}
\end{table}

marked as duplicate by egreg, Werner, jub0bs, Stefan Kottwitz Sep 5 '13 at 20:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    have you compiled your source file more than once? latex puts the value for the \label into the .aux file in the first pass, and reads it back in (and outputs it) in the second pass. also, \label must be input after \caption, since it picks up the value of the last referenceable object, and that (for a table or figure) is the \caption. so in this example, the cross-reference won't be the table, but whatever might have been numbered previously, like a \section. – barbara beeton Sep 5 '13 at 18:18
  • 1
    This is a very often asked question: the \label command must go after the \caption. Don't use \center, but rather \centering. And don't add vertical rules in tables, if you're going to use \toprule and friends; better yet, never use vertical rules in tables. – egreg Sep 5 '13 at 19:31
2

You need to put the \label after the \caption because \label actually references the last modified counter and \caption steps the counter. So if you issue \label before \caption the counter that was modified before the table is referenced.

I have also taken the liberty of modifying your table a little bit using booktabs: it is recommended not to use vertical lines, I have also replaced \hline by \midrule (compare the spacing around the rule), finally I used \emph instead of \textit (but that is really a matter of taste: I assumed you wanted to emphasise the heads).

\documentclass{article}
\usepackage{booktabs}
\begin{document}
  \begin{table}
    \centering
    \begin{tabular}{ l c c c c }
      \toprule
      \emph{algorithm} & \emph{first} & \emph{second} & \emph{third} & \emph{total}\\
      \midrule
      GR & 4 & 4 & 2 & 22\\
      SHALG & 4  & 3 & 4 & 22\\ 
      CUST2 & 4 & 4 & 1 & 21\\ 
      \bottomrule
    \end{tabular}
    \caption{The summary of the performance of the algorithms in terms of the number of nodes activated during the information diffusion}
    \label{tab:medaltable}
  \end{table}
  Lets \verb|\ref| the table: \ref{tab:medaltable}.
\end{document}

enter image description here

If you want the reference to jump to the beginning of the table, not the caption itself, add \usepackage[all]{hyperref} to the preamble (be aware that hypcap might conflict with packages redefining \caption macro).

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