# Linking the section text to the TOC

Any ideas? :)

(Somewhat related to this question)

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]
\titleformat{name=\chapter,numberless}
{\normalfont\huge\bfseries}{}{-20pt}{\Huge#1}
\titleformat{\section}
\titleformat{name=\section,numberless}
{\normalfont\Large\bfseries}{}{0pt}{#1}
\titleformat{\subsection}
\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}

• Your solution indeed does work like a charm :) Still, I'm having problems trying to make it play nicely with the rest of my manuscript. I've chased the error down to the following line \renewcommand{\thechapter}{\texorpdfstring{\fontfamily{cmr}\selectfont\arabic{chapter}}{\arabic{chapter}}}, which basically gives me titling numbers in the sectionings (see tex.stackexchange.com/questions/31942/…). Should I mark your answer as accepted (it does work)? do you have any ideas how to make both things "compatible"? – mpr Oct 24 '11 at 12:26
• @mpr: see my updated answer. I redefined the font family for the chapter counter inside \titleformat, so there's no need to use the problematic \renewcommand\thechapter{...}. – Gonzalo Medina Oct 24 '11 at 17:12
• It works like a charm in the MWE, but in my manuscript I get a gazillion warnings along the lines of "destination with the same identifier (num2) has been already used, dupicate ignored"... I even included all of the packages / commands I use in my manuscript into the MWE, it seems to me that it's a matter of page numbering or something... I modified your commands slightly, for example: \hyperlink{subsec-\thechapter-\thesection-\thesubsection}{#1} and hypertarget{subsec-\thechapter-\thesection-\thesubsection}{} – mpr Oct 24 '11 at 18:11
• @mpr: then you will have to provide a minimal version of your code. You can edit your original question to add this minimal version. – Gonzalo Medina Oct 24 '11 at 19:01
• I'm very sorry for all the back-n-forth! I found that the problem are * chapters / sections / subsections / etc. The MWE you propose fails with a duplicate warning. – mpr Oct 24 '11 at 19:33

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{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}
{1pc}
{}

% renew \subsection to link to the toc
\titleformat{\subsection}
{\normalfont\bf}
{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]{%
} %%

\let\oldsubsection\subsection
\renewcommand\subsection[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}