7

I want to have hyperlinks pointing to the ToC page. (In my MWE, the links are situated in the section titles, but it makes no difference for the discussion here). My MWE:

\documentclass{article}
\usepackage{hyperref}
\usepackage{blindtext}

\let\oldtableofcontents\tableofcontents
\renewcommand{\tableofcontents}[0]{\label{toc}\oldtableofcontents}

\begin{document}
First page\newpage
\tableofcontents\newpage

\section{Premier. Go to \hyperref[toc]{toc}}
\blindtext  \blindtext  
\section{deuxieme. Go to \hyperref[toc]{toc}}
\blindtext  \blindtext  
\section{Troisieme. Go to \hyperref[toc]{toc}}
\blindtext  \blindtext  
\end{document}

It works only partially: the links are there, but if I click on them, it goes to the "first page", instead of the ToC page. I redefined \tableofcontents, in order to put a label juste before \tableofcontents, but I guess this is the problem. In the original command \tableofcontents, the must be at the beginning some initialisation, like \newpage or similar, which makes the problem.

Thorsten
  • 12,872
Loic Rosnay
  • 8,167

1 Answers1

7

Use \hypertarget and \hyperlink for placing targets and linking to them. You can use \addtocontents to write the \hypertarget into the .toc file.

\documentclass{article}
\usepackage{hyperref}
\usepackage[english]{babel}
\usepackage{blindtext}
\begin{document}
First page\newpage
\addtocontents{toc}{\protect\hypertarget{toc}{}}
\tableofcontents\newpage
\section[Premier]{Premier. Go to \hyperlink{toc}{toc}}
\blindtext  \blindtext  
\section[Deuxieme]{Deuxieme. Go to \hyperlink{toc}{toc}}
\blindtext  \blindtext  
\section[Troisieme]{Troisieme. Go to \hyperlink{toc}{toc}}
\blindtext  \blindtext  
\end{document}

Or, even better, place \hypertarget directly before \tableofcontents, but make \clearpage before.

\clearpage
\hypertarget{toc}{}
\tableofcontents

This ensures to link above the contents heading. The \addtocontents way places the target below the heading.

Stefan Kottwitz
  • 231,401