2

I am using hyperref to typeset a book containing many different research articles. Each article has its own set of theorems, definitions etc. and with every \maketitle the corresponding counters are reset, just as one would expect. Thus, there are multiple "Theorem 1" etc. This seems to be no problem for hyperref since they all have different anchors; everything is as it should be.

Now some of the articles contain algorithms typeset with algorithm2e and the counters for those are not reset by default, which is why I put a

\setcounter{algocf}{0}

in front of every article. This changes the label, but I get

destination with the same identifier (name {algocf.1})
has been already used, duplicate ignored

and the hyperref link always points to the "first" Algorithm 1 etc.

I am afraid this solution (where figures and tables were considered) does not work for my problem:

hyperref points wrong figure/table with reset counter

I tried

\renewcommand{\theHalgocf}{new.\thealgocf}

but to no avail.

The same problem appears with the subfloat command from the subfig package (here, counters are reset, but anchors are still ambiguous):

destination with the same identifier (name{subfigure.4.2})
has been already used, duplicate ignored

MWE

\documentclass{article}

\usepackage{algorithm2e}
\usepackage{hyperref}
\usepackage{cleveref}

\crefname{algorithm}{Algorithm}{Algorithms}

\begin{document}

\begin{algorithm}
  \caption{Algo 1}\label{1}
  Test\;
\end{algorithm}

\setcounter{algocf}{0}

\begin{algorithm}
  \caption{Algo 2}\label{2}
  Test\;
\end{algorithm}

\Cref{1} and \cref{2}.

\end{document}
  • Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – Heiko Oberdiek Feb 27 '18 at 7:09
  • By default, \maketitle does not reset counters, apart from footnote. By default, you can't use \maketitle more than once as well (well, you can, but it does nothing after the first usage) – user31729 Feb 27 '18 at 7:56
1

The cause for this problem is that algorithm2e redefines \theHalgocf in its caption wrapper again.

I've changed this to prepend a prefix to \thealgocf in \theHalgocf each time \Maketitle is called, which steps a titlectr counter, that resets the algocf counter as well.

\Maketitle is a wrapper for \maketitle, the later can't be used more than once without changing -- it is not shown how those changes are done by the O.P. to allow for more than one usage of \maketitle.

The regular \maketitle does not reset counters apart from footnote, however.

\documentclass{article}

\usepackage{algorithm2e}
\usepackage{chngcntr}
\usepackage{hyperref}
\usepackage{cleveref}


\newcounter{titlectr}

\counterwithin*{algocf}{titlectr}

%\counterwithin*{subfig}{titlectr}% for subfigures



\makeatletter
\AtBeginDocument{%
\long\def\algocf@caption@algo#1[#2]#3{%
  \ifthenelse{\equal{\algocf@algocfref}{\relax}}{}{\algocf@captionref}%
  \@ifundefined{hyper@refstepcounter}{\relax}{% if hyper@refstepcounter undefind, no hyperref, else...
    \ifthenelse{\equal{\algocf@algocfref}{\relax}}{\renewcommand{\theHalgocf}{\thetitlectr.\thealgocf}}{% take algocf as Href
      \renewcommand{\theHalgocf}{\thetitlectr.\algocf@algocfref}}%else if SetAlgoRefName done, take this name as ref.
    \hyper@refstepcounter{algocf}%set algocf as category of ref
  }%
   \algocf@latexcaption{#1}[{#2}]{{#3}}% call original caption
}%
}
\makeatother




\newcommand{\Maketitle}{%
  \stepcounter{titlectr}%
}


\title{foo}

\author{A. U. Thor}

\crefname{algorithm}{Algorithm}{Algorithms}

\begin{document}
\Maketitle

\begin{algorithm}
  \caption{Algo 1}\label{1}
  Test\;
\end{algorithm}


\Maketitle

\begin{algorithm}
  \caption{Algo 2}\label{2}
  Test\;
\end{algorithm}

\Cref{1} and \cref{2}.

\end{document}

Excerpts of \jobname.aux:

\@writefile{loa}{\contentsline {algocf}{\numberline {1}{\ignorespaces Algo 1}}{1}{algocf.1.1}}
\newlabel{1}{{1}{1}{}{algocf.1.1}{}}
\newlabel{1@cref}{{[algorithm][1][]1}{[1][1][]1}}
\@writefile{loa}{\contentsline {algocf}{\numberline {1}{\ignorespaces Algo 2}}{1}{algocf.2.1}}
\newlabel{2}{{1}{1}{}{algocf.2.1}{}}
\newlabel{2@cref}{{[algorithm][1][]1}{[1][1][]1}}
  • Thank you very much for this. That worked like a charm. I am sorry that I did not explain that I use a non-standard cls-file with \maketitle modified. Can I use the same strategy also for the counter subfigure or are there some more strings to pull since that counter in turn depends on the figure counter? – Howard Feb 27 '18 at 12:22
  • @Howard: Uncommenting my line for \counterwithout*{subfigure}{titlecntr} and \renewcommand{\theHsubfig}{subfigure.\thetitlecntr.\thesubfigure} should be sufficient, but this depends on the used subfigure code, i.e. the relevant package – user31729 Feb 27 '18 at 15:13
1

Had some similar problems in the past:

MWE:

\documentclass{article}
\usepackage{hyperref}
\usepackage{cleveref}

\crefname{figure}{Fig}{Figs}

\begin{document}

\begin{figure}
  \caption{Test1}\label{1}
\end{figure}

\begin{figure}
\caption{Test2}\label{2}
\end{figure}

\setcounter{figure}{0}

\pagebreak
\begin{figure}
\caption{Test3}\label{3}
\end{figure}

\Cref{1} and \cref{2}.

\Cref{3}.

\end{document}

This gives the error and second link Figure 1 doesnt work (ignored)

pdfTeX warning (ext4): destination with the same identifier (name{figure.1}) has been already used, duplicate ignored

By adding and using the caption or subcaption package problem solved:

\documentclass{article}
\usepackage{hyperref}
\usepackage{cleveref}
\usepackage{caption}
%\usepackage{subcaption}

\crefname{figure}{Fig}{Figs}


\begin{document}

\begin{figure}
  \captionof{figure}{Test1}\label{1}
\end{figure}

\begin{figure}
\captionof{figure}{Test2}\label{2}
\end{figure}

\setcounter{figure}{0}

\pagebreak
\begin{figure}
\captionof{figure}{Test3}\label{3}
\end{figure}

\Cref{1} and \cref{2}.

\Cref{3}.

\end{document}

Now everything works as expected.

PS: Sorry for changing the example but have not algorithm installed and could not test.

An alternative (in case it doesn't work even with caption or subcaption) was a command I had created like

\AtBeginDocument{\let\oldcaptionof\captionof
\renewcommand{\captionof}[2]{\ifx#1{figure}\captionsetup{type=figure}\else\ifx#1{table}\captionsetup{type=table}\else\ifx#1{algorithm}\captionsetup{type=algorithm}\else\ifx#1{subfigure}\captionsetup{type=subfigure}\else\ifx#1{subtable}\captionsetup{type=subtable}\else\ifx#1{subalgorithm}\captionsetup{type=subalgorithm}\fi\fi\fi\fi\fi\fi\oldcaptionof{#1}{#2}}}

But can't test it now because previous fix works for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.