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.

I am using the algorithmic package to write algorithms. I want to have a link to the line of the algorithm like I can do to formulas using \label and \ref.

share|improve this question
add comment

1 Answer

You can number the lines and then use the standard \label, \ref mechanism:

\documentclass{article}
\usepackage{algorithm}
\usepackage{algorithmic}

\begin{document}

As we can see in line~\ref{alg:line} of Algorithm~\ref{alg:test}...

\begin{algorithm}
\begin{algorithmic}[1]
\STATE $S \leftarrow 0$
\STATE $T \leftarrow 1$\label{alg:line}
\STATE $U \leftarrow 2$
\end{algorithmic}
\caption{A test algorithm}
\label{alg:test}
\end{algorithm}

\end{document}

enter image description here

When the hyperref package is loaded, you will get hyperlinks. If you don't want to number the lines of the algorithm, but you want to generate a hyperlink to some particular line(s), then you can use the \hypertarget, \hyperlink mechanism provided by the hyperref package:

\documentclass{article}
\usepackage{algorithm}
\usepackage{algorithmic}
\usepackage{hyperref}

\begin{document}

As we can see in \hyperlink{alg:line}{the second line} of Algorithm~\ref{alg:test}...

\begin{algorithm}
\begin{algorithmic}
\STATE $S \leftarrow 0$
\hypertarget{alg:line}{\STATE $T \leftarrow 1$}
\STATE $U \leftarrow 2$
\end{algorithmic}
\caption{A test algorithm}
\label{alg:test}
\end{algorithm}

\end{document}
share|improve this answer
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.