# Hyperref, cleveref and algpseudocode same identifier warning

I get the warning

pdfTeX warning (ext4): destination with the same identifier
(name{ALG@line.2}) 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?

## 1 Answer

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.