3

I wish to have chapter appendices in the sense that there are sections at the end of chapters with \Alph numbering. My macros \chapterappendix, \mainchapter and \mainappendix work in so far as they switch section numbering in the document and TOC exactly how I would like it.

However, hyperref links in the TOC for the appendix sections point to wrong targets, namely to sections with \arabic numbering, see below.

MWE:

\documentclass{scrbook}
\usepackage{hyperref}
\makeatletter

\newcommand*\mainchapter{%
    \if@twoside\cleardoubleoddpage\else\clearpage\fi
    \setcounter{page}{1}%
    \renewcommand*{\thesection}{\arabic{chapter}.\arabic{section}} }

\newcommand*{\chapterappendix}{%
    \if@twoside\cleardoubleoddpage\else\clearpage\fi
    \setcounter{section}{0}%
    \renewcommand*{\thesection}{\arabic{chapter}.\Alph{section}} }

\newcommand*{\mainappendix}{%
    \if@openright\cleardoubleoddpage\else\clearpage\fi
    \stepcounter{chapter}\setcounter{section}{0}%
    \renewcommand*{\thesection}{\Alph{section}} 
    \addtocontents{toc}{\protect\contentsline{chapter}{%
        \protect\nonumberline Appendix}{}{}} }

\begin{document}
\tableofcontents

\mainchapter
\chapter{MainChap1}
\section{MainSec1.1}
\chapterappendix
\section{AppSec1.A}

\mainchapter
\chapter{MainChap2}
\section{MainSec2.1}
\chapterappendix
\section{AppSec1.A}

\mainappendix
\section{AppSecA}

\end{document}

Content of .toc file:

\contentsline {chapter}{\numberline {1}MainChap1}{1}{chapter.1}
\contentsline {section}{\numberline {1.1}MainSec1.1}{1}{section.1.1}
\contentsline {section}{\numberline {1.A}AppSec1.A}{3}{section.1.1}
\contentsline {chapter}{\numberline {2}MainChap2}{1}{chapter.2}
\contentsline {section}{\numberline {2.1}MainSec2.1}{1}{section.2.1}
\contentsline {section}{\numberline {2.A}AppSec1.A}{3}{section.2.1}
\contentsline {chapter}{\nonumberline Appendix}{}{}
\contentsline {section}{\numberline {A}AppSecA}{5}{section.3.1}

How can I tell hyperref to link, for example, to section section.1.A instead of section.1.1?

EDIT

I have stated more precisely that my problem includes switching back and forth between appendix and non-appendix mode. The MWE is more complete now.

2

Your problem with regards to incorrect jumping from the ToC is corrected by using the hyperref hypertexnames option, together with an update of hyperref's \thesection equivalent (\theHsection).

I was unable to correct the page anchors without some hacking using a plainpages "page series", thanks to etoolbox:

\documentclass{scrbook}

\usepackage{hyperref,etoolbox}
\hypersetup{
  hypertexnames,
  plainpages
}

\makeatletter
\newcommand*\mainchapter{%
  \if@twoside\cleardoubleoddpage\else\clearpage\fi
  \setcounter{page}{1}%
  \renewcommand*{\thesection}{\arabic{chapter}.\arabic{section}}%
  \renewcommand*{\@pageseries}{chapter.\thechapter.}% Add a page series to keep hyperref happy
}

\newcommand*{\chapterappendix}{%
  \if@twoside\cleardoubleoddpage\else\clearpage\fi
  \setcounter{section}{0}%
  \renewcommand*{\thesection}{\thechapter.\Alph{section}}%
  \renewcommand*{\theHsection}{\thesection}%
}

\newcommand*{\mainappendix}{%
  \if@openright\cleardoubleoddpage\else\clearpage\fi
  \stepcounter{chapter}%\setcounter{section}{0} <- done automatically
  \renewcommand*{\thesection}{\Alph{section}}%
  \addtocontents{toc}{\protect\contentsline{chapter}{%
    \protect\nonumberline Appendix}{}{}}%
}

\patchcmd{\Hy@EveryPageAnchor}% <cmd>
  {page.}% <search>
  {page.\@pageseries}% <replace>
  {}{}% <success><failure>
\newcommand{\@pageseries}{}
\makeatother

\begin{document}

\tableofcontents

\mainchapter
\chapter{MainChap1}
\section{MainSec1.1}
\chapterappendix
\section{AppSec1.A}

\mainchapter
\chapter{MainChap2}
\section{MainSec2.1}
\chapterappendix
\section{AppSec1.A}

\mainappendix
\section{AppSecA}

\end{document}

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.