# Hyperref and titlesec conflict and warning

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}


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}

\subsection*{This is subsection 1}

\subsection*{This is 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

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}

\clearpage
\phantomsection
\subsection*{This is subsection 1}

\clearpage
\phantomsection
\subsection*{This is subsection 2}

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