[Tex/LaTex] Question marks in pdf file when referring to table

cross-referencingtables

Have a look at the MWE below. In the pdf file I get questionmarks when reffering to the table, rather than a numeric reference. Even if I run LaTeX twice. Any ideas how this can be solved?

\documentclass{report} 
\usepackage{float}
\begin{document}

we refer to table \ref{tab:ex}

\begin{table}[h]\label{tab:ex}
\centering
\begin{tabular}{|l|l|l|l|l|}
\hline
9  & 12 & 8 & 10 & 9           \\ \hline
10 & 7  & 8 & 13 & \textbf{50} \\ \hline
\end{tabular}
\end{table}

\end{document}

Best Answer

You missed a \caption in your table. LaTeX uses the \caption macro to number your tables thus without a caption you won't get a number and thus cannot reference the table.

Try this:

\documentclass{report} 
\usepackage{float}
\begin{document}

we refer to table \ref{tab:ex}

\begin{table}[h]
\caption{Example of a simple table.}
\label{tab:ex}
\centering
\begin{tabular}{|l|l|l|l|l|}
\hline
9  & 12 & 8 & 10 & 9           \\ \hline
10 & 7  & 8 & 13 & \textbf{50} \\ \hline
\end{tabular}
\end{table}

\end{document}

A few things to remember:

  • Typically table-captions go above the table, figure captions go below the figure (at least in european typography)
  • The \label has to appear after the \caption, because \caption creates the number the \label can then refer to.
  • If you do not want a caption you won't get a number. But it's wise to always provide a short caption at least.
  • There are means to get Captions outside of table or figure environments with KOMA-Script or the caption package