54

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?

2 Answers 2

78

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

\usepackage[hypertexnames=false]{hyperref}
7
  • 11
    Wow, this actually works, and makes sense. But it’s not guessable from the documentation’s description of the option. Documentation fail. :-( Commented Nov 28, 2010 at 18:01
  • 2
    This helped me with hyperref getting its PDF bookmarks all tangled up because I was resetting chapter numbering in my book parts. +1!
    – DevSolar
    Commented Sep 21, 2011 at 15:46
  • +1 well deserved! Got my problem in scientific workplace fixed where my hiërarchy got totally messed up.
    – Nick
    Commented Nov 8, 2013 at 15:48
  • 1
    @KonradRudolph I can confirm this works having stumbled into the same problem and used the same fix, but how in the world does this make sense? :(
    – ffledgling
    Commented Nov 1, 2015 at 16:21
  • 6
    @ffledgling Look at Ulrike’s answer: by default, hyperref derives internal link identifiers based on chapter numbers. However, when using different sections, there are two chapters with number 1. So by making hyperref dumber, we force it to adopt internal consecutive identifiers, rather than identifiers that map to actual chapter numbering. Commented Nov 1, 2015 at 16:49
16

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}
1
  • 3
    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). Commented Nov 28, 2010 at 17:54

You must log in to answer this question.

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