[Tex/LaTex] Wrong number when reference inside algorithm

algorithmicalgorithmscross-referencing

\usepackage{algorithm}
\usepackage{algorithmic}
\begin{algorithm}[tb]
    \caption{Algorithm}
    \label{alg}
    \begin{algorithmic}[1]
        \STATE blalblabla \label{cond1}
        \STATE blablabla \label{cond2}
        \STATE  return to step \ref{cond2}.
    \end{algorithmic}
\end{algorithm}

enter image description here

As you see it gives "return to step 1" (instead of 2)

Best Answer

If you use a label and a ref, you need to compile your document twice. Otherwise an error may occur.

\documentclass{article}

\usepackage{algorithm}
\usepackage{algorithmic}

\begin{document}

\begin{algorithm}[tb]
    \caption{Algorithm}
    \label{alg}
    \begin{algorithmic}[1]
        \STATE blalblabla \label{cond1}
        \STATE blablabla \label{cond2}
        \STATE  return to step \ref{cond2}.
    \end{algorithmic}
\end{algorithm}
\end{document}

Which gives when compilied twice:

enter image description here

Related Question