# Problem with cross references in table and algorithm

I am having a problem with cross references, which I have not been able to solve. I have a table and an algorithm which cannot be referenced. However, the behaviour in both cases is different: when I try to reference the table, LaTex produces ??, whereas in the case of the algorithm, a blank space is produced.

I am including the LaTeX document here. Thank you very much in advance for your help.

===========================================

\documentclass[11pt,a4paper]{article}

\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{amsmath}
\setcounter{tocdepth}{3}
\usepackage{latexsym,epsfig}
\usepackage{graphicx} %To include graphics
\usepackage[usenames,dvipsnames]{color} %To use color
\usepackage[hyphens]{url} %To use links. Option 'hyphens' is good for breaking urls.
\usepackage{enumerate} %To choose enumerator symbols
\usepackage{soul} %To use highlighting of the text
\usepackage{array} %To use arrays
\usepackage{verbatim}
\usepackage[hang,small,bf]{caption}
\usepackage[ruled,linesnumbered]{algorithm2e}  %To create nice algorithms - must be loaded *after* natbib
\usepackage{hyperref}

\title{Test}
\author{Moondog }
\begin{document}
\maketitle

%%===============================%%

\section{Introduction}
\label{sec:intro}

The process is best described by Algoritm \label{algo:iterated}.

% -  ALGORITHM --

\begin{algorithm}[ht]
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
%\linesnumbered
%\dontprintsemicolon
%\SetLine
\vspace{.2cm}
\Input{A matrix}
\Output{A set of matrices}
\For{$r$:=$1$ \KwTo $N$}{
$B$ := $C$ \;
}  % End of For loop

\caption{Algorithm}
\label{algo:iterated}
\end{algorithm}
%-------------------

This process is described schematically in Table~\ref{tab:schematic1}.

\begin{table}[ht]
\label{tab:schematic1}
\centering
\begin{tabular}{|lcr|}
\hline
$\mathbf{Q}^{(1)}_{0,0}$ & $\mathbf{Q}^{(1)}_{1,0}$ & $\mathbf{Q}^{(1)}_{2,0}$ \\
$\mathbf{Q}^{(1)}_{0,1}$ & $\mathbf{Q}^{(1)}_{1,1}$ & $\mathbf{Q}^{(1)}_{2,1}$ \\
$\mathbf{Q}^{(1)}_{0,2}$ & $\mathbf{Q}^{(1)}_{1,2}$ & $\mathbf{Q}^{(1)}_{2,2}$ \\
\hline
\end{tabular}
\caption{Schema}
\end{table}

\begin{thebibliography}{4}
\bibitem{M01} Meyer, C.D.: Matrix Analysis and Applied Linear Algebra. SIAM (2001).
\end{thebibliography}
\end{document}

• \label{tab:schematic1} must appear after \caption{Schema}. – user31729 Nov 28 '15 at 13:20
• Thanks a lot. I think I had heard it before, but I must have forgotten. – Manolito Pérez Nov 28 '15 at 13:36

The references are generated using counter values, those are written by \label to the .aux file, but in order to get the right label the \label command must be done after \caption, since that macro does use \refstepcounter, which provides the correct information.

In addition there was \label{algo:iterated} twice, the first occurence should be a \ref{algo:iterated}, in my point of view.

\documentclass[11pt,a4paper]{article}

\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{amsmath}
\setcounter{tocdepth}{3}
\usepackage{latexsym,epsfig}
\usepackage{graphicx} %To include graphics
\usepackage[usenames,dvipsnames]{color} %To use color
\usepackage[hyphens]{url} %To use links. Option 'hyphens' is good for breaking urls.
\usepackage{enumerate} %To choose enumerator symbols
\usepackage{soul} %To use highlighting of the text
\usepackage{array} %To use arrays
\usepackage{verbatim}
\usepackage[hang,small,bf]{caption}
\usepackage[ruled,linesnumbered]{algorithm2e}  %To create nice algorithms - must be loaded *after* natbib
\usepackage{hyperref}

\title{Test}
\author{Moondog }
\begin{document}
\maketitle

%%===============================%%

\section{Introduction}
\label{sec:intro}

The process is best described by Algoritm \ref{algo:iterated}.

% -  ALGORITHM --

\begin{algorithm}[ht]
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
%\linesnumbered
%\dontprintsemicolon
%\SetLine
\vspace{.2cm}
\Input{A matrix}
\Output{A set of matrices}
\For{$r$:=$1$ \KwTo $N$}{
$B$ := $C$ \;
}  % End of For loop

\caption{Algorithm}
\label{algo:iterated}
\end{algorithm}
%-------------------

This process is described schematically in Table~\ref{tab:schematic1}.

\begin{table}[ht]
\centering
\begin{tabular}{|lcr|}
\hline
$\mathbf{Q}^{(1)}_{0,0}$ & $\mathbf{Q}^{(1)}_{1,0}$ & $\mathbf{Q}^{(1)}_{2,0}$ \\
$\mathbf{Q}^{(1)}_{0,1}$ & $\mathbf{Q}^{(1)}_{1,1}$ & $\mathbf{Q}^{(1)}_{2,1}$ \\
$\mathbf{Q}^{(1)}_{0,2}$ & $\mathbf{Q}^{(1)}_{1,2}$ & $\mathbf{Q}^{(1)}_{2,2}$ \\
\hline
\end{tabular}
\caption{Schema}
\label{tab:schematic1}
\end{table}

\begin{thebibliography}{4}
\bibitem{M01} Meyer, C.D.: Matrix Analysis and Applied Linear Algebra. SIAM (2001).
\end{thebibliography}
\end{document}