# Wrong (hyper-)references after resetting chapter counter

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?

-

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

\usepackage[hypertexnames=false]{hyperref}

-
Wow, this actually works, and makes sense. But it’s not guessable from the documentation’s description of the option. Documentation fail. :-( –  Konrad Rudolph Nov 28 '10 at 18:01
This helped me with hyperref getting its PDF bookmarks all tangled up because I was resetting chapter numbering in my book parts. +1! –  DevSolar Sep 21 '11 at 15:46
+1 well deserved! Got my problem in scientific workplace fixed where my hiërarchy got totally messed up. –  Nick Nov 8 '13 at 15:48

You have two chapter 1. Naturally this confuse hyperref. You must give the internal counter a unique "look". E.g.:

\documentclass{report}
\usepackage{hyperref}
\newcommand*\backmatter{%
\setcounter{chapter}{0}%
\renewcommand\theHchapter{back.\arabic{chapter}}}

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

-
Naturally. ;-) But I did originally set \thechapter without effect. And \theHchapter doesn’t even appear in the documentation (nor does any other occurrence of ‘theH’, according to grep). –  Konrad Rudolph Nov 28 '10 at 17:54