手动设置字母数字章节编号时,目录中的超链接错误

手动设置字母数字章节编号时,目录中的超链接错误

我希望有章节附录,即在章节末尾有带\Alph编号的节。我的宏\chapterappendix\mainchapter可以\mainappendix正常工作,因为它们可以完全按照我的要求切换文档和目录中的节编号。

但是,hyperref目录中附录部分的链接指向错误的目标,即带有\arabic编号的部分,见下文。

梅威瑟:

\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}

文件内容.toc

\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}

我怎样才能知道hyperref链接到例如部分section.1.A而不是section.1.1

编辑

我已经更准确地说明了我的问题包括在附录和非附录模式之间来回切换。 MWE 现在更加完整了。

答案1

通过使用hyperref hypertexnameshyperref选项,以及的等价更新\thesection\theHsection)。

我无法不使用plainpages“页面系列”进行一些黑客攻击来纠正页面锚点,这要归功于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}

相关内容