Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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)

share|improve this question

2 Answers 2

up vote 8 down vote accepted

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}
share|improve this answer
    
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{c‌​hapter}}{\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[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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.