[Tex/LaTex] \cref does not work for Section in emulateapj.cls

clevereflabelssectioning

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}
\hypersetup{colorlinks,
linkcolor=blue,
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, \\
Villanueva de la Ca\~nada (Madrid), Spain; \\
\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:
enter image description here

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.

Best Answer

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.

Related Question