Wrong number when reference inside algorithm

1,072

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

Share:
1,072

Related videos on Youtube

user96784
Author by

user96784

Updated on January 27, 2020

Comments

  • user96784
    user96784 almost 4 years
    \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)

    • Mensch
      Mensch almost 8 years
      Please make your code snippet compilable! Welcome to TeX.SE!
  • user96784
    user96784 almost 8 years
    In a new latex file it works for me, but not in the original one. Any idea why ??
  • Romain Picot
    Romain Picot almost 8 years
    @user96784 You may have used the same label twice for example. You can try to remove the aux too
  • user96784
    user96784 almost 8 years
    No, also tried to remove aux, still the same problem.
  • Romain Picot
    Romain Picot almost 8 years
    @user96784 well, without a proper MWE it will be hard to answer the question then