9

What I'd finally like to have are clickable backlinks from the headings to the table of contents. The goal is to click on a heading and land on that exact heading in the toc. As far as my search efforts are concerned there is no easy way to reach this. So I tried to start by linking to the toc like here. But I already fail to place links in headings, which seems to be impossible and results in this error message:

pdfTeX error (ext1): \pdfendlink cannot be used in vertical mode.

Just to clarify this, as there seem to be potential misunderstandings in other discussions on the web: I don't want to change the style of the headings! The links should be invisible like when using the hidelinks option. I left the links visible in the MWE for better overview.

So here's the MWE with one of my error producing lines NOT commented out:

\documentclass{scrreprt}
\usepackage{hyperref}
% \usepackage[hidelinks]{hyperref}%Works like a charm

% And I would like to do something like that too:
% \renewcommand{\chapter}[1]{\hyperlink{toc}{\chapter{#1}}}

\begin{document}

\hypertarget{toc}{\tableofcontents}

\chapter{This is a great headline}%Working
% \chapter{This \hyperlink{toc}{is} a great headline}%Not working
% \chapter{\hyperlink{toc}{This is a great headline}}%Also not working
\hyperlink{toc}{\chapter{This is a great headline}}%Also not working
Here is some text to prove, that \hyperlink{toc}{links} to toc work, if they're not in headings\ldots
\section{This is a section heading}
% \section{This is a \hyperlink{toc}{section} heading}%Also not working
\section{and another}

\chapter{This is a bad headline!}
\section{last section}

\end{document}

In the end it would be great to get the links automatically in every heading, like I tried to with the renewcommand.

1
  • The main problem is that the chapter title (even the optional chapter title) is used in more than one place. I can put a \hypertarget into the TOC, but it will also be placed on the top of each page. What you really need is to modify \contentsline to add a hypertaget such as backlink.chapter.1 (where chapter.1 is the current 4th argument). Jun 24 '15 at 14:36
5

I placed the redefinition of \contentsline after \begin{document} since hyperref puts what appears to be a redefinition in the aux file. But on closer examination it seems this code doesn't actually do anything.

\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

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

\end{document}
15
  • Well, if I could click on "accept this answer a 2nd time" I would do it... :) Solved everything. Thanks very much!
    – caligula
    Jun 24 '15 at 16:18
  • I have a habit of refining my answers until I get it right. For example, I just noticed that there is a \chapter* and \section* option I need to implement. Jun 24 '15 at 16:28
  • I wish you good luck :) In the meantime I found out that a \let\chapter=\section after all of this has to be \let\hyperchapter=\hypersection to work....
    – caligula
    Jun 24 '15 at 16:50
  • :D You're pretty fast. On the first sight I'd say there's a \makeatother missing...
    – caligula
    Jun 24 '15 at 16:52
  • I caught it. Also used \@empty instead of \empty. I generally don't bother, but since I need \@ifstar anyway... Jun 24 '15 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.