0

I'm trying to do something similar to "Using the same figure twice with no new number", but with the algorithm2e package.
Therefore I wrote the following code.

\newcommand{\repeatAlgoCaption}[2]{%
    \renewcommand{\thealgocf}{\ref{#1}}% modify the displayed label
    \captionsetup{list=no}% not displayed in the list of algorithms
    \caption{#2 (repeated from page~\pageref{#1})}% page reference of the original algorithm
    \addtocounter{algocf}{-1}% the next figure after the repeat gets the right number
}

Using this code with hyperref produces the following error

! Argument of \reserved@a has an extra }.
<inserted text>
              \par

I have no clue how to get this to work, any suggestion is appreciated.

This is my working MWE (without hyperref)

\documentclass{article}
    
\usepackage{caption}
\usepackage{algorithm2e}
    
% from https://tex.stackexchange.com/questions/200211/
\newcommand{\repeatAlgoCaption}[2]{%
    \renewcommand{\thealgocf}{\ref{#1}}% modify the displayed label
    \captionsetup{list=no}% not displayed in the list of algorithms
    \caption{#2 (repeated from page~\pageref{#1})}% page reference of the repeted algorithm
    \addtocounter{algocf}{-1}% the next figure after the repeat gets the right number
}
    
% \usepackage{hyperref}% The one guilty
    
% arara: pdflatex
% arara: pdflatex
\begin{document}
    
\begin{algorithm}[H]
\caption{my caption}\label{alg:mylabel}
\end{algorithm}
    
\begin{algorithm}[H]
\caption{my other caption}
\end{algorithm}
    
\begin{algorithm}[H]
\repeatAlgoCaption{alg:mylabel}{my caption}
\end{algorithm}
    
\begin{algorithm}[H]
\caption{my other caption}
\end{algorithm}
    
\end{document}

The output is

correct ouput

1 Answer 1

1

One could save the contents of \thealgoc (using an extra macro) instead of using \ref (or \ref*), for example:

\documentclass{article}

\usepackage{caption}
\usepackage{algorithm2e}

\makeatletter
\newcommand\saveAlgoCounter[1]{%
    \expandafter\xdef\csname saveAlgoCounter@#1\endcsname{\thealgocf}}% save \thealgocf
\newcommand{\repeatAlgoCaption}[2]{%
    \expandafter\let\expandafter\thealgocf\csname saveAlgoCounter@#1\endcsname% use saved \thealgocf
    \captionsetup{list=no}%
    \caption{#2 (repeated from page~\pageref{#1})}% page reference of the repeted algorithm
    \addtocounter{algocf}{-1}% the next figure after the repeat gets the right number
}
\makeatother

\usepackage[hypertexnames=false]{hyperref}% The one guilty
% "hypertexnames=false" prevents the warning "destination with the same identifier (name{algocf.1}) has been already used, duplicate ignored"

% arara: pdflatex
% arara: pdflatex
\begin{document}

\begin{algorithm}[H]
\caption{my caption}\label{alg:mylabel}
\saveAlgoCounter{alg:mylabel}
\end{algorithm}

\begin{algorithm}[H]
\caption{my other caption}
\end{algorithm}

\begin{algorithm}[H]
\repeatAlgoCaption{alg:mylabel}{my caption}
\end{algorithm}

\begin{algorithm}[H]
\caption{my other caption}
\end{algorithm}

\end{document}
2
  • May I ask you why \ref or \ref* don't work? Commented Mar 28, 2021 at 10:33
  • 1
    \ref uses the aux file to get the information and therefore contains code which isn't easy to handle, at least not for me who isn't a TeX freak. Furthermore things like \ref do not deliver the needed information on the 1st LaTeX run. Both are reasons - at least for me - to store and restore \thealgoc on my own. (Luckily we don't need back references here.) So in short: I have no clue why \ref or \ref* does not work here but since I'm not a TeX freak I guess I would not even understand the explanation. But I'm looking forward to a better solution for this problem than mine. Commented Mar 28, 2021 at 12:27

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.