[Tex/LaTex] Incorrect reference to a line in algorithmic using hyperref

algorithmicxalgorithmscross-referencinghyperref

I am using algpseudocode and algorithm packages to typeset pseudocode. I also use float, hyperref and caption packages. The problem is that when I reference a line in the pseudocode, the number is correct, but when I click it, the document moves to the beginning of the chapter and not the algorithm.

Minimal example:

\documentclass{book}
\usepackage{blindtext}
\usepackage{float}
\usepackage{hyperref}
\usepackage[hypcap]{caption}
\usepackage{algpseudocode}
\usepackage[chapter]{algorithm}

\begin{document}
\chapter{Heading}
Reference to the line \ref{line}.
\Blindtext
\begin{algorithm}
\begin{algorithmic}[1]
    \State Statement \label{line}
\end{algorithmic}
\caption{Example}
\label{alg}
\end{algorithm}
\Blindtext
\end{document}

Best Answer

There are two ways to do this. As said, just loading a new version of cleveref partially fixes the problem, links are now to line numbers rather than the start of the chapter; but when there are two or more algorithms hyperref's link doesn't distinguish the same line number in different algorithms. The simplest solution is just to load hyperref via

\usepackage[hypertexnames=false]{hyperref}

This changes all link names in to something unique, but not humanly recognisable/meaningful. If you think this is a blunderbust approach, then you can just define \theHALG@line appropriately, to give unique links, via

\providecommand\theHALG@line{\thealgorithm.\arabic{ALG@line}}

In hyperref each \thecounter has a corresponding \theHcounter which can be redefined the create unique identifiers. In your case, algorithm provides a unique container for your algorithmic environment, and so its number representation can be used to distinguish different instances.

Because of the @ symbols in the command names above, you will have to put this code between \makeatletter and \makeatother:

\documentclass{book}
\usepackage{lipsum}
\usepackage{algpseudocode}
\usepackage{float}
\usepackage[chapter]{algorithm}
\usepackage[hypcap]{caption}
\usepackage{hyperref}
\usepackage{cleveref}

\makeatletter
\providecommand\theHALG@line{\thealgorithm.\arabic{ALG@line}}
\makeatother

\begin{document}
\chapter{Heading}

Here's the first test to line \ref{test1} of Algorithm \ref{alg1}.\\
Here's the second test to line \ref{test2} of Algorithm \ref{alg1}.
Here's the first test to line \ref{test21} of Algorithm \ref{alg2}.\\
Here's the second test to line \ref{test22} of Algorithm \ref{alg2}.

\begin{algorithm}[p]
\begin{algorithmic}[1]
  \State Statement
    \State Statement
    \State Statement    
    \State Statement    \label{test1}
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement    \label{test2}
    \State Statement
\end{algorithmic}
\caption{Example}
\label{alg1}
\end{algorithm}

\begin{algorithm}[p]
\begin{algorithmic}[1]
  \State Statement
    \State Statement
    \State Statement    
    \State Statement    \label{test21}
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement
    \State Statement    \label{test22}
    \State Statement
\end{algorithmic}
\caption{Example}
\label{alg2}
\end{algorithm}

\end{document}
Related Question