1

I am writing my thesis and I am struggeling with the links in the table of content.

At the end of the first chapter I have an appendix. Then another chapter follows. To end the appendix and return to the original numbering I implemented a solution I found online (definition of an \unappendix command).
The table of content looks great and the numbering is like I wanted it to be. Unfortunately the part between \makeatletter and \makeatother breaks the links of section 1.1 and 1.2 and makes them point to A.1 and A.2. All other links in the table of content work as expected (see Methods2).
How can I fix the two broken links?

A minimal working example can be found below.

\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 Answer 1

0

hyperref has to create destination names for the links. It does this normally by using the section number. If you look in the .out-file created by hyperref you can see the problem of your document:

\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

The destinations names section.1.1 and section.1.2 appear twice and pdftex will issue warnings in the log:

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

To make the destination names unique you can redefine \theHsection and other \theH-commands. In your case this e.g. works:

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

Now the .out-file looks like this:

\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
1
  • Thank you very much for your fast solution. It works perfectly in my case.
    – Green
    Apr 3, 2019 at 16:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .