# \cref does not work for Section in emulateapj.cls

While \cref works quite well for me, it doesn't seem to recognise section labels when used in emulateapj.cls framework. Here's an example:

\documentclass[iop,apj,tighten,numberedappendix,twocolappendix,revtex4]{emulateapj}
% PACKAGES
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{bm}
\usepackage[pdfa]{hyperref}
\usepackage[capitalize,noabbrev]{cleveref}
\usepackage{filecontents}
\usepackage{natbib}
\bibliographystyle{apj}
\usepackage{paralist}
\usepackage{color}
citecolor=blue,
filecolor=blue,
urlcolor=blue}

\begin{document}

\title{Towards Unifying Time Domain Astronomy}
\shorttitle{Towards Unifying Time Domain Astronomy}

\author{G.\ B\'elanger}
\affil{European Space Astronomy Centre (ESA/ESAC), Science Operations Department, \\
\href{mailto:gbelanger@sciops.esa.int}{gbelanger@sciops.esa.int}}

\begin{abstract}
Time domain astronomy concerns itself with the study of astrophysical systems
by characterising the properties of the light they emit in order to deduce and
infer something about the physical mechanisms that could be responsible for
giving rise to this emission.
\end{abstract}
\keywords{methods: data analysis -- methods: statistical}

\section{Introduction}

The task is divided in three the categories mentioned earlier, which relate to
the characterisation of constance, which also includes searching for new or
transient sources (\cref{s:constancy}); periodicity at any scale and whatever
the nature (\cref{s:periodicity}); and stochastic variability, including the
tracing and detection of state changes (Section \ref{s:variability}).

\section{Characterising Constancy}
\label{s:constancy}

\section{Characterising Periodicity}
\label{s:periodicity}

\section{Characterising Stochastic Variability}
\label{s:variability}

\end{document}


The output looks like this:

Tom from the TeXBlog suggested

\newcommand\sref[1]{Section \ref{#1}}


which works. It would be nice to find the cause of the problem and fix it for everyone in cleveref or emulateapj, but this is far beyond my level of competence as just a user and not a latex dev.

• Welcome to TeX.SX! A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it). – jub0bs Apr 16 '14 at 10:08
• I believe that the problem is in that emulateapj loads revtex4 (or revtex4-1, but then defines sectional commands in a different way, and probably this confuses cleveref. – egreg Apr 16 '14 at 17:46
• Thank you. Any suggestions for a fix? – Guillaume Belanger Apr 21 '14 at 14:34

After spending half a day digging through the sources, I think I may have discovered the cause of this problem.

All LaTeX \section commands contain the macro \refstepcounter, which is responsible for updating section counters. It is redefined by cleveref to store additional information about the current section in \cref@currentlabel. This information is later written out by a redefined \label command to the .aux file. If we now look at the redefinition of \@sect (renamed to \H@old@sect by hyperref) in emulateapj.cls, we find that \refstepcounter is now enclosed between a pair of \begingroup and \endgroup, so all changes to \cref@currentlabel are lost upon exiting the group. A kludge is to make the assignment inside the group global.

We illustrate by the following example:

\documentclass{emulateapj}
\usepackage{hyperref,cleveref}

% patch
\makeatletter
\usepackage{etoolbox}
\patchcmd\H@refstepcounter{\protected@edef}{\protected@xdef}{}{}
\makeatother

\begin{document}
\section{This}\label{this}\cref{that}
\section{That}\label{that}\cref{this}
\end{document}


If we were to typeset the document without the patch, we would find the following lines in the .aux file:

\newlabel{this}{{1}{2}{This}{section.1}{}}
\newlabel{this@cref}{{}{2}}
...
\newlabel{that}{{2}{3}{That}{section.2}{}}
\newlabel{that@cref}{{}{3}}


This is the section information written out by \label; clearly something is missing between the empty braces in the second and fourth lines. With the patch in place, we get instead:

\newlabel{this}{{1}{2}{This}{section.1}{}}
\newlabel{this@cref}{{[section][1][]1}{2}}
...
\newlabel{that}{{2}{3}{That}{section.2}{}}
\newlabel{that@cref}{{[section][2][]2}{3}}


The information is now present, and section references work as well.

• Thank you so much for taking the time to look into this and identify the problem. If we were to correct this issue at the source, should the patch be added to emulateapj.cls, cleveref.sty or hyperref.sty? Just wanted to know if I should try to contact the respective authors to share the solution you have found and fix the problem for everyone. – Guillaume Belanger Apr 22 '15 at 7:26
• For the time being, I'd suggest adding the patch to the document itself. But as luck would have it, a friend asked me a similar question. It's hard to say which package is to 'blame' for this. But if you look into the source of emulateapj, you'll see that it has to manually set \@currentlabel after the group, so it seems a reasonable place to set \cref@currentlabel as well, but I don't want to dig into cleveref to figure out how it sets \cref@currentlabel in the first place, and I'm not sure there's much overlap between users of emulateapj and cleveref. – photon.engine Apr 22 '15 at 16:01
• Thank you. I sent a note to Alexey about this to see if he will add a little fix to emulateapj. – Guillaume Belanger Apr 23 '15 at 17:43