# exsheets and/or xsim: enumerate equations by exercises

I am working with the exsheets (and xsim) package within a scrartcl document class. I want to create a sheet with Problems and their respective Solutions, but such that the equations within each problem are enumerated according to the Problem they are in (with same counter for the Problem and its respective Solution). So far (minimal example), this is what I have:

\documentclass[paper=a4, fontsize=11pt]{scrartcl}

\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{exsheets}
\SetupExSheets{solution/print = true}

\begin{document}

\begin{question} First exercise with equation
$$a + b = 1$$
\end{question}
\begin{solution} Solution to first exercise with equation
$$a - b = 1$$
\end{solution}

\begin{question} First exercise with equation
$$a + b = 1$$
\end{question}
\begin{solution} Solution to first exercise with equation
$$a - b = 1$$
\end{solution}

\end{document}


which yields,

Now, if I add the instruction \numberwithin{equation}{question} then I can reset the counter by Problem,

but instead, I would like something like this:

Edit: Thank to @cgnieder now I know xsim exists. If possible (or more convenient) I'd like to do the same in this package.

• this would work in exsheets' successor package xsim. – cgnieder Mar 31 '20 at 17:10
• @cgnieder thank for the information, I didn' know about xsim. From the manual, I know exercise/within reset the numbering according to the provided counter, do you know how to set such criterion to something like exercise/within = {exercise} (which is wrong)? – SNC92 Mar 31 '20 at 19:54
• You probably need to \renewcommand{\theequation}{\QuestionNumber{\CurrentQuestionID}.\arabic{equation}} with exsheets – cgnieder Apr 1 '20 at 5:10

With an up to date tex distribution \counterwithin works out of the box using

\counterwithin{equation}{question}


\counterwithin is defined by the LaTeX kernel since April 2018 so no additional package is needed.

\documentclass{article}

\usepackage{exsheets}
\SetupExSheets{solution/print = true}

\counterwithin{equation}{question}
\begin{document}

\begin{question}
First exercise with equation
$$a + b = 1$$
\end{question}
\begin{solution}
Solution to first exercise with equation
$$a - b = 1$$
\end{solution}

\begin{question}
First exercise with equation
$$a + b = 1$$
\end{question}
\begin{solution}
Solution to first exercise with equation
$$a - b = 1$$
\end{solution}

\end{document}


With older versions of exsheets you may need to do

\renewcommand*\theequation{\GetQuestionProperty{counter}{\CurrentQuestionID}\arabic{equation}}


Some time between March and May 2017 I published the package xsim as successor to exsheets. The latter only gets bug fix releases while xsim is actively developed and improved.

\documentclass{article}
\usepackage[no-files]{xsim}
\xsimsetup{solution/print}
\counterwithin{equation}{exercise}

\begin{document}

\begin{exercise}
First exercise with equation
$$a + b = 1$$
\end{exercise}
\begin{solution}
Solution to first exercise with equation
$$a - b = 1$$
\end{solution}

\begin{exercise}
First exercise with equation
$$a + b = 1$$
\end{exercise}
\begin{solution}
Solution to first exercise with equation
$$a - b = 1$$
\end{solution}

\end{document}


Try \renewcommand{\theequation}{\thequestion.\arabic{equation}} in addition to \numberwithin{equation}{question}.

Edit that was completely wrong. Sorry! \numberwithin also changes how the counter is displayed, as does \counterwithin.

• thank you for the answer. I tried your suggestion but it didn't work; commands in different order but nothing. I'm shifting now to xsim package. If any, all comments are welcome. – SNC92 Mar 31 '20 at 21:49
• \numberwithin is defined in amsmath. I think that \counterwithin is better in this case; requires \usepackage{chngcntr}. – barbara beeton Mar 31 '20 at 22:17
• @barbarabeeton, you're right, and the \renewcommand is completely unnecessary. But the question seems to have amsmath loaded. Using \counterwithin also worked without loading chngcntr (maybe some other package loaded it). Weirdly everything I tried seemed to work, both \numberwithin and \counterwithin. I can't figure out why nothing worked for @snc92 – Subhaneil Lahiri Apr 1 '20 at 2:01