0

The code below is used to generate two side-by-side algorithms that float to the top of the page.

\documentclass{article}

\usepackage{algpseudocode}
\usepackage{algorithm}
\usepackage{caption}
\usepackage{hyperref}

\begin{document}
\begin{figure}[!t]
  \begin{minipage}[t]{.475\linewidth}
    \begin{algorithm}[H]
      \caption{Foo}
      \label{alg:foo}
      \begin{algorithmic}
        \State Hi
      \end{algorithmic}
    \end{algorithm}
  \end{minipage}\hfill
  \begin{minipage}[t]{.475\linewidth}
    \begin{algorithm}[H]
      \caption{Bar}
      \label{alg:bar}
      \begin{algorithmic}
        \State Hi
      \end{algorithmic}
    \end{algorithm}
  \end{minipage}
\end{figure}

\end{document}

The program works as expected except that hyperref generates warning

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

The warning still occurs even if there is only one minipage instead of two side-by-side, so I imagine it is from nesting algorithm[H] within figure.

How can the warning hyperref be fixed?

I have read this thread and all references therein but could not find the solution.

6
  • It's just a warning, not an error. The potential problem with reusing identifiers is that a hyperlink might take you to the wrong one, but these are right next to each other, and it seems unlikely you'll want to link especially to the one on the right rather than the one on the left. So unless your actual use case is more complicated, you can just ignore the warning.
    – frabjous
    Jun 14 at 18:12
  • Unfortunately for this particular case, the hyperref must resolve to the correct location.
    – jII
    Jun 14 at 18:21
  • Could you add the relevant kind of hyperlinks to the example so we can see a example where the hyperlinks matter?
    – frabjous
    Jun 14 at 18:24
  • I'm not sure I understand the request. The link is created by \cref{fig:foo}, for example. It must resolve to the correct location because we are developing a digital book with a range of capabilities such as audio, jump-to highlighting, etc., and so the metadata error can cause incorrect results. We are also happy to explore alternative ways to create side-by-side algorithms that avoid this issue altogether.
    – jII
    Jun 14 at 19:02
  • There's no \label in the sample, so no way to use \cref. The cleveref package is not loaded. \cref lists a number, but there are no numbers here. There's also no list of figures in the sample. Should these have different numbers/LoF entries? As is, I don't see the problem. These will be on the same page, so what metadata are you worried about? Clicked links typically scroll the content so that the target is at the top of the screen. These start in the same place vertically, so clicking on a link to one will have identical effects to clicking a link to the other, no?
    – frabjous
    Jun 14 at 21:01

1 Answer 1

1

Disable the hypcap feature, either globally or only for this figure. It gets confused by the double caption in your float:

\documentclass{article}

\usepackage{algpseudocode}
\usepackage{algorithm}
%
\usepackage{caption}
\usepackage{hyperref}

\begin{document}
\begin{figure}[!t]
\captionsetup{hypcap=false}
  \begin{minipage}[t]{.475\linewidth}
    \begin{algorithm}[H]
    \caption{Foo}
      \label{alg:foo}
      \begin{algorithmic}
        \State Hi
      \end{algorithmic}
    \end{algorithm}
  \end{minipage}\hfill
  \begin{minipage}[t]{.475\linewidth}
    \begin{algorithm}[H]
      \caption{Bar}
      \label{alg:bar}
      \begin{algorithmic}
        \State Hi
      \end{algorithmic}
    \end{algorithm}
  \end{minipage}
\end{figure}

\end{document}
1
  • it worked great. I did not understand "it gets confused by the double caption in your float", though, since the warning shows up even if there is only one algorithm instead of two.
    – jII
    Jun 15 at 12:31

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.