# Exsheets: how to get a counter/reference combining section and question numbers

I am writing a document that has several major sections, and each section will have some exercises and solutions. I am using the exsheets package, which seems to do almost everything I want. However, I'm puzzled about how to get my numbering and references to work the way I want. I would like the question numbers to be S.Q where S is the section # and Q the question #. I've used the counter-format and counter-within-section options to do that, and it looks like what I want in the document. The only problem is that I would like to be able to refer to the exercises by these numbers, but if I use a label in the question the counter only seems to keep track of Q. I can get the effect I want by using multiple references, for example \ref{s:seclabel}.\ref{q:exlabel}. But this seems clunky, and I have to look up the section label every time. I would have expected that when I set the counter-within option it would also make the reference numbers work the way I want, but it doesn't seem to. Is there a better way to do this than my hacky one?

I've included a MWE that uses the solution I came up with, so you can see what I would like it to do. The key part is the references in section 2. In the MWE, I also refer to a subpart of one question, which requires three separate references. To be honest, I'm not sure I would ever need to do this, and I suspect it's even trickier since I have a separate enumerate environment. But if anyone can come up with a general solution that deals with this also, that would be even better.)

\documentclass[11pt]{article}

\usepackage[counter-format=se.qu,counter-within=section]{exsheets}
\usepackage{enumitem}

\begin{document}

\section{First Section}
\label{s:first}
\subsection{Introduction}
Here's some text.

\subsection{Explanation}
And some more.

\subsection{Exercises}
\begin{question}\label{q:first}
Suppose I have ...
\begin{enumerate}[label=\alph*)]
\item What is ... \label{q:firstsub}
\item Now compute ...
\end{enumerate}
\end{question}

\begin{solution}
\begin{enumerate}[label=\alph*)]
\end{enumerate}
\end{solution}

\begin{question}\label{q:second}
Now I have ... What is ...
\end{question}

\section{Second Section}
\label{s:second}

\subsection{Some text}
Here I have an example that refers back to Exercise
\ref{s:first}.\ref{q:first}. I {\em might} even want to refer to
Exercise \ref{s:first}.\ref{q:first}\ref{q:firstsub} although that is
less important.

\subsection{Exercises}
\begin{question}\label{q:later}
And here's more...
\begin{enumerate}[label=\alph*)]
\item What is ...
\item Now compute ...
\end{enumerate}
\end{question}

And perhaps now I refer to Exercise \ref{s:second}.\ref{q:later}.

\end{document}


For the question number, you can redefine \thequestion (the representation used for the question counter): to aprepend the section counter and the dot:

\renewcommand\thequestion{\thesection.\arabic{question}}


Having done this, for the enumerations you can use the ref key to provide the desired formatting for cross-referenced simply as:

ref=\thequestion\alph*)


I added this as a general setting for enumerate

\setlist[enumerate,1]{label=\alph*),ref=\thequestion\alph*)}


but you can do this, for a dedicated list, in case you want other formatting for some other enumerate environments.

The complete example:

\documentclass[11pt]{article}
\usepackage[counter-format=se.qu,counter-within=section]{exsheets}
\usepackage{enumitem}

\renewcommand\thequestion{\thesection.\arabic{question}}
\setlist[enumerate,1]{label=\alph*),ref=\thequestion\alph*)}

\begin{document}

\section{First Section}
\label{s:first}
\subsection{Introduction}
Here's some text.

\subsection{Explanation}
And some more.

\subsection{Exercises}
\begin{question}\label{q:first}
Suppose I have ...
\begin{enumerate}
\item What is ... \label{q:firstsub}
\item Now compute ...
\end{enumerate}
\end{question}

\begin{solution}
\begin{enumerate}
\end{enumerate}
\end{solution}

\begin{question}\label{q:second}
Now I have ... What is ...
\end{question}

\section{Second Section}
\label{s:second}

\subsection{Some text}
Here I have an example that refers back to Exercise~\ref{q:first}. I {\em might} even want to refer to
Exercise~\ref{q:firstsub} although that is
less important.

\subsection{Exercises}
\begin{question}\label{q:later}
And here's more...
\begin{enumerate}[label=\alph*)]
\item What is ...
\item Now compute ...
\end{enumerate}
\end{question}

And perhaps now I refer to Exercise~\ref{q:later}.

\end{document}


• Wouldn't \renewcommand\thequestion{\thesection.\arabic{question}}\setlist[enumerate,1]{label=\alph*),ref=\thequestion\alph*)} be more natural? – clemens Aug 27 '15 at 14:20
• @clemens Much more natural, indeed. I updated the question. Thanks. – Gonzalo Medina Aug 27 '15 at 14:25