# How does the Hyperref package affect on regular \ref-command to break this code

I write my thesis in LaTeX and refer to the research questions in subsequent chapters. I label the questions and designed the \nthref (se code below) to refer to the nth research question by its label.

My problem is that if my code includes the hyperref-package, which I also like, my \nthref code breaks down. It seems like when hyperref is used, \ref{} still inserts a number, but then again not really. I can no longer get an \ifx\ref{#1}=1 statement to trigger even if the \ref in question becomes a 1. I'm confused, because in the rendered pdf the 1 looks identical. Shouldn't \ref{q:whatever} always replace with the same thing? Confusion!

My question is what hyperref really does to the \ref-command in order to break my code in the example below. Removing \usepackage{hyperref} of course is a solution, but not one I'd prefer, mostly because I wouldn't understand why I'd have to do it.

\documentclass[11pt,twoside]{book}
\usepackage[utf8]{inputenc}

% Hyperref destroys \nthref below
\usepackage{hyperref}

% let nthref give an ordinal word for the reference to the label of of an enumeric list environment - like research questions
\newcommand\nthref[1]{\ifcase\ref{#1}???\or{first}\or{second}\or{third}\or{fourth}\or{fifth}\else???\fi}

\begin{document}
There are research questions like:
\begin{enumerate}
\label{sec:questions}
\item What is a sunset?\label{q:sunset}
\item Can sunsets be modelled?\label{q:model_sunset}
\item How many sunsets are there in two pineapples?\label{q:pinaple_relation}
\end{enumerate}
% Research questions are labeled as:
%   q:sunset
%   q:model_sunset
%   q:pinaple_relation

In research question \ref{q:sunset} (the \nthref{q:sunset} research question) sunsets are considered. Sunsets remain the themes of the \nthref{q:model_sunset} and \nthref{q:pinaple_relation} questions.

\end{document}

• hyperref makes \ref unexpandable, that's the reason – user31729 Sep 22 '17 at 14:36
• I would expect that the first run of the document should produce errors – user31729 Sep 22 '17 at 14:43
• I'm surprised it ever worked. \ref has always been protected (cannot be expanded until printed). It depends on how \ifcase is implemented. OTOH, hyperref turns \ref into a \hyperlink, not just a macro. – John Kormylo Sep 22 '17 at 15:07
• @JohnKormylo I am not sure what you mean, \refis not protected and can definitely be expanded, the problem is that it inserts a \null token which expands to \hbox{} so it can not be used in numerical context as is. – user4686 Sep 22 '17 at 16:10
• (independently of the fact that \thesection etc... do not alway produce numerical things to start with, which is what \label will store) – user4686 Sep 22 '17 at 16:16

The \hyperref version of \ref is not expandable, i.e. it can't be used in a \ifcase etc. test. directly in order to make a query on its value.

\hyperref's \ref command is a protected version and also provides a \ref* in order to omit hyperlinks.

However, \getrefnumber which is a macro from refcount package (which is included by hyperref anyway) can be used instead. If a reference is not found, -1 is returned.

\documentclass[11pt,twoside]{book}
\usepackage[utf8]{inputenc}

% Hyperref destroys \nthref below
\usepackage{hyperref}

% let nthref give an ordinal word for the reference to the label of of an enumeric list environment - like research questions
\newcommand\nthref[1]{\ifcase\getrefnumber{#1}???\or{first}\or{second}\or{third}\or{fourth}\or{fifth}\else???\fi}

\begin{document}

There are research questions like:
\begin{enumerate}
\label{sec:questions}
\item What is a sunset?\label{q:sunset}
\item Can sunsets be modelled?\label{q:model_sunset}
\item How many sunsets are there in two pineapples?\label{q:pinaple_relation}
\end{enumerate}
% Research questions are labeled as:
%   q:sunset
%   q:model_sunset
%   q:pinaple_relation

In research question \ref{q:sunset} (the \nthref{q:sunset} research question) sunsets are considered. Sunsets remain the themes of the \nthref{q:model_sunset} and \nthref{q:pinaple_relation} questions.

\end{document}

• \ref has never been expandable, independently of hyperref. \edef\x{\ref{sec:1}} then \show\x produces 1\hbox {} when reference is defined. So it can not be used in \ifcase as is and this is not hyperref's fault. – user4686 Sep 22 '17 at 16:08
• @jfbu: As far as I know, \ref is made expandable with calc package. I know that the regular version isn't expandable – user31729 Sep 22 '17 at 16:12
• It does not change anything to my \edef\x{\ref{sec:1}} test. There is of course the extra problem that this produces the resulte of \thesection etc or \thepage for \pageref which is not guaranteed to be usable in numerical contexts anyhow, but here the problem is with the extra \null token. Of course in case reference is undefined then LaTeX code is even less compatible with \ifcase etc... – user4686 Sep 22 '17 at 16:15
• As long as \theenumi expands to an integer number, \getrefnumber is correct here. I did not claim that \ifcase\ref{...} is useful. – user31729 Sep 22 '17 at 16:18
• when I said in my comment "has never been expandable" my tongue slipped I meant "not compatible with numerical contexts". This is all caused by an extra \null thing. I think your advice is very good about using \getrefnumber, I just wanted to point out that it is not hyperref's fault if one can not use \ref in numerical contexts. – user4686 Sep 22 '17 at 16:20