[Tex/LaTex] Hyperref and ntheorem’s thref: inconsistent numbering

hyperrefincompatibilityntheoremtheorems

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
\@addtoreset{chapter}{part}
\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

Buggy output

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
\@addtoreset{chapter}{part}
\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:

Output that I want


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.

Best Answer

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
\@addtoreset{chapter}{part}
\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.

Related Question