\documentclass[a4paper,12pt]{book}
\usepackage[hidelinks]{hyperref}
\begin{document}
\tableofcontents
\newpage
\addcontentsline{toc}{section}{from there}
no text
\newpage
\addcontentsline{toc}{section}{to here}
some text
\newpage
\addcontentsline{toc}{subsection}{and here}
still more text
\newpage
\addcontentsline{toc}{subsection}{not here}
or maybe less text
\newpage
\addcontentsline{toc}{section}{also here}
and more text
\newpage
\addcontentsline{toc}{subsection}{maybe here}
and still more text
\end{document}
1 Answer
\addcontentsline
does not create an anchor, it uses the anchor, that is created before, even if the anchor belongs to one of the previous pages. This is by design, it allows, e.g.:
\section*{Introduction}
\addcontentsline{toc}{section}{Introduction}
If \addcontentsline
would create an anchor, then it would point below the section title.
The solution here is to use \phantomsection
, which creates the needed anchor:
\newpage
\phantomsection
\addcontentsline ...
\addcontentsline
here at all? Let\section
etc. do this job and the links anchors and positions are correct!