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
.
Best Answer
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.