1

I have multiple algorithms and I have changed counter in between. When I am cross-referencing a later algorithm, it gets linked to the original sequence instead of the actual algorithm. For example, suppose I have this code,

\usepackage[ruled,linesnumbered]{algorithm2e}
\begin{document}
...
\begin(algorithm)
   \caption{\label{algo1}}
\end{algorithm}
\setcounter{algocf}{0}
\begin(algorithm)
   \caption{\label{algo2}}
\end{algorithm}
...
\end{document}

When I am trying to reference Algorithm 2 (\ref{algo2}), then the hyperlink generated in the pdf jumps to algo1 instead of algo2.

Any ideas, why??

How do I update the counter so that this problem does not arise?? Thanks for any help.

0

1 Answer 1

1

With this approach you should notice the following in your .log:

LaTeX Warning: Reference `algo1' on page 1 undefined on input line 8.

[1

{c:/texlive/2021/texmf-var/fonts/map/pdftex/updmap/pdftex.map}pdfTeX warning (e
xt4): destination with the same identifier (name{algocf.1}) has been already us
ed, duplicate ignored
<argument> ...shipout:D \box_use:N \l_shipout_box 
                                                  \__shipout_drop_firstpage_...
l.19 \end{document}

This results from hyperref needing a unique identifier to jump to, yet you've made the identifier the same due to resetting (or stepping back) the algocf counter (the algorithm2e counter identifying the algorithm).

One possible reason for this is because you want to split an algorithm across multiple pages yet still number them the same. Or, possibly, provide an updated version (improvement, maybe) of the previous algorithm.

To achieve this properly, you need to load hyperref with the hypertexnames=false option:

enter image description here

\documentclass{article}

\usepackage[ruled,linesnumbered]{algorithm2e}
\usepackage[hypertexnames=false]{hyperref}

\begin{document}

See Algorithm~\ref{alg:algo1} and~\ref{alg:algo2}. There is also Algorithm~\ref{alg:algo3}.

\begin{algorithm}
  \caption{First algorithm}
  \label{alg:algo1}
\end{algorithm}

\begin{algorithm}
  \setcounter{algocf}{0}% Reset algorithm counter (algocf)
  \caption{Second algorithm}
  \label{alg:algo2}
\end{algorithm}

\begin{algorithm}
  \caption{Third algorithm}
  \label{alg:algo3}
\end{algorithm}

\end{document}

You must log in to answer this question.

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