18

I get the warning

pdfTeX warning (ext4): destination with the same identifier 
(name{[email protected]}) has been already used, duplicate ignored

with the MWE down below.

\documentclass{article}

\usepackage{algpseudocode}
\usepackage{hyperref}
\usepackage{cleveref}

\begin{document}
\begin{algorithmic}
\Procedure{Euclid}{$a,b$}
\EndProcedure
\end{algorithmic}

\begin{algorithmic}
\Procedure{Euclid}{$a,b$}
\EndProcedure
\end{algorithmic}
\end{document}

Is there a chance to fix that?

3 Answers 3

18

You should change the load order (pushing algpseudocode after hyperref) and redefine the way hyperref looks at destinations for lines within algorithmic:

\documentclass{article}

\usepackage{hyperref}
\usepackage{algpseudocode}
\usepackage{cleveref}

\makeatletter
\newcounter{algorithmicH}% New algorithmic-like hyperref counter
\let\oldalgorithmic\algorithmic
\renewcommand{\algorithmic}{%
  \stepcounter{algorithmicH}% Step counter
  \oldalgorithmic}% Do what was always done with algorithmic environment
\renewcommand{\theHALG@line}{ALG@line.\thealgorithmicH.\arabic{ALG@line}}
\makeatother

\begin{document}
\begin{algorithmic}
\Procedure{Euclid}{$a,b$}
\EndProcedure
\end{algorithmic}

\begin{algorithmic}
\Procedure{Euclid}{$a,b$}
\EndProcedure
\end{algorithmic}
\end{document}

Above I've made each line be referenced using ALG@line.<algnum>.<linenum> where I've inserted <algnum>. as a newly made counter (incremented at every use of algorithmic). This makes line number X in one algorithm distinguishable from the same line number X in another.

Not sure whether this affects algorithms that are saved and restored.

3

With more modern methods:

\documentclass{article}

\usepackage{algpseudocode}
\usepackage{hyperref}
\usepackage{cleveref}

% fix anchor names for algorithm lines
\newcounter{Halgorithmic}
\AtBeginEnvironment{algorithmic}{\stepcounter{Halgorithmic}}
\ExpandArgs{c}\newcommand{theHALG@line}{\arabic{Halgorithmic}.\arabic{ALG@line}}

\begin{document}

\begin{algorithmic}
\Procedure{Euclid}{$a,b$}
\EndProcedure
\end{algorithmic}

\begin{algorithmic}
\Procedure{Euclid}{$a,b$}
\EndProcedure
\end{algorithmic}

\end{document}

We inject code at the beginning of algoritmic, namely to step a dummy counter Halgorithmic and define \theHALG@line to yield

<current value of Halgorithmic>.<current value of ALG@line>

using the (documented) fact that hyperref uses, for its anchors, \theH<name> whenever it's defined, for every counter <name>.

The construction

\ExpandArgs{c}\newcommand{theHALG@line}{...}

is the same as

\expandafter\newcommand\csname theHALG@line\endcsname{...}

but it's definitely simpler (c stands for control sequence name).

3
+50

This can be fixed by adding the hypertexnames=false option to hyperref.

\usepackage[hypertexnames=false]{hyperref}

See What does the hyperref option hypertexnames do? for why this works.

(Edit:) How this works

The issue here is that each line gets the link name ALG@line.<line number>. Hence, if there are two algorithms in the document, their corresponding lines would have the same link names.

One way of fixing this, as the other answers suggest, is to change the link name format to something like ALG@line.<algorithm name>.<line number>.

Another way of fixing this is to somehow not reset the <line number> component of the link name at the beginning of each algorithm. This can be done using hypertexnames=false. What does the hyperref option hypertexnames do? gives a good explanation of what this option does:

With the option hypertexnames hyperref uses the corresponding counter in addition to the link type to construct the link name. Imagine, you have two chapters and one section in each chapter plus one table. The link names would be:

With hypertexnames:

chapter.1
section.1.1
table.1.1
chapter.2
section.2.1

Without hypertexnames:

chapter.3
section.4
table.5
chapter.6
section.7
4
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – Mensch
    Jan 5, 2023 at 18:34
  • The link is not necessary for fixing the issue; it only gives an explanation for why it works. Hence, I thought it's okay to not import important parts of the link. Anyways, I have explained the link now. Jan 5, 2023 at 20:10
  • Beautiful!!! Thanks for the fix, I've been struggling with hacky solutions on this one for a while. Jan 12, 2023 at 20:16
  • P.S.: This works for algpseudocodex as well. Jan 12, 2023 at 20:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .