[Tex/LaTex] Wrong (hyper-)references after resetting chapter counter

countershyperref

The following MWE produces a PDF whose TOC has two entries: “Foo” and “Bar” with correct page numbers. So far, so good. However, upon clicking on the link to “Bar”, I am taken to the page for “Foo” instead.

\documentclass{report}
\usepackage{hyperref}
\newcommand*\backmatter{\setcounter{chapter}{0}}

\begin{document}
\tableofcontents
\chapter{Foo}
\backmatter
\chapter{Bar}
\end{document}

(Produced with pdflatex, run twice.)

This MWE is whittled down from a (much!) more complex document but the above does seem to reproduce the error reliably.

Re-setting the chapter count seems to confuse hyperref, even though the page references are still accurate. Notice that in my real document, \backmatter does other things, including setting the numbering style to alphabetical so there’s no chance of confusion even though both chapters are ostensibly labelled as “Chapter 1”.

How do I fix this? Is this a bug? Are there workarounds?

Best Answer

Hyperref is just too clever sometimes. Make it dumber, and hence more robust:

\usepackage[hypertexnames=false]{hyperref}
Related Question