4

I'm having back reference issues when using an algorithm.

Consider the following example

\documentclass{article}

\usepackage[algo2e,linesnumbered,algonl]{algorithm2e}
\usepackage[backref=section]{hyperref}

\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@misc{myarticle,
    author  = {ABC},
    title   = {A Title},
    year    = {2023},
}
\end{filecontents}

\begin{document}

\section{Test}
\subsection{Test}
\subsubsection{Test}

% comment/uncomment the algorithm
\begin{algorithm2e}
$a=b$\;
$b=c$\;
$c=d$\;
\end{algorithm2e}

\paragraph{Test}

Citing~\cite{myarticle}.

\bibliographystyle{plain}
\bibliography{\jobname}

\end{document}

The back reference picks up the the last line number of the algorithm as the last label, apparently, instead of the last section. If the algorithm is commented out, I see the expected behavior, and the back reference refers to the section.

with the algorithm

enter image description here

without the algorithm:

enter image description here

Any ideas on a fix? It seems that \@currentlabel is overwritten in the algorithm. Can the section label be stored before the algorithm and restored after the algorithm?

1 Answer 1

5

backref=section doesn't actually pick up the section level. For instance, if you do

\newtheorem{theorem}{Theorem}

and

\begin{theorem}
Something with \cite{myarticle}
\end{theorem}

the back reference will show the theorem number (test it). What actually happens is that hyperref picks up the current value of \@currentlabel that's by no means guaranteed to be the current section number.

In common cases this seems to work, provided your \cite commands aren't in environments that do their own numbering (such as theorem above).

So this ought to work with the algorithm2e environment, you say. No, sorry. The reason is that algorithm2e does a Bad Thing™, namely

\gdef\@currentlabel{...}

in connection with line numbering, so at the point of usage of your \cite command, \@currentlabel points to the last line number in the algorithm.

Conclusion: backref=section isn't reliable.

Full example.

\begin{filecontents*}{\jobname.bib}
@misc{myarticle,
    author  = {ABC},
    title   = {A Title},
    year    = {2023},
}
\end{filecontents*}

\documentclass{article}

\usepackage[algo2e,linesnumbered,algonl]{algorithm2e}
\usepackage[backref=section]{hyperref}

\newtheorem{theorem}{Theorem}
\renewcommand{\thetheorem}{!!!\arabic{theorem}!!!}% very distinctive

\begin{document}

\section{Test}
\subsection{Test}
\subsubsection{Test}

\cite{myarticle}

% comment/uncomment the algorithm
\begin{algorithm2e}
$a=b$\;
$b=c$\;
$c=d$\;
\end{algorithm2e}

\paragraph{Test}

\cite{myarticle}

\begin{theorem}Citing~\cite{myarticle}.\end{theorem}

\bibliographystyle{plain}
\bibliography{\jobname}

\end{document}

enter image description here

Possible workaround (with many limitations)

\begin{filecontents*}{\jobname.bib}
@misc{myarticle,
    author  = {ABC},
    title   = {A Title},
    year    = {2023},
}
\end{filecontents*}

\documentclass{article}

\usepackage[algo2e,linesnumbered,algonl]{algorithm2e}
\usepackage[backref=section]{hyperref}

\newtheorem{theorem}{Theorem}
\renewcommand{\thetheorem}{!!!\arabic{theorem}!!!}% very distinctive

\makeatletter
\NewCommandCopy{\original@cite}{\cite}
\RenewDocumentCommand{\cite}{om}{%
  \begingroup
  \restore@sectional@label
  \IfNoValueTF{#1}{\original@cite{#2}}{\original@cite[#1]{#2}}%
  \endgroup
}
\newcommand{\restore@sectional@label}{%
  \protected@edef\@currentlabel{%
    \ifnum\value{subsubsection}>0 \thesubsubsection\else
    \ifnum\value{subsection}>0 \thesubsection\else
    \thesection\fi\fi
  }%
}
\makeatother


\begin{document}

\section{Test}

\cite{myarticle}

\subsection{Test}

\cite{myarticle}

\subsubsection{Test}

\cite{myarticle}

% comment/uncomment the algorithm
\begin{algorithm2e}
$a=b$\;
$b=c$\;
$c=d$\;
\end{algorithm2e}

\paragraph{Test}

\cite{myarticle}

\begin{theorem}Citing~\cite{myarticle}.\end{theorem}

\bibliographystyle{plain}
\bibliography{\jobname}

\end{document}

enter image description here

6
  • we should get algorithm2e to not use gdef or (simpler) save and restore \@current;label for the enviornment so at least locally it restores correctly at the end? Dec 11, 2023 at 10:40
  • @DavidCarlisle Getting bugs fixed in algorithm2e is a dead attempt.
    – egreg
    Dec 11, 2023 at 11:01
  • LPPL............ Dec 11, 2023 at 11:10
  • @DavidCarlisle I cannot recommend algorithm2e over other similar packages. Anyway, as I showed, backref=section isn't reliable.
    – egreg
    Dec 11, 2023 at 11:19
  • well there is that too Dec 11, 2023 at 11:20

You must log in to answer this question.

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