4

When I use the titlesec and hyperref packages in the same document, I get a warning when I try to add subsection titles to the ToC with this code:

\subsection*{This is subsection}
\addcontentsline{toc}{subsection}{This is a subsection}

The warning states:

Package hyperref Warning: The anchor of a bookmark and its parent's must not be the same. Added a new anchor on input line 12.

This warning only appears when subsections are added to the ToC in this way, not sections. Line 12 is where the subsection is added. Here is a minimal working code showing the isse (live demo here); the section part is not triggering the warning, only the subsection parts:

\documentclass[11pt,a4paper]{article}

\usepackage{titlesec}
\usepackage{hyperref}

\begin{document}

\section*{This is a section}
\addcontentsline{toc}{section}{This is a section}

\subsection*{This is subsection 1}
\addcontentsline{toc}{subsection}{This is a subsection 1}

\subsection*{This is subsection 2}
\addcontentsline{toc}{subsection}{This is a subsection 2}

\end{document}

I have found out that removing any of the titlesec or hyperref packages removes the warning. How can this conflict between the packages be fixed and the warning removed?

  • titlesec changes many things internally considering the section commands and as such, the hyper anchor commands are wrong. You need a \phantomsection before each manual addition – user31729 Apr 10 '17 at 8:50
3

Use a \phantomsection before each manual addition of a \subsection* to the ToC (and bookmarks) in order to reestablish the hyper anchors.

\documentclass[11pt,a4paper]{article}

\usepackage{titlesec}
\usepackage{hyperref}

\begin{document}

\section*{This is a section}
\addcontentsline{toc}{section}{This is a section}

\clearpage
\phantomsection
\subsection*{This is subsection 1}
\addcontentsline{toc}{subsection}{This is a subsection 1}

\clearpage
\phantomsection
\subsection*{This is subsection 2}
\addcontentsline{toc}{subsection}{This is a subsection 2}

\end{document}
1

You can also include, in the preamble:

\newcommand{\sectionbreak}{\phantomsection}

This does not seem to mess up numbered sections, and applies consistency throughout the document. You can also throw a \clearpage in the definition to get your "sections start on a new page" automatically.

0

With a similar problem and a complex preamble, and also having a couple of \addcontentslines, I've found a simple solution in making sure that hyperref is loaded after titlesec, as actually recommended in hyperref's manual.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.