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'm using the hyperref package to create hyperlinks in my document. The links from the TOC->chapters/sections work great, but I was wondering if there was a way to automatically insert hyperlinks from section titles BACK to the TOC (section->TOC). For easier two-way navigation (especially when clicking on an incorrect link in the TOC).

In case it matters I'm working in the report class, and this is a (minimized) version of what I have now.

\documentclass[11pt,a4paper]{report}
\usepackage{hyperref}

\begin{document}
First page

\clearpage
\tableofcontents
\clearpage

Blank page

\clearpage
\chapter{First chapter}
\end{document}

Thanks for any help!

share|improve this question
add comment

2 Answers

Here's one possible solution using the \hyperlink, \hypertarget mechanism; \hyperlinks are conveniently added using a redefinition of \@makechapterhead and a redefinition of \@chapter was used to place the corresponding \hypertargets:

\documentclass{report}
\usepackage{hyperref}

\makeatletter
\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
  \refstepcounter{chapter}%
  \typeout{\@chapapp\space\thechapter.}%
  \addcontentsline{toc}{chapter}%
    {\protect\numberline{\thechapter}\string\hypertarget{chap\thechapter}{#1}}%
\else
  \addcontentsline{toc}{chapter}{#1}%
\fi
\chaptermark{#1}%
\addtocontents{lof}{\protect\addvspace{10\p@}}%
\addtocontents{lot}{\protect\addvspace{10\p@}}%
\if@twocolumn
  \@topnewpage[\@makechapterhead{#2}]%
\else
  \@makechapterhead{#2}%
  \@afterheading
\fi}
\def\@makechapterhead#1{%
  \vspace*{50\p@}%
  {\parindent \z@ \raggedright \normalfont
    \ifnum \c@secnumdepth >\m@ne
        \huge\bfseries \@chapapp\space \thechapter
        \par\nobreak
        \vskip 20\p@
    \fi
    \interlinepenalty\@M
    \Huge \bfseries \hyperlink{chap\thechapter}{#1}\par\nobreak
    \vskip 40\p@
  }}
\makeatother

\begin{document}

\tableofcontents
\chapter{Test Chapter One}
\chapter{Test Chapter Two}
\chapter{Test Chapter Three}

\end{document}

Here's the same idea using this time the titlesec package with its explicit option to place the \hyperlinks:

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

\titleformat{\chapter}[display]
  {\normalfont\huge\bfseries}{\chaptertitlename\ \thechapter}{20pt}  
  {\Huge\hyperlink{chap\thechapter}{#1}}
\titleformat{name=\chapter,numberless}[display]
  {\normalfont\huge\bfseries}{}{-22pt}  
  {\Huge#1}

\makeatletter
\def\@chapter[#1]#2{\ifnum \c@secnumdepth >\m@ne
  \refstepcounter{chapter}%
  \typeout{\@chapapp\space\thechapter.}%
  \addcontentsline{toc}{chapter}%
    {\protect\numberline{\thechapter}\string\hypertarget{chap\thechapter}{#1}}%
\else
  \addcontentsline{toc}{chapter}{#1}%
\fi
\chaptermark{#1}%
\addtocontents{lof}{\protect\addvspace{10\p@}}%
\addtocontents{lot}{\protect\addvspace{10\p@}}%
\if@twocolumn
  \@topnewpage[\@makechapterhead{#2}]%
\else
  \@makechapterhead{#2}%
  \@afterheading
\fi}
\makeatother

\begin{document}

\tableofcontents
\chapter{Test Chapter One}
\chapter{Test Chapter Two}
\chapter{Test Chapter Three}

\end{document}
share|improve this answer
add comment

use \hypertarget

\documentclass{book}
\usepackage{hyperref}

\begin{document}    
\tableofcontents

\chapter{\protect\hyperlink{chap:\thechapter}{My chapter}}
\addtocontents{toc}{\protect\hypertarget{chap:\thechapter}{}}

\chapter{\protect\hyperlink{chap:\thechapter}{My chapter}}
\addtocontents{toc}{\protect\hypertarget{chap:\thechapter}{}}

foo     
\end{document}
share|improve this answer
add comment

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.