# Hyperref and ntheorem's thref: inconsistent numbering

When using ntheorem with the thref option to get the named reference command \thref, sometimes the theorem numbers produced is "different" from the theorem numbers produced by just \ref. Any suggestions on fixes/workarounds?

Here's a MWE:

\documentclass{book}
\usepackage{amsmath}
\usepackage[standard,amsmath,thref,hyperref]{ntheorem}
\usepackage{hyperref}

\makeatletter
\makeatother

\numberwithin{Theorem}{section}

\begin{document}
\part{Part 1}
\chapter{Chaptername}
\section{Test}
\begin{Theorem}\label{test1} Test theorem
\end{Theorem}

Cite with ref: \ref{test1}

Cite with thref: \thref{test1}
\end{document}


Which compiles to

Notice in particular that the \thref output shows Theorem 1.1.1.1, the first (extra) 1 belonging to the \part.

\documentclass{book}
\usepackage{amsmath}
\usepackage[standard,amsmath,thref]{ntheorem}

\makeatletter
\makeatother

\numberwithin{Theorem}{section}

\begin{document}
\part{Part 1}
\chapter{Chaptername}
\section{Test}
\begin{Theorem}\label{test1} Test theorem
\end{Theorem}

Cite with ref: \ref{test1}

Cite with thref: \thref{test1}
\end{document}


Without using hyperref, however, everything looks fine:

Remarks

Without the \@addtoreset{chapter}{part} line, the output is okay. However, I need the \@addtoreset{chapter}{part} because for this thing I am working on I want the chapter numbers to be within individual parts, and without explicitly setting this, hyperref fouls up and gets confused between equation 1.1.1 of part 1 and equation 1.1.1 of part 2.

-

I don't think I understand: how can your reader distinguish between Theorem 1.1.1 in Part 1 from Theorem 1.1.1 in Part 2?

The following hack seems to work, but, like every hack, it might have unexpected consequences.

\makeatletter
\def\thm@fmt@hyplabel@ii#1.#2.#3{#3}
\makeatother


In order to get correct hyperlinks, \thref doesn't use \theTheorem, but the more complex \theHTheorem, which is set up by hyperref. This representation of the internal counter HTheorem (which is synchronized with Theorem) must take into account the part number or wrong and duplicate links would be produced.

The hack consists in modifying the definition of \thm@fmt@hyplabel that's usually defined as \def\thm@fmt@hyplabel@ii#1.#2{#2} and put in front of the internal reference produced by hyperref: in the "normal" case this would be

Theorem.<chapter>.<section>.<theorem>


but with your setting it is

Theorem.<part>.<chapter>.<section>.<theorem>


Thus the normal definition of \thm@fmt@hyplabel is producing the part number, while you want the chapter number; hence the modified definition.

-
to distinguish Theorem 1.1.1 in Part 1 and Theorem 1.1.1 in Part 2, I am using smartref's \partref command. I have a macro set-up such that citing theorems in the same part of the book will generate "Theorem 1.1.1", whereas citing theorems in a different part of the book will generate "Part II, Theorem 1.1.1". Since there aren't very many cross-part references, this can help make the text slightly more readable. –  Willie Wong Feb 2 '12 at 11:35
Does the hack work with your macros? –  egreg Feb 2 '12 at 11:39
Looks like nothing broke! (Just tried both pdf and dvi versions.) So it looks like it works. Thanks. I will leave this open for a little bit just to get some eyes on it. Otherwise I'll accept this answer later today. –  Willie Wong Feb 2 '12 at 11:52
What I am confused about is why the difference introduced by hyperref? –  Willie Wong Feb 2 '12 at 11:54
Ah, thanks for the update. I see how it works now. –  Willie Wong Feb 2 '12 at 21:47