Create a smart cross-reference to a line in an 'algorithmic' environment using \autoref

To set the name of the \autoref command for an algorithm I use:

\newcommand{\algorithmautorefname}{Algorithm}


like commented here. I would also like to be able to refer to lines within an algorithmic environment with line x.

\begin{algorithm}
\label{alg:myalg}
\begin{algorithmic}[1]
\State Do X
\State Do Y \label{algl:y}
\State $x = y + z$
\end{algorithmic}
\end{algorithm}


\autoref{alg:myalg} gives me correctly Algorithm 1. In addition, I would also like to have \autoref{algl:y} generate line 2. Until now I'm using: line~\ref{algl:y}.

Seeing the same answer you can define the autoref names by: \....autorefname. But then he mentions that some counters are hidden, for example the line numbering: \c@ALG@line. Is it possible to create an autoref name for them?

I have tried:

\newcommand{\ALGlineautorefname}{line}
\newcommand{\ALG_lineautorefname}{line}
\newcommand{\ALG@lineautorefname}{line}


but none work and only the first compiles.

If possible, how can I create an \autorefname for a line in an algorithmic environment.

The line counter is hidden from hyperref's \autoref because the counter is stepped using \addtocounter{ALG@line}{1} instead of \refstepcounter{ALG@line}. The following patch corrects this, allowing you to define \ALG@lineautorefname:

\documentclass{article}

\usepackage{algorithm,algpseudocode}
\usepackage{hyperref,etoolbox}

\makeatletter
\newcommand{\ALG@lineautorefname}{Line}
\makeatother

\newcommand{\algorithmautorefname}{Algorithm}

\begin{document}

See \autoref{alg:myalg}, specifically \autoref{algl:y}.

\begin{algorithm}
\caption{An algorithm}\label{alg:myalg}
\begin{algorithmic}[1]
\State Do X
\State Do Y \label{algl:y}
\State $x = y + z$
\end{algorithmic}
\end{algorithm}

\end{document}

• +1. What's nice about this solution is that it doesn't interfere with cleveref's own patch of the counter-incrementing setting of the algorithmic environment. That way, should the OP or some future readers of this posting decide to use the cleveref package as well, they won't need to undo the patch shown above. – Mico Jan 30 '17 at 18:08
• Thanks, it seems to work well, but with pdflatex it refers to the lines of the first algorithm (if there are more). – agold Jan 31 '17 at 8:03
• I'm using algorithm2e and this patch doesn't seem to work - any ideas what changes might be required to adjust the \autoref text for a line in an algorithm2e block? – aaronsnoswell Jan 15 at 12:54
• @aaronsnoswell: Try this code. \autoref-ing a line number uses \AlgoLineautorefname. – Werner Jan 15 at 17:06
• @Werner amazing thank you! – aaronsnoswell Jan 16 at 2:01

The following is a cleveref-based solution. Note: \cref (the main user macro of the cleveref package) works very much like \autoref, except that it's actually far more powerful and flexible than \autoref. For instance, \cref can take multiple arguments; see the code below for an example. Further good news: cleveref is programmed "out of the box" to know that lines in an algorithmic environment should be called, well, "lines" -- no need to provide this setup yourself.

\documentclass{article}
\usepackage{algorithm,algpseudocode}
\begin{document}

\begin{algorithm}
\caption{Baseline}\label{ALG_baseline}
\begin{algorithmic}[1]
\State Do X
\State Do Y        \label{algl:y}
\State $x = y + z$ \label{algl:sum}
\end{algorithmic}
\end{algorithm}
\cref{algl:y} in \cref{ALG_baseline} is \dots

\cref{algl:y,algl:sum} of the algorithm are based on \dots
\end{document}

• Thanks, I did not know about this packages it sounds even better than autoref. However, when I tried it it actually referred to a line of another algorithm, but I'll try to solve that later. – agold Jan 31 '17 at 7:19
• @agold - Could you provide a bit more information about the the cross-reference that's going astray? E.g., what do the \label instructions look like for the two lines? – Mico Jan 31 '17 at 8:58
• Actually I found that it was due to the use of pdflatex (like with the solution of Werner), with latex it works correctly. – agold Jan 31 '17 at 9:01
• @agold - Hmm, not sure I understood what you just wrote. pdflatex, xelatex, and lualatex are the combinations of the LaTeX format and the pdftex, xetex, and luatex engines, respectively. What exactly do you run when you run "latex"? – Mico Jan 31 '17 at 9:19
• sorry for the confusion; I mean that compiling with latex and then doing dvipdf the references are correct, however when I compile with pdflatex I get the problem of all lines referring to the first algorithm. – agold Jan 31 '17 at 9:22