目录中的链接已损坏

目录中的链接已损坏

我正在写论文,正在努力处理目录中的链接。

第一章末尾有一个附录。然后是另一章。为了结束附录并返回原始编号,我实施了在线找到的解决方案(\unappendix 命令的定义)。
目录看起来很棒,编号也符合我的要求。不幸的是,\makeatletter 和 \makeatother 之间的部分破坏了第 1.1 和 1.2 节的链接,使它们指向 A.1 和 A.2。目录中的所有其他链接均按预期工作(请参阅方法 2)。
我该如何修复这两个断开的链接?

下面可以找到一个最小的工作示例。

\documentclass[a4paper,12pt]{book}
\usepackage[colorlinks=false, breaklinks]{hyperref}

\hypersetup{
   hypertexnames=true,
   colorlinks=true,         % false: boxed links; true: colored links
   linkcolor=black,          % color of internal links 
}

\usepackage{chngcntr}

\makeatletter
\newcounter{savesection}
\newcounter{apdxsection}
\renewcommand\appendix{\par
  \setcounter{savesection}{\value{section}}%
  \setcounter{section}{\value{apdxsection}}%
  \setcounter{subsection}{0}%
  \gdef\thesection{\@Alph\c@section}}
\newcommand\unappendix{\par
  \setcounter{apdxsection}{\value{section}}%
  \setcounter{section}{\value{savesection}}%
  \setcounter{subsection}{0}%
  \gdef\thesection{\thechapter.\arabic{section}}}
\makeatother

\begin{document}
\setlength{\parindent}{0pt}
\frontmatter

\newpage

\addtocontents{toc}{\protect\hypertarget{toc}{}}
\tableofcontents
\markboth{}{}
\cleardoublepage

\thispagestyle{empty}
\mainmatter
\setcounter{page}{1}

\chapter{chapter1 chapter1 chapter1}

\section{Introduction}
text text text text text text text text text text text text text text text       text text text text text text text text text text text text text text text text text text text 

\section{Methods Methods Methods Methods Methods Methods Methods Methods  Methods Methods Methods Methods Methods Methods Methods Methods }

\section{Methods2 Methods2}
\newpage
\appendix
\counterwithin{figure}{section}

\renewcommand{\thesection}{A.\arabic{section}}
\newpage
\section{Part 1}
\newpage

\section{Part 2}
\newpage

\unappendix

\chapter{chapter2 chapter2 chapter2}

\section{Introduction}
text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text 

\end{document}

答案1

hyperref 必须为链接创建目标名称。它通常使用节号来执行此操作。如果您查看 hyperref 创建的 .out 文件,您会发现文档存在问题:

\BOOKMARK [0][-]{chapter.1}{chapter1 chapter1 chapter1}{}% 1
\BOOKMARK [1][-]{section.1.1}{Introduction}{chapter.1}% 2
\BOOKMARK [1][-]{section.1.2}{Methods Methods Methods Methods Methods Methods Methods Methods Methods Methods Methods Methods Methods Methods Methods Methods }{chapter.1}% 3
\BOOKMARK [1][-]{section.1.3}{Methods2 Methods2}{chapter.1}% 4
\BOOKMARK [1][-]{section.1.1}{Part 1}{chapter.1}% 5
\BOOKMARK [1][-]{section.1.2}{Part 2}{chapter.1}% 6
\BOOKMARK [0][-]{chapter.2}{chapter2 chapter2 chapter2}{}% 7
\BOOKMARK [1][-]{section.2.1}{Introduction}{chapter.2}% 8

目的地名称section.1.1section.1.2出现两次,pdftex 将在日志中发出警告:

pdfTeX warning (ext4): destination with the same identifier (name{section.1.1}
) has been already used, duplicate ignored

为了使目标名称唯一,您可以重新定义\theHsection和其他\theH命令。对于您的情况,例如:

\renewcommand\appendix{\par
  \setcounter{savesection}{\value{section}}%
  \setcounter{section}{\value{apdxsection}}%
  \renewcommand\theHsection{appendix.\thesection}
  \setcounter{subsection}{0}%
  \gdef\thesection{\@Alph\c@section}}
\newcommand\unappendix{\par
  \setcounter{apdxsection}{\value{section}}%
  \setcounter{section}{\value{savesection}}%
  \renewcommand\theHsection{\thesection}
  \setcounter{subsection}{0}%
  \gdef\thesection{\thechapter.\arabic{section}}}

现在.out 文件如下所示:

\BOOKMARK [0][-]{chapter.1}{chapter1 chapter1 chapter1}{}% 1
\BOOKMARK [1][-]{section.1.1}{Introduction}{chapter.1}% 2
\BOOKMARK [1][-]{section.1.2}{Methods Methods Methods Methods Methods Methods Methods Methods Methods Methods Methods Methods Methods Methods Methods Methods }{chapter.1}% 3
\BOOKMARK [1][-]{section.1.3}{Methods2 Methods2}{chapter.1}% 4
\BOOKMARK [1][-]{section.appendix.A.1}{Part 1}{chapter.1}% 5
\BOOKMARK [1][-]{section.appendix.A.2}{Part 2}{chapter.1}% 6
\BOOKMARK [0][-]{chapter.2}{chapter2 chapter2 chapter2}{}% 7
\BOOKMARK [1][-]{section.2.1}{Introduction}{chapter.2}% 8

相关内容