1
% !TeX TS-program = pdflatex
% !TeX encoding = UTF-8
\documentclass{article}
\usepackage{float}
\usepackage{graphicx}
\counterwithin{figure}{section}
\usepackage{hyperref}
%\counterwithin{figure}{section}
%\usepackage[all]{hypcap}
\begin{document}

\section{one}
 \begin{figure}[H]
  \centerline{\includegraphics[width=10cm]{example-image}}
  \caption{example-image}
 \end{figure}


\clearpage
\section{two}
 \begin{figure}[htb]
  \centerline{\includegraphics[width=11cm]{example-image-a}}
  \caption{example-image-a}
 \end{figure}

\end{document}

(pdftex.def) Requested size: 312.9803pt x 234.74237pt.
pdfTeX warning (ext4): destination with the same identifier (name{figure.1}) ha
s been already used, duplicate ignored
to be read again>
\relax
l.23 \caption{example-image-a}

I've read

and the Problem persist

3 Answers 3

2

Redefine \theHfigure so that it gives an unique identifier for the destination anchor:

\documentclass{article}
\usepackage{float}
\usepackage{graphicx}
\counterwithin{figure}{section}
\usepackage{hyperref}
%\counterwithin{figure}{section}
%\usepackage[all]{hypcap}
\renewcommand\theHfigure{\thesection.\arabic{figure}}
\begin{document}

\section{one}
 \begin{figure}[H]
  \centerline{\includegraphics[width=10cm]{example-image}}
  \caption{example-image}
 \end{figure}


\clearpage
\section{two}
 \begin{figure}[htb]
  \centerline{\includegraphics[width=11cm]{example-image-a}}
  \caption{example-image-a}
 \end{figure}

\end{document}
1
  • By adding \renewcommand\theHfigure{\thesection.\arabic{figure}} and \renewcommand\theHtable{\thesection.\arabic{table}} to my preamble, I was able to eliminate all these annoying warnings in my long document. Thanks!
    – R.K.G.
    Commented Feb 3, 2022 at 16:48
2

hyperref patches \counterwithin but you use it before hyperref got the change of patching it. So use \counterwithin after loading the hyperref package and the problem should be gone.

The answer from Ulrike is just a work-around, trying to correct the damage done by the wrong package load and usage order. IMHO this is the wrong approach.

See also https://texfaq.org/FAQ-hyperdupdest

"In most cases, therefore, you should load your package before you load hyperref, and hyperref will patch things up so that they work, so you can utilise your (patched) package after loading both"

Means in this case: Load the package which provides \counterwithin first (not necessary here since it is offered by recent LaTeX kernels), load hyperref afterwards (so it can patch \counterwithin, and then (and only then) use \counterwithin.

1
\counterwithin{figure}{section}
\usepackage[all]{hypcap}

in the preamble after the hyperref-package. In this case \counterwithin{figure}{section} aswell \usepackage[all]{hypcap} is enough, but sometimes you need both, also \usepackage{caption} often helps.

This question uses

1
  • I like this answer as well, and it worked just as well as Ulrike Fischer's answer. I also had to add \counterwithin{table}{section}. This eliminated all the annoying warnings.
    – R.K.G.
    Commented Feb 3, 2022 at 16:53

You must log in to answer this question.

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