[Tex/LaTex] Linking the section text to the TOC

hyperreflinkssectioningtable of contents

I'd like the section headings in running text to link to their "line" in the Table of Contents so as to get "two-way" navigation, eg: a user looks in the Table of Contents for a section title, jumps there, reads a couple of lines, clicks on the section title again and gets taken to the section's entry in the Table of Contents once more.

Any ideas? 🙂

(Somewhat related to this question)

Best Answer

Here's another approach, using the titlesec package and the \hypertarget, \hyperlink mechanism from hyperref. A little example showing the necessary settings for \chapter, \section, \subsection and their starred versions:

\documentclass{book}
\usepackage[explicit]{titlesec}
\usepackage{hyperref}

\titleformat{\chapter}[display]
  {\normalfont\huge\bfseries}{\chaptertitlename\ {\fontfamily{cmr}\selectfont\thechapter}}{20pt}{\hyperlink{chap-\thechapter}{\Huge#1}
\addtocontents{toc}{\protect\hypertarget{chap-\thechapter}{}}}
\titleformat{name=\chapter,numberless}
  {\normalfont\huge\bfseries}{}{-20pt}{\Huge#1}
\titleformat{\section}
  {\normalfont\Large\bfseries}{\thesection}{1em}{\hyperlink{sec-\thesection}{#1}
\addtocontents{toc}{\protect\hypertarget{sec-\thesection}{}}}
\titleformat{name=\section,numberless}
  {\normalfont\Large\bfseries}{}{0pt}{#1}
\titleformat{\subsection}
  {\normalfont\large\bfseries}{\thesubsection}{1em}{\hyperlink{subsec-\thesubsection}{#1}
\addtocontents{toc}{\protect\hypertarget{subsec-\thesubsection}{}}}
\titleformat{name=\subsection,numberless}
  {\normalfont\large\bfseries}{\thesubsection}{0pt}{#1}

\begin{document}

\tableofcontents

\chapter{Numbered test chapter one}

\section{Test section one}

\subsection{test subsection one one}

\subsection{test subsection one two}

\chapter*{Unnumbered test chapter one}

\section*{Unnumbered test section}

\subsection*{Unnumbered test subsection}

\end{document}
Related Question