2

I've implemented the back-link procedure given in the answer here, which works fine when also extended further to include \subsection{} and \subsubsection{} back-links. All work fine in normal chapters and appendices except for \chapter headings used in Appendices which show as a link in the PDF but go nowhere when clicked. But, if I start the appendix without a `\chapter{}' the TOC and appendix's heading title is wrong.

So how do I get the \chapter{} elements in Appendices to work as they do in main body book chapters.

My MWE:

\documentclass{scrreprt}
\usepackage{hyperref}
\makeatletter
\let\hyperchapter\chapter
\def\chapter{\@ifstar\starchapter\mychapter}
\def\starchapter{\hyperchapter*}
\newcommand{\mychapter}[2][\@empty]% #1=optional (toc and top of page), #2=title
{\ifx#1\@empty \hyperchapter[#2]{\hyperlink{toc.chapter.\thechapter}{#2}}
 \else \hyperchapter[#1]{\hyperlink{toc.chapter.\thechapter}{#2}}
 \fi}

\let\hypersection\section
\def\section{\@ifstar\starsection\mysection}
\def\starsection{\hypersection*}
\newcommand{\mysection}[2][\@empty]% #1=optional (toc), #2=title
{\ifx#1\@empty \hypersection[#2]{\hyperlink{toc.section.\thesection}{#2}}
 \else \hypersecton[#1]{\hyperlink{toc.section.\thesection}{#2}}
 \fi}
\makeatother

\usepackage[titletoc,title]{appendix}
\begin{document}
\let\hypercontentsline=\contentsline
\renewcommand{\contentsline}[4]{\hypertarget{toc.#4}{}\hypercontentsline{#1}{#2}{#3}{#4}}

\tableofcontents

\chapter{This is a great headline}
\section{and another}

\chapter{This is a bad headline!}
\section{last section}
\begin{appendices}
\appendix
\chapter{First One}
Stuff
\section{A Heading}
Other Stuff
\chapter{Second One}
Some notes here.
\end{appendices}

\end{document}

[edit: clarity]

mwra
  • 171
  • I tried changing the first heading in the appendix from \chapter to \section but that resulted in the appendix not being in the ToC. Still hoping someone might have an answer... – mwra Jun 20 '18 at 10:51
  • Maybe this similar question contains your answer? https://tex.stackexchange.com/questions/174887/link-to-appendix-from-anywhere-in-the-document-goes-to-the-wrong-place It worked for me. – Graipher Feb 12 '19 at 11:54
  • @Graipher - sadly not. Plus adding code from that answer broke all the section/sub-section links back to the TOC and broke in-text links in the appendices. – mwra Feb 13 '19 at 13:15

1 Answers1

2

I was also looking at how to fix this problem. So without being a super-expert in LaTeX programming, I think I figured this out.

How to debug it

The main trick is to figure out how each chapter and section is actually called in the TOC. If you go and modify this line:

\renewcommand{\contentsline}[4]{\hypertarget{toc.#4}{}\hypercontentsline{#1}{#2}{#3}{#4}}

to

\renewcommand{\contentsline}[4]{\hypertarget{toc.#4}{}\hypercontentsline{#1}{#2}{#3}{#4}#4}

It will print the "internal" name of that section/chapter/appendix. Now, note that this command is used to simply add an hypertarget to a line in the TOC. The actual line is the hypercontentsline, which is simply a macro that references the original contentsline:

\let\hypercontentsline=\contentsline

The other part is the hypertarget, which is what we're actually adding, with a target name called toc.#4.

Now if we go up to the part of the code that modifies the chapter command, it works like this. The first three lines are simply making sure that if you call \chapter* you bypass the macro and do the normal thing, since there's no need to add links to the TOC if they are not going to be in the TOC anyway.

The line \def\chapter{\@ifstar\starchapter\mychapter} is the one that actually replaces \chapter with our definition of \mychapter which adds the backlink.

In \mychapter there is an if that simply checks whether you passed the optional short title for the TOC (like \chapter[shortToc]{normalName}). Both branches then call hyperchapter, which is simply the original chapter macro, and pass as the title an hyperlink to the TOC, plus the actual title you want.

So the form there is:

\hyperchapter[#2]{\hyperlink{toc.chapter.\thechapter}{#2}}
              ^^                      ^^^             ^^
       title to put in TOC | where the link goes | the actual title

Now the trick is that while at the hypertarget definition we do hypertarget{toc.#4}, where #4 contains the "type" of the chapter/appendix/section, the \thechapter macro only returns the "numbering" without the type. This is why when we specify the hyperlink we add that part explicitly: hyperlink{toc.chapter.\thechapter}.

Now, the whole issue comes from the fact that in the hypertarget definition in TOC, an appendix chapter is denoted as "appendix", and not "chapter". For example, "appendix.A", and not "chapter.A" (so overall "toc.appendix.A"). This is why when we go to construct the hyperlink, we create a link to "toc.chapter.A", which doesn't exist, and doesn't work.


How to fix it

We simply need to introduce a new command, which we will use to specify chapters in the appendix, which creates the appropriate "appendix" link to the target.

I did it like this:

\let\hyperappchapter\chapter
\def\appchapter{\@ifstar\starappchapter\myappchapter}
\def\starappchapter{\hyperappchapter*}
\newcommand{\myappchapter}[2][\@empty]% #1=optional (toc and top of page), #2=title
{\ifx#1\@empty \hyperchapter[#2]{\hyperlink{toc.appendix.\thechapter}{#2}}
 \else \hyperchapter[#1]{\hyperlink{toc.appendix.\thechapter}{#2}}
 \fi}

The idea is that underneath we're still using the \chapter command (since that's what creates a chapter even in the appendix). However, this time our links will have the correct appendix prefix when defining the hyperlink.

Now you just need to define a chapter in an appendix as \appchapter{MyTitle}, and the link will work correctly!

Svalorzen
  • 296
  • Thanks, this is wonderful news. Amending my test code example above to replace the 8 lines starting \let\hyperappchapter\chapter and changing \chapter{First One} to \appchapter{First One} all worked after I'd cleaned out existing aux files from a previous bad [sic] test. There well may be a better /cleverer solution, but I wouldn't know :). This works, so marked as answered. Too late for my thesis but welcomed nonetheless and I hope it helps others doing non-trivial document structures. Thanks again – mwra Sep 19 '22 at 18:36
  • Oops - I meant *add add the extra \let\hyperappchapter\chapter after `\let\hyperchapter\chapter not replace replace as I commented above. It would be useful if a mod or admin could add a functional amended code listing of my example into @Svalorzen's answer as a copy-able full working example is more likely to help later readers. – mwra Sep 19 '22 at 19:58