Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 ?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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}% http://ctan.org/pkg/algorithms
\usepackage{algorithmic}% http://ctan.org/pkg/algorithmic
\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 from algorithmicx:

\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\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}
share|improve this answer
    
in references, why some people use ~, example: ~\ref and not a whitespace \ref ? –  JuanPablo Jul 7 '12 at 22:40
3  
@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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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