0

hyperref messes up with \renewcommand\appendix. This even tho \appendix is renewed exactly as it is defined in report.cls (and works with hyperref !)

The problem: .toc file 4th entry of \contentsline changes (i.e links go wrong).

Without redefining \appendix .toc file is

\contentsline {chapter}{\numberline {1}Chap}{2}{chapter.1}
\contentsline {chapter}{\numberline {A}App}{3}{appendix.A}

As \appendix is redefined, .toc file becomes

\contentsline {chapter}{\numberline {1}Chap}{2}{chapter.1}
\contentsline {chapter}{\numberline {A}App}{3}{chapter.1}

MWE:

\documentclass{report}
\usepackage{hyperref}
%\makeatletter
%\renewcommand\appendix{\par
%  \setcounter{chapter}{0}%
%  \setcounter{section}{0}%
%  \gdef\@chapapp{\appendixname}%
%  \gdef\thechapter{\@Alph\c@chapter}}
%\makeatother
\begin{document}
\tableofcontents
\chapter{Chap}
\appendix
\chapter{App}
\end{document}

Why? And how to fix?

  • 1
    The hyperref package redefines a lot of LaTeX internals. Problems are usually avoided by loading it at the end of your preamble. However, in this case your redefinition will be overwritten if you do that. What effect are you trying to achieve? – Ian Thompson Sep 1 '14 at 18:37
  • 1
    \usepackage{hyperref} after your renewcommand will work -- well, Ian Thompson just made a quicker statement ;-) – user31729 Sep 1 '14 at 18:37
  • 1
    The problem is, that the chapter number is reset to zero within the \appendix, leading to the same hyperlink anchor chapter.1. – user31729 Sep 1 '14 at 18:40
4

Package hyperref already fixes \appendix by prepending \appendix to fix the anchor names. Your redefinition of \appendix throws the fix away. Therefore the redefinition, not hyperref is faulty.

Options:

  • Redefine \appendix before package hyperref, because hyperref redefinition is defensive and keeps the current meaning of \appendix in the redefined \appendix.

  • If \appendix should be redefined after package hyperref or any other package that defines/redefines \appendix, then it should be defined in a defensive way, e.g. via patch commands of package etoolbox. Or you need to put the additions of hyperref into the new definition of \appendix.

1

[This is perhaps not a full solution, but too long for a comment...]

I am not sure what you are trying to achieve as in your MWE the pdf file, with the \renewcommand commented out, hyperref is doing the right thing. Presumably you want to redefine \appendix so that it does something different to what is described in the comments. In which case, rather than looking at the standard definition of \appendix from report.cls you should look at hyperref's definition of \appendix, which is the following:

\def\appendix{%
  \ltx@IfUndefined{chapter}{%
    \gdef\theHsection{\Hy@AlphNoErr{section}}%
  }{%
    \gdef\theHchapter{\Hy@AlphNoErr{chapter}}%
  }%
  \xdef\Hy@chapapp{\Hy@appendixstring}%
  \HyOrg@appendix
}

Note that, according to hyperref.sty, \HyOrg@appendix=\appendix. So, rather than modifying \appendix you should change \HyOrg@appendix. For example, if you use

\renewcommand\HyOrg@appendix={\par
  \setcounter{chapter}{0}%
  \setcounter{section}{0}%
  \gdef\@chapapp{\appendixname}%
  \gdef\thechapter{\@Alph\c@chapter}}

then the pdf file is now correct. Depending on what you are trying to achieve you may also need to modify the hyperref commands above as well.

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.