11

if I have this code

\begin{algorithm}
  \label{alg:the_alg}
  \begin{algorithmic}[1]
    \STATE operation 0
    \STATE operation 1
  \end{algorithmic}
\end{algorithm}

I can make a reference to alg:the_alg. But, how I can make a reference to a STATE in algorithmic ?

12

You would use the same technique as you would with a regular label. The reference will be to the line number inside algorithmic:

enter image description here

\documentclass{article}
\usepackage{algorithm,algorithmic}% http://ctan.org/pkg/algorithms
\begin{document}
\begin{algorithm}
  \caption{This is an algorithm}
  \label{alg:the_alg}
  \begin{algorithmic}[1]
    \STATE operation 0 \label{op0}
    \STATE operation 1 \label{op1}
  \end{algorithmic}
\end{algorithm}

See Algorithm~\ref{alg:the_alg}. More specifically, Operation~\ref{op1}.
\end{document}

Consider using the compatible (modernized) algorithmicx bundle which offers algpseudocode. Here's a duplicate of the above MWE, now with the updated algpseudocode package:

\documentclass{article}
\usepackage{algorithm,algpseudocode}
\begin{document}
\begin{algorithm}
  \caption{This is an algorithm}
  \label{alg:the_alg}
  \begin{algorithmic}[1]
    \State operation 0 \label{op0}
    \State operation 1 \label{op1}
  \end{algorithmic}
\end{algorithm}

See Algorithm~\ref{alg:the_alg}. More specifically, Operation~\ref{op1}.
\end{document}
  • in references, why some people use ~, example: ~\ref and not a whitespace \ref ? – JuanPablo Jul 7 '12 at 22:40
  • 9
    @JuanPablo: To avoid a line break between the two since they're considered a "single unit". See Why should I put a ~ before \ref or \cite? – Werner Jul 7 '12 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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