2

I have a bunch of algorithms which I need to explain by referring to certain line numbers in them. I'm trying to use \crefrange for this, but the simplest example seems to fail.

In the example below, on the 2nd page, I refer to line range 1-3 in the 2nd algorithm using \crefrange. Unfortunately, in the resulting PDF, when I click on "Lines 1-3," the link takes me to the right line but in the wrong algorithm (i.e., algorithm one, not two.)

\documentclass{article}
\usepackage{algorithm,algpseudocode}
\usepackage[colorlinks]{hyperref}
\usepackage[noabbrev,capitalize,nameinlink]{cleveref}
\begin{document}

\section{Page 1}
\cref{a:one:line2} in \cref{a:one} is \dots

\cref{a:one:line2,a:one:line3} of the algorithm are based on \dots
\begin{algorithm}
\caption{Baseline}\label{a:one}
\begin{algorithmic}[1]
  \State Do X 
  \State Do Y        \label{a:one:line2}
  \State $x = y + z$ \label{a:one:line3}
\end{algorithmic}
\end{algorithm}

\pagebreak

\section{Page 2}
\cref{a:two:line2} in \cref{a:two} is \dots

\crefrange{a:two:line1}{a:two:line3} of the algorithm are based on \dots
\begin{algorithm}
    \caption{Baseline}\label{a:two}
    \begin{algorithmic}[1]
        \State Do X        \label{a:two:line1}
        \State Do Y        \label{a:two:line2}
        \State $x = y + z$ \label{a:two:line3}
    \end{algorithmic}
\end{algorithm}
\end{document}

I'm using TexStudio with pdflatex on Ubuntu, if that matters.

Mico
  • 506,678
  • You also get the warning l.30 \State Do Y \label{a:two:line2}pdfTeX warning (ext4): desti nation with the same identifier (name{ALG@line.3}) has been already used, dupli cate ignored, right? –  Apr 03 '18 at 20:36
  • I can confirm that the issue arises not only with \crefrange but with \cref as well. – Mico Apr 03 '18 at 20:39
  • Yes, getting that warning and similar ones for ALG@line.2, ALG@line.1. – Alin Tomescu Apr 03 '18 at 20:46
  • algorithm is not among the supported packages, see section 13 of the cleveref package. –  Apr 03 '18 at 20:47
  • Page 30 here (http://tug.ctan.org/tex-archive/macros/latex/contrib/cleveref/cleveref.pdf) says "Added support for algorithm package". And page 24 says algorithmic is not supported, which I'm not using am I? I am only doing \usepackage{algorithm,algpseudocode} – Alin Tomescu Apr 03 '18 at 20:54
  • 1
    It lists the supported packages and also explicitly mentions some that are not supported, but this does not mean that all the other packages are supported. –  Apr 03 '18 at 22:00
  • Looks like I'll have to rewrite my algorithms into something supported by not-so-cleverref :( – Alin Tomescu Apr 04 '18 at 03:30
  • @Alin Tomescu Your comment is clever, but misguided ;-) – Toby Cubitt Apr 04 '18 at 20:40

1 Answers1

6

When using cleveref together with hyperref, if you see the "destination with the same identifier has already been used, duplicate ignored" warning in the log, a work-around that often works is to disable hypertexnames in hyperref:

\usepackage[hypertexnames=false]{hyperref}

Setting this option in your MWE produces correct hyperlinks for me.

Most instances of this issue were fixed in cleveref some years ago, and this work-around shouldn't be necessary. The algorithm, algorithmicx and algorithm2e packages are all supported by cleveref (see below for more details). Looks like there's a bad interaction between cleveref's algorithmicx and hyperref support, which needs fixing in cleveref.

The algorithm package just provides a separate floating environment for algorithms. The only support required is to provide a default name (and translations thereof) for these algorithm environments, which cleveref does. The algorithmicx package provides a framework for typesetting the actual algorithms. You usually load a companion layout package that styles the algorithms for you (which internally loads the algorithmicx package), rather than loading the algorithmicx package itself. algpseudocode is the most commonly used of these algorithmicx layout packages. cleveref supports the algorithmicx framework, hence all its layout packages like algpseudocode. The algorithmicx package is by-and-large a better drop-in replacement for algorithmic package (which is why the latter isn't supported by cleveref). The algorithm2e package is an alternative to algorithmicx. See this stackexchange answer for a more complete explanation.

Toby Cubitt
  • 2,072
  • Yes, this seems to fix the minimal working example, but in my project I use acmart.cls which imports hyperref with different options so I get an Option clash for package hyperref error. But I will figure out a way to fix... Thank you! – Alin Tomescu Apr 05 '18 at 19:38
  • Yes, a \PassOptionsToPackage{hypertexnames=false}{hyperref} before the \documentclass[sigconf]{acmart} did it. Thank you again! :) – Alin Tomescu Apr 05 '18 at 19:41