# How to make hyperref destination names unique, to make TOC links work?

The circumstances of this question are somewhat unlike the results I got from searching the error message on my own.

[20]LuaTeX warning (ext4): destin
ation with the same identifier (name{subsection.1.2.1}) has been already used,
duplicate ignored


Basically, I want duplicate section names, and the error is... not erroneous, I just need a way to make the destination names unique, so the TOC clicky links work.

Here's the MWE which should explain what is going on.

% main.tex

\documentclass[12pt]{article}

\usepackage{tocloft}
\usepackage{regexpatch}
\usepackage{etoolbox}
\usepackage[newparttoc]{titlesec}

\usepackage[pdfencoding=auto,plainpages=false,pdfpagelabels,psdextra]{hyperref}
\usepackage{bookmark}
\usepackage{fontspec, xunicode, microtype}

\makeatletter
\def\@seccntformat#1{\protect\makebox[0pt][r]{\csname
\makeatother

\makeatletter
\patchcmd{\@part}% <cmd>
{\markboth{}{}}% <search>
{\partmark{#1}}% <replace>
{}{}% <success><failure>
\makeatother

\makeatletter
\makeatother

\cftpagenumbersoff{part}

\title{Test}

\begin{document}
\pagenumbering{gobble}

% titlepage

\begin{center}

A Title

\end{center}

% end title page

\pagebreak
\tableofcontents
\pagebreak

\hypersetup{pageanchor=true}
\pagenumbering{arabic}

% would be \input{"test1"}, but it doesn't matter

\pagebreak
\part{Part One}
\section{To Slice a Hotdog}
\subsection{Operating a Knife}
Foo Bar.

\renewcommand{\thepart}{\Alph{part}}

\pagebreak
\pagebreak

\setcounter{part}{0}
% would be \input{"addendums"}, but again, doesn't change things

\pagebreak
\section{To Blend a Hotdog}
\subsection{Operating a Blender}
Something something.

\end{document}


I'm aware that the reason I get these errors is due to me resetting the part number counter. The document I have has the first part with sections numbered numerically, ie. 1-9, etc. The next part, which is sort of an appendix but more of an addendum, is ordered A-Z. Hence I reset the counter and set the numbering to \Alph.

Of course, if there is a better way do accomplish what I want to achieve without resetting the counter and getting all these problems, that works too.

The appearance of those in the TOC is fine, but clicking on the item ever only brings me to the first section, which is understandable, of course. That's what I'm trying to solve.

From what I've gathered, I need to attack this macro: \HyperDestNameFilter. I've lost the original StackOverflow answer that recommended this, but I first tried \renewcommand*{\HyperDestNameFilter}[1]{\jobname-#1}, only to realise that since my files are \inputed into a main file, the jobname will be the same.

Then, I tried using \renewcommand*{\HyperDestNameFilter}[1]{\thepage-#1}. On first glance, that seems to work, in that I don't get the error anymore.

However, upon compiling all the pages, I get a whole bunch, and I mean a whole bunch, of these:

LuaTeX warning (dest): name{-subsubsection.1.3.3.3} has been referenced but does
not exist, replaced by a fixed one

LuaTeX warning (dest): name{-subsubsection.1.3.3.2} has been referenced but does
not exist, replaced by a fixed one

LuaTeX warning (dest): name{-subsubsection.1.3.3.1} has been referenced but does
not exist, replaced by a fixed one

LuaTeX warning (dest): name{-subsection.1.3.3} has been referenced but does not
exist, replaced by a fixed one

LuaTeX warning (dest): name{-subsection.1.3.2} has been referenced but does not
exist, replaced by a fixed one


It goes on and on, and I think it's for every section, subsection, subsubsection, everything. Now, all the headings in my TOC simply bring me to the last page when clicked. All the links become broken. Help?

TL;DR, then: how do I make it so that I get unique destination names, so my TOC links will work properly even when I have identical section headings with the same number?

Thanks so much.

• What shall we do with this list of packages? Most of them is completely unrelated to the problem.The last resort method is hypertexnames=false as an option to hyperref or hypersetup, but you have not shown the real cause of the problem! – user31729 Apr 20 '16 at 15:10
• The cause are identical section etc. numbers -- this confuses hyperref. There's a remedy, however ;-) Just show the compilable code that produces the wrong links, that should be enough. – user31729 Apr 20 '16 at 15:13
• The error is not about duplicate titles, it doesn't matter that your section title "To Slice a Hotdog" appears twice. You have two subsection numbered 1.2.1 and you need to show us how you did this. – Ulrike Fischer Apr 20 '16 at 15:19
• This is not a proper MWE, it is a bunch of snippets. Why are you resetting the part counter? – Ulrike Fischer Apr 20 '16 at 15:21
• No I can't reproduce the issue with your snippet as I can't compile the snippet. And I won't spent my time to try to build a complete document. It is your problem so you will have to do the work to setup a sensible test document. – Ulrike Fischer Apr 20 '16 at 16:08

Resetting counters of the sectioning structure by hand must confuse hyperref since it generates anchors from the counter values.

E.g. a anchor name could be part.1.

However, this will appear again for the Addendum part, even if \thepart is redefined to be \Alph{part} -- the anchor name is generated of a special command named \theHpart etc. If this gets a new prefix, say addendum all should get well.

Thus

\renewcommand{\theHpart}{addendum.\thepart}

should provide the unique names.

\documentclass[12pt]{article}

\usepackage{tocloft}

\usepackage{etoolbox}
\usepackage[newparttoc]{titlesec}
\usepackage{fontspec, microtype}
\usepackage[pdfencoding=auto,plainpages=false,pdfpagelabels,psdextra]{hyperref}
\usepackage{bookmark}

\makeatletter
\def\@seccntformat#1{\protect\makebox[0pt][r]{\csname
\makeatother

\makeatletter
\patchcmd{\@part}% <cmd>
{\markboth{}{}}% <search>
{\partmark{#1}}% <replace>
{}{}% <success><failure>
\makeatother

\makeatletter
\makeatother

\cftpagenumbersoff{part}

\title{Test}

\begin{document}
\pagenumbering{gobble}

% titlepage

\begin{center}

A Title

\end{center}

% end title page

\pagebreak
\tableofcontents
\pagebreak

\hypersetup{pageanchor=true}
\pagenumbering{arabic}

% would be \input{"test1"}, but it doesn't matter

\pagebreak
\part{Part One}
\section{To Slice a Hotdog}
\subsection{Operating a Knife}
Foo Bar.

\renewcommand{\thepart}{\Alph{part}}

\pagebreak
\pagebreak

\setcounter{part}{0}
% would be \input{"addendums"}, but again, doesn't change things

\pagebreak

• Alright this worked, thanks. Didn't see \theHpart mentioned in my searches, didn't think to look at that. – zhiayang Apr 21 '16 at 0:38
• I'm not sure why you're loading regexpatch – egreg Apr 21 '16 at 7:49
• @ChristianHupfer I see; well it's not needed; and xunicode should not be loaded. – egreg Apr 21 '16 at 8:02