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.