13

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.

caligula
  • 563
  • 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). – John Kormylo Jun 24 '15 at 14:36

1 Answers1

9

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}
John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • 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. – John Kormylo 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... – John Kormylo Jun 24 '15 at 17:04
  • ...and I checked if you can put the contentsline stuff into the preamble and it seems to work if i put it before the other stuff! – caligula Jun 24 '15 at 17:08
  • 1
    Figured out this be done to also make subsections and subsubsections clickable by just changing every time we see the word "section" in the bottom command to "subsection". – Luke Davis Jan 27 '18 at 06:36
  • Is there anyway to also make the section-numbers clickable? The section-numbers in my table of contents are clickable, and the discrepancy bugs me. – Luke Davis Jan 27 '18 at 06:50
  • @LukeDavis - See https://tex.stackexchange.com/questions/126994/hyperref-not-linking-toc \hypersetup{linktoc=all} – John Kormylo Jan 27 '18 at 18:34
  • @JohnKormylo Sorry to be clear, the section-numbers and section-text in my Table of Contents are clickable, whereas only the section-text where they appear in the document are clickable -- e.g. for the section header "1 MySection", I can only click on the "MySection" part. I was hoping to make the section-numbers where they appear in the document to also be clickable. – Luke Davis Jan 27 '18 at 22:27
  • I tried putting the entire \chapter inside the target, but you cannot start or end a target in vmode. The changes I made to the title were superficial. What you want is much more invasive, and will depend on the definition of \chapter (which is document class dependent). – John Kormylo Jan 28 '18 at 00:03
  • @LukeDavis - In other words, it can be done, but it would be best to ask a new question using your specific document class. – John Kormylo Jan 28 '18 at 17:51
  • @JohnKormylo Thanks, I may do so. Alternatively, is there a simpler way to make the table-of-contents text, but not the section numbers clickable? Just wanted the numbers to either always be clickable or always not. Obviously this is a very nitpicky thing. – Luke Davis Jan 28 '18 at 21:25
  • @LukeDavis - One could modify \contentsline to NOT start the link, then modify \numberline to start it instead. Again, that is really outside the scope of this question. – John Kormylo Jan 29 '18 at 16:16
  • @JohnKormylo Thanks, I started a question here. – Luke Davis Jan 29 '18 at 23:21
  • Please see my answer here if you are interested to extend this to the chapters in the appendix. – Svalorzen Sep 15 '22 at 07:19
  • I had issues with the ToC breaking across odd places across a page when using \documentclass[report] and I had to change it to what's in the answer above \documentclass{scrreprt}. – dim_voly Dec 23 '22 at 11:13
  • There is LaTeX and there is KOMA. They have very little in common. (Beamer is worse.) Actually, a LaTeX solution may be easier. – John Kormylo Dec 23 '22 at 14:56