4

What is the correct way to count exercises per section like

enter image description here

I got it with

% Count exercises per section =====
\let\oldsection\section
\renewcommand{\section}{%
\setcounter{exercise}{0}%
\oldsection}

but is there a function inside xsim?

MWE:

\documentclass{scrartcl}
\usepackage{xsim}
\xsimsetup{
exercise/the-counter = \thesection.\arabic{exercise},
}
% Count exercises per section =====
\let\oldsection\section
\renewcommand{\section}{%
\setcounter{exercise}{0}%
\oldsection}

\begin{document}
\section{First section}

\begin{exercise}
An exercise in section 1
\end{exercise}

\section{Second  section}

\begin{exercise}
An exercise in section 2
\end{exercise}
\begin{exercise}
Another exercise in section 2
\end{exercise}
\begin{exercise}
Another exercise in section 2
\end{exercise}
\end{document}

1 Answer 1

5

The xsim package provides the option exercise/within that determines when the exercise counter is reset, see page 20 of the manual.

Code:

\usepackage{xsim}
\xsimsetup{
exercise/the-counter = \thesection.\arabic{exercise},
exercise/within = section
}
2
  • Ah, very good! ;)
    – cis
    Commented Mar 2, 2020 at 15:30
  • Which is just a wrapper around the kernel macro \counterwithin :)
    – cgnieder
    Commented Mar 2, 2020 at 17:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .