# Using cleveref for cross-referencing to exsheets questions

I really like the functionalities offered by exsheets, a package for typesetting exercise sheets and exam papers (and their solutions). One particularly nice feature is the possiblity of assigning labels to question environments. This is done by passing the auto-label package option, which, if no label=<label> option is passed to a question environment, uses the question's unique ID to create a label, as follows:

\label{qu:<label>}


Unfortunately, that labelling functionality currently doesn't take advantage of the (clever) redefinition of \label by the cleveref package, my favourite package for typesetting cross-references. More specifically, cleveref redefines the \label macro as taking one optional argument in addition to its mandatory argument:

\label[<type>]{<label>}


The <type> optional argument is used by cleveref during a \ref call (cleveref also redefines \ref) to identify what kind of environment the referenced label is associated with, so that the correct cross-reference format be selected and typeset.

I'd like to patch exsheets's question environment so that it create a label using \label[qu]{<label>}, not \label{qu:<label>}. That sounds relatively easy. Unfortunately, because I'm not familiar with expl3 at all (yet), which exsheets is built upon, all my attempts have so far proven infructuous. Could anybody familiar with expl3 give me a hand with this, please?

Below is some code describing what I'm trying to achieve.

\documentclass{article}

\usepackage[english]{babel}
\usepackage{lipsum}

\newcommand\myQname{Q.} % macro for question headings (for more automation)

\usepackage[auto-label]{exsheets}
\SetupExSheets[question]{name=\myQname}

\usepackage{hyperref}
\usepackage{cleveref}
\crefformat{qu}{#2\myQname~#1#3} % custom format for cross-ref. to a single question

\begin{document}

\begin{question}[name=Question, ID=foo]
% Currently, 'ID=foo' (assuming 'auto-label') results in '\label{qu:foo}'.
% However I would like it to result in
\label[qu]{foo}
% but that should be done by exheets "behind the scenes".
%
\lipsum[1]
\end{question}
%
Then, I could refer to that first question
using {\ttfamily\string\cref\{foo\}} (see below).

\begin{question}[name=Question]
Do as in \cref{foo}, but use the following data instead:
\lipsum[2]
\end{question}

\end{document}

• Unfortunately the labeling is hard-coded into question environment. It would be easy to change this, though... So: another feature request? :) – clemens Nov 27 '13 at 22:30
• @cgnieder I don't know if a lot of people use cleveref in conjunction with your exsheets package, so perhaps that feature doesn't warrant a new release; it's up to you. It sure would be useful to me, though :) Perhaps compatibility with cleveref could be offered as an exsheets package option; in that case, the user should ideally be able to specify the type associated with questions; it could be qu by default, but the user should be able to change that if s/he wants. I'm curious, though: is there no easy way of patching question without redefining it from scratch? – jub0bs Nov 27 '13 at 23:10
• Currently the internal environment calls \label directly so you'd need to redefine it in order to change this... what it should do is call a function instead that could be set as option or by redefinition... I'll add a suitable user interface. – clemens Nov 27 '13 at 23:18
• @cgnieder I see. That would be great. I guess my question turns out to be a feature request, then :) Sorry for not posting it on your bitbucket page, but I originally thought I might be able to implement that feature with an easy fix. Thanks again for your help. I use exsheets more and more these days, and, as far as I'm concerned, it's ready for prime time. I think you should seriously consider v1.0 in the near future. – jub0bs Nov 27 '13 at 23:21
• Thanks. If you have other feature questions now would be the time :) I could collect for a new update – clemens Nov 28 '13 at 9:48

@Jubobs keeps me busy :)

Version 0.12 2013/12/02 (arrived in TeX Live today) implements the options

• label-format - the format of the automatically placed label when the auto-label option is used. #1 is replaced with the questions ID. Default: qu:#1
• label-cmd - the command used for creating the label. This command should have one mandatory argument (and of course actually create a label...). Default: \label

So now you can do this:

\documentclass{article}
\usepackage[auto-label]{exsheets}[2013/12/02]
\usepackage{cleveref}

\SetupExSheets{
label-format = {#1} ,
label-cmd    = \label[qu]
}
\crefname{qu}
{\GetLCTranslation{exsheets-exercise-name}}
{\GetLCTranslation{exsheets-exercise-name}s}
\Crefname{qu}
{\GetTranslation{exsheets-exercise-name}}
{\GetTranslation{exsheets-exercise-name}s}

\begin{document}

\begin{question}
foo
\end{question}

\begin{question}[ID=bar]
bar
\end{question}

Text \cref{1} text \cref{bar} text. \Cref{bar} \ldots

\Cref{1,bar}

\end{document}


• Nice :) I'll test that right away. – jub0bs Dec 3 '13 at 19:42
• @Jubobs BTW: what always has been possible was placing \label manually in the question body but I guess you already knew that. This fix is only for the auto-label option (I had forgotten about that option until your question... :) ) – clemens Dec 3 '13 at 19:45
• Yes, I knew I could always put a \label[qu]{...} in the body of the question environment, but because I strive for automation (I'm pretty lazy), I was trying to hack exsheets to get it to do \label[qu]{<ID>} automatically. I couldn't have done it alone, though. So thanks again! Are you still testing exsheets? I think it's likely to become the king of exercise-typesetting packages out there, if it isn't already. Do you have any plans for v1.0? – jub0bs Dec 3 '13 at 19:48
• I've now tested your solution. Your v0.12 update of exsheets is awesome. Thanks again. – jub0bs Dec 5 '13 at 14:27
• @Jubobs except that subtitles aren't working any more... next update is on its way – clemens Dec 8 '13 at 15:35