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 algorithmshyperref
's link doesn't distinguish the same line number in different algorithms. The simplest solution is just to loadhyperref
viaThis 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, viaIn
hyperref
each\thecounter
has a corresponding\theHcounter
which can be redefined the create unique identifiers. In your case,algorithm
provides a unique container for youralgorithmic
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
: