[Tex/LaTex] Hyperlinks from chapter and section headings back to the table of contents

hyperreflinkssectioningtable of contents

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.

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