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 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.

share|improve this question
2  
You need a reference point. Therefore you can use \phantomsection or hyperrlink. Use \phantomsection\label{toc}. –  Marco Daniel Feb 24 '12 at 9:52
    
ok, i think i understand. I thought \label create a reference point, but actually, it can only refer to a section, and not to any point in the text, am i right ? –  nicolas roy Feb 24 '12 at 10:12
1  
linking-the-section-text-to-the-toc –  cmhughes Feb 24 '12 at 17:07
add comment

1 Answer

up vote 6 down vote accepted

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.

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.