0

long but accurate title. When I include the titlesec package (even without really using it), and when a section start at the top of a new page, the table of contents links to the previous one. Here is a MWE. When clicking on "Section Two" in the toc, I arrive to the previous page (Section One). If I remove \usepackage{titlesec} OR if I add/remove several sentences in Section One to make Section Two not start at the top, the problem disappear.

\documentclass[a4paper,11pt]{article}
\usepackage{hyperref}

\usepackage{titlesec}

\begin{document}

\tableofcontents

\newpage

\section{Section One}

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est.

\section{Section Two}

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam

\end{document}

  • 3
    Welcome to TeX.SX! In general and with few exceptions, \usepackage{hyperref} must be loaded last in your preamble, because hyperref has to adjust itself to all your other packages. This is all the more true with a package like titlesec, which redefines commands such as \section. In your example, after hyperref does its work, titlesec undoes it to some extent. – Try loading \usepackage{titlesec} \usepackage{hyperref} in this order and see if the problem disappears. – marquinho Mar 24 '22 at 16:11
  • perfect ! thanks a lot, it indeed solved my problem. I dont know how I can accept your answer and flag my question "solved' or something like that ? – Seb Gege Mar 24 '22 at 17:29
  • You ask @marquinho to create an answer – daleif Mar 24 '22 at 17:32

1 Answers1

1

Your issue is a prime example of why hyperref should be the last of your loaded packages in the preamble,

to give it a fighting chance of not being over-written, since its job is to redefine many LATEX commands

according to the documentation.

hyperref redefines LaTeX internals and macros such as \section, \footnote and other things that should perform as hyperlinks or hypertargets. For this to work, hyperref has to adjust itself to all other packages that may have previously redefined those same internals.

titlesec is one of these packages: it alters the appearance and function of sectioning commands such as \section via a series of redefinitions. The definitions in titlesec have the potential to overwrite those made by hyperref, effectively undoing some of the work done by it – which results in a loss of hypertext functionality.

To avoid this, hyperref must be loaded after titlesec (and similar packages). Reversing the order of the two packages in the preamble solves your problem:

\documentclass[a4paper,11pt]{article}

\usepackage{titlesec} \usepackage{hyperref}

\begin{document}

There are but a few exceptions to this rule: see Which packages should be loaded after hyperref instead of before?

marquinho
  • 1,721