Linking the section text to the TOC
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}
This solution sets up \hypertargets
in the toc
by renewing the \contentsline
command. It links back to the toc
by setting the section headings as \hyperlinks
using the titlesec
package. Note that each of the new \hypertarget
start with the word 'toc'
You can easily copy this approach for chapters
etc.
\documentclass{article}
\usepackage{lipsum} % sample text
\usepackage[explicit]{titlesec} % to change headings
\usepackage{hyperref}
% renew \contentsline for toc to include hypertarget
\let\oldcontentsline\contentsline%
\renewcommand\contentsline[4]{%
\hypertarget{toc#4}{}%
\oldcontentsline{#1}{#2}{#3}{#4}}
% renew \section to link to the toc
\titleformat{\section}
{\normalfont\Large\bf}
{{\thesection} \hyperlink{tocsection.\thesection}{#1}}
{1pc}
{}
% renew \subsection to link to the toc
\titleformat{\subsection}
{\normalfont\bf}
{{\thesection} \hyperlink{tocsubsection.\thesubsection}{#1}}
{1pc}
{}
\begin{document}
\tableofcontents
\clearpage
\section{First section}
\lipsum[1]
\subsection{my sub section}
\lipsum[1]
\section{Second section}
\lipsum[2]
\clearpage
\section{Third section}
\lipsum[3]
\end{document}
Explanation
Note that if you look at the .toc
file for the above example you'll see the following entries
\contentsline {section}{\numberline {1}First section}{2}{section.1}
\contentsline {subsection}{\numberline {1.1}my sub section}{2}{subsection.1.1}
\contentsline {section}{\numberline {2}Second section}{2}{section.2}
\contentsline {section}{\numberline {3}Third section}{3}{section.3}
The \contentsline
takes four arguments, the fourth of which will uniquely identify each entry. I used this unique entry combined with the string 'toc' as the \hypertarget{toc#4}{}
; it would not work without the additional string 'toc' (or something similar), as it would result in a duplicated identifier
which would be ignored.
I wrote a very simple example of myself.
\documentclass{article}
\usepackage{lipsum}
\usepackage{hyperref}
\let\oldsection\section
\renewcommand\section[1]{%
\addtocontents{toc}{\protect\hypertarget{#1}{}}
\oldsection{\texorpdfstring{\protect\hyperlink{#1}{#1}}{#1}}
} %%
\let\oldsubsection\subsection
\renewcommand\subsection[1]{%
\addtocontents{toc}{\protect\hypertarget{#1}{}}
\oldsubsection{\texorpdfstring{\protect\hyperlink{#1}{#1}}{#1}}
} %%
\begin{document}
{
\let\section\oldsection
\pdfbookmark{Contents}{Contents}
\vspace*{.5cm}
\tableofcontents
}
\clearpage
\section{First section}
\lipsum[1]
\subsection{my sub section}
\lipsum[1]
\section{Second section}
\lipsum[2]
\clearpage
\section{Third section}
\lipsum[3]
\end{document}