3
\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
  • Why do you use \addcontentsline here at all? Let \section etc. do this job and the links anchors and positions are correct!
    – user31729
    Sep 6, 2017 at 6:34

1 Answer 1

6

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .