# hyperref weirdness with simple custom package

Trying to retroactively get hyperref working for my thesis.

Fun.

Encountered the following weirdness... I have the following (hard earned) minimal-ish working example:

\documentclass{article}

\usepackage{blindtext}
% LINE 0 BELOW
% \usepackage{mypackage}
\usepackage{hyperref}

% LINE 1 BELOW
\newtheorem{definition}{Definition}[section]

\begin{document}

\section{Section One}
See Definition~\ref{def:one}.

\begin{definition}\label{def:one}
\blindtext
\end{definition}

\Blindtext

\section{Section Two}
When using external \texttt{mypackage}, Definition~\ref{def:two} \texttt{hyperref}'s to Definition~\ref{def:one}---otherwise it's fine.

\begin{definition}\label{def:two}
\blindtext
\end{definition}

\end{document}


Which works fine after a couple of builds. However, if I put the newtheorem command (LINE 1) into a new custom mypackage.sty as follows:

\ProvidesPackage{mypackage}

\newtheorem{definition}{Definition}[section]


...stick it in the same directory as the main file, and toggle the comments on LINE 0 & 1 in the main file, after a couple of builds I get a warning:

pdfTex warning (ext4): destination with the same identifier (name{definition.1}) has already been used, duplicate ignored

Also, in the resulting PDF, the label for the second Definition links to the first one.

Now, I have my fix (move the newtheorem declaration into my main .tex file)...

...but I've been held hostage by hyperref for so long this time, I've developed Stockholm syndrome. (Looking back at my history with this package, it was only a matter of time.)

Can someone give me some closure and explain why hyperref cares where I put my newthereom declaration?

Otherwise, can someone point me in the direction of a hyperref victim support group?

• – badroit Mar 10 '11 at 2:46
• See in the readme.pdf of hyperref section 8 "Subordinate counters" for an explanation and solutions. You can use \AtBeginDocument to move the \newtheorem behind hyperref in your package. – Ulrike Fischer Mar 10 '11 at 8:51

hyperref needs to patch many LaTeX commands (and commands offered by other packages) to make proper hyper links happen, and one of them is \newtheorem. So if you include your package before the hyperref package, the \newtheorem inside your package will use the original definition by LaTeX and not the patched one by hyperref.
Another example: The float package offers \newfloat, and this one needs to be patched by hyperref. So wenn using \newfloat, one has to include the float package first (so \newfloat gets defined), the hyperref package afterwards (so \newfloat gets patched), and finally one can use the (patched) \newfloat command.
• Usually I add hyperref as a last step for putting a document online... it's always a painful process, mainly because I never took the time to learn what hyperref actually does. This answer will help me avoid such pain in future. Many thanks for that! – badroit Mar 10 '11 at 20:47