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}

% And I would like to do something like that too:

\begin{document}

\hypertarget{toc}{\tableofcontents}

\section{and another}

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

• 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

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

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

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

\tableofcontents


• 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.... Jun 24 '15 at 16:50
• :D You're pretty fast. On the first sight I'd say there's a \makeatother missing... Jun 24 '15 at 16:52