# problems with hyperref when \thethm points to undefined reference

Consider the following three minimal examples. I do not understand why the first doesn't work while the second and third is fine. Can anyone tell me? Hyperref seems to have problems when \thethm is pointing to an undefined reference. Please don't ask why pointing to an undefined reference makes sense. I do have a good reason for that but it has nothing to do with this question.

First example (produces strange error):

\documentclass{article}

\usepackage[thmmarks,hyperref]{ntheorem}
\usepackage{hyperref}

\newtheorem{thm}{Theorem}
\newtheorem{prop}[thm]{Proposition}

\begin{document}
\begingroup
\def\thethm{\ref{a}'}
\addtocounter{thm}{-1}
\begin{prop}
yyy
\label{test}
\end{prop}
\endgroup
\end{document}


Second example, hyperref removed, otherwise identical code (works fine):

\documentclass{article}

\usepackage[thmmarks]{ntheorem}

\newtheorem{thm}{Theorem}
\newtheorem{prop}[thm]{Proposition}

\begin{document}
\begingroup
\def\thethm{\ref{a}'}
\addtocounter{thm}{-1}
\begin{prop}
yyy
\label{test}
\end{prop}
\endgroup
\end{document}


Third example, code as in the first example but \thethm is changed (also works well):

\documentclass{article}

\usepackage[thmmarks,hyperref]{ntheorem}
\usepackage{hyperref}

\newtheorem{thm}{Theorem}
\newtheorem{prop}[thm]{Proposition}

\begin{document}
\begingroup
\def\thethm{1'}
\addtocounter{thm}{-1}
\begin{prop}
yyy
\label{test}
\end{prop}
\endgroup
\end{document}

-

## 2 Answers

I have no problem with this document:

\documentclass{article}

\usepackage{refcount}
\usepackage[thmmarks,hyperref]{ntheorem}
\usepackage{hyperref}

\newtheorem{thm}{Theorem}
\newtheorem{prop}[thm]{Proposition}

\theoremstyle{nonumberplain}

\newcommand\specialref{}

\newtheorem{unnumbered}{\specialref}
\newenvironment{thmref}[2][$'$]
{\edef\theunnumbered{\getrefnumber{#2}\unexpanded{#1}}%
\renewcommand\specialref{Theorem \ref{#2}#1}\unnumbered}
{\endunnumbered}

\newenvironment{propref}[2][$'$]
{\edef\theunnumbered{\getrefnumber{#2}\unexpanded{#1}}%
\renewcommand\specialref{Proposition \ref{#2}#1}\unnumbered}
{\endunnumbered}

\begin{document}
\begin{prop}\label{a}
aaa
\end{prop}

\begin{propref}{a}\label{test}
yyy
\end{propref}

\ref{test}
\end{document}


Changing the meaning of \thethm with hyperref active is not recommendable.

-
Yes, that works. Problem is I do not really understand the code. What does \edef, \thepropx, \unexpanded, mean for example ... –  lpdbw Jul 24 '12 at 8:22
@lpdbw Instead of doing "manually", I define two unnumbered theorems (for theorems and propositions) where the label is dynamically modified as needed. If you have other needs (using a shade behind the theorem, for instance) you should make it clear in the question. –  egreg Jul 24 '12 at 8:25
Well, I do manage to adjust your code so that I can have shades, leftbars etc. I just don't understand the meaning of what comes after \edef. And why do you need \newtheorem only once ... This was different before your edit ... in general, what was wrong with the code before the edit (it worked fine)? –  lpdbw Jul 24 '12 at 8:42
I now notice that if you replace \begin{propref}{a} by \begin{propref}{\ref{abc}}, where \ref{abc} is an undefined reference, there is an error. So in fact your answer doesn't really solve the original problem with undefined references! –  lpdbw Jul 24 '12 at 8:53
@lpdbw The argument should be only the label, not \ref{label}; \begin{propref}{abc} works also if abc is not defined (with warning, of course). I changed the code because it's not necessary to define one unnumbered theorem for each type: it's sufficient to suitably define \specialref in the two \newenvironment commands. –  egreg Jul 24 '12 at 9:00

Using unexpandable stuff apart from characters inside \the<counter> is a little risky. The number could be used in many contexts and might break in expandable context like here. The solution below uses package refcount to extract the reference data in an expandable way. Outside \refused notifies LaTeX about the reference usage and LaTeX can report undefined or changed references. The link is stripped.

The counter is decremented that means there are two theorems with the same number and therefore the same destination name. This can be resolved by modifying \theHthm that hyperref uses instead of \thethm for the destination name.

  \refused{a}%
\edef\thethm{\getrefbykeydefault{a}{}{?}'}%
\edef\theHthm{\theHthm'}%


The complete example:

\documentclass{article}

\usepackage[thmmarks,hyperref]{ntheorem}
\usepackage{hyperref}
\usepackage{refcount}

\newtheorem{thm}{Theorem}
\newtheorem{prop}[thm]{Proposition}

\begin{document}
\begin{prop}\label{a}
Proposition with label a.
\end{prop}
\begingroup
\refused{a}%
\edef\thethm{\getrefbykeydefault{a}{}{?}'}%
\edef\theHthm{\theHthm'}%
\addtocounter{thm}{-1}%
\begin{prop}
yyy
\label{test}
\end{prop}
\endgroup
\end{document}

-