1

I came across a PDF which mutually links TOC entries and the headings present in the text body. It was created using Adobe Indesign. If I am reading the book and need to take a look at the TOC I can easily click the heading itself to see it in TOC. This will be a helpfull feature if content has many sub-headings. Are there any packages to achieve this functionality. Please guide.

  • 1
    Do yiu really need it ? IMo the bookmarks created by hyperref in conjonction with pdflatex do provide already similar fonctionality, don't they? – Jhor Nov 19 '18 at 07:22
  • Yes I need it. It will be helpfull to see larger picture when many sub-topics are discussed. We need to create a label each time for each subheading and refer to it, becomes messy when many headings are involved. I dont know whether a package exists for this purpose. – Ramaprakasha Nov 19 '18 at 07:57

1 Answers1

2

First, using etoolbox's \patchcmd, one can add to the TOC filling a command for the creation of an hypertarget :

\patchcmd{\addcontentsline}{{#2}{#3}{\thepage }}%
   {{#2}{#3}{\protect\hypertarget{back\@currentHref}{}\thepage }}%
   {\typeout{*patch success}}{\typeout{*patch failure}}

Here I have chosen to alter directly the \addcontentsline command as it is still called by the LaTeX \@startsection (actually \@sect) after its modification by hyperref. This enable to affect all the titles in the TOC.

Second, one has to add the hyperlink on the sectioning titles. In both article and book standard classes, this is achieved by redefinition of the \Sectionformat introduced in \@sect by hyperref:

\AtBeginDocument{\renewcommand{\Sectionformat}[2]%
    {\ifnum #2>\c@secnumdepth {#1}\else\hyperlink{back\@currentHref}{#1}\fi}}

Finally an MWE is:

% !TEX encoding = UTF-8
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{etoolbox}
\usepackage{blindtext}
\usepackage{hyperref}
\makeatletter
\patchcmd{\addcontentsline}{{#2}{#3}{\thepage }}%
   {{#2}{#3}{\protect\hypertarget{back\@currentHref}{}\thepage }}%
   {\typeout{*patch success}}{\typeout{*patch failure}}
\AtBeginDocument{\renewcommand{\Sectionformat}[2]%
   {\ifnum #2>\c@secnumdepth {#1}\else\hyperlink{back\@currentHref}{#1}\fi}}
\makeatother

\begin{document}
\tableofcontents
\clearpage
\Blinddocument
\Blinddocument
\Blinddocument
\Blinddocument
\Blinddocument
\Blinddocument
\end{document}

Of course the ugly link appearance can be improved with the suitable \hypersetup{...}

Some caveats :

  • This code is rather simple, but will likely conflict with any other classes or packages modifying titles and/or tableofcontents, namely the popular titlesec and titletoc packages, or etoc or tocloft, etc.
  • Nevertheless, it can surely be adapted to other situations, by looking as the content of the sectioning macro by using \meaning
  • In this spirit, the same procedure can be surely be used to modify the chapters in the book class, it will require to patch the \@makechapterhead macro.
  • There is still a small default: the backlink opens the TOC page but the upper visible line is not the linked one, but the one just followingit.

EDIT:

Following the lines of this question I have slightly changed the patchcmd of my answer to fix the 'small default' so that the backlinks now point to the appropriate lines. I also added the command to patch the \@makechapterhead as suggested in my initial answer. I finaly added the \hypersetup{...} that I was talking about.

The MWE becomes now :

    \documentclass{book}
    \usepackage[utf8]{inputenc}
    \usepackage{etoolbox}
    \usepackage{blindtext}
    \usepackage{hyperref}
    \makeatletter
    \patchcmd{\addcontentsline}{{#2}{#3}{\thepage }}%
       {{#2}{#3}{\protect\Hy@raisedlink{\protect\hypertarget{back\@currentHref}{}}{\thepage}} }%
       {\typeout{** patch \string\addcontentsline\space success}}%
       {\typeout{** patch \string\addcontentsline\space failure}}%  \AtBeginDocument{\renewcommand{\Sectionformat}[2]%
       {\ifnum #2>\c@secnumdepth {#1}\else\hyperlink{back\@currentHref}{#1}\fi}}
    \patchcmd{\@makechapterhead}{\bfseries #1\par}{\bfseries\hyperlink{back\@currentHref}{#1}\par}%
       {\typeout{** patch \string\@makechapterhead\space success}}%
       {\typeout{** patch \string\@makechapterhead\space failure}}
    \makeatother

    \hypersetup{colorlinks=true,linkcolor=blue}

    \begin{document}
    \tableofcontents
    \clearpage
    \Blinddocument
    \Blinddocument
    \Blinddocument
    \Blinddocument
    \Blinddocument
    \Blinddocument
    \end{document}

Of course, the \patchcmd{\@makechapterhead} must only be used f the class has \chapters.

I hope that this now a more accurate and efficient answer to yhe OP.

Jhor
  • 4,179
  • I have improved my answer to handle chapter and have better back links. Obviously, the limitation that this solution works out of the box only with standard classes remains unchanged. – Jhor Dec 14 '18 at 18:50