I'm trying to create links from my table of contents to the corresponding sections using hyperref.
However, I've noticed that if the first entry is an unnumbered section (\section*
), the link just brings me to the table of contents.
Here's a small example to illustrate what I'm trying to say. Note that the second unnumbered section works while the first does not. Also, using a numbered section as the first entry would fix the problem.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{hyperref}
\hypersetup{
colorlinks=true,
linktoc=all,
linkcolor=black,
}
\begin{document}
\tableofcontents
\newpage
\addcontentsline{toc}{section}{Unnumbered section 1}
\section*{Unnumbered section 1}
\addcontentsline{toc}{section}{Unnumbered section 2}
\section*{Unnumbered section 2}
\end{document}
How can I have the first link so that it targets the correct page?
Best Answer
I found out that using
\addcontentsline
just after\section*
fixes the problem.So the corrected code would be
As pointed out by @daleif, you should add
\phantomsection
before\addcontentsline
if you use hyperref.So just like