# Reset counter with starred sections as well (example using exsheets)

I am typesetting exercise/exam sheets with the “exsheets” package. It allows me to reset the question counter with every other counter step (e.g. with every section). Unfortunately this does not work for starred section commands (\section*{Topic}), since no counter is stepped with it (see screenshot), but that is the behavior I need.

Is there a standard solution for that problem? If not, what would be the best/easiest/most elegant/whatever way to implement it?

• This depends on the documentclass, but I'll try to provide a solution. – user31729 Jul 14 '15 at 16:33
• This is generally not possible: starred sections don't have a counter that is stepped thus no associated counter is reset. This is not a problem of exsheets in particular – clemens Jul 14 '15 at 16:33
• @clemens: Nope, but you could hack into section* ;-) – user31729 Jul 14 '15 at 16:33
• @ChristianHupfer Great! I'm using the KOMA-Script classes. – Chris Jul 14 '15 at 16:34
• @ChristianHupfer probably: \pretocmd\section{\setcounter{question}{0}} or something (with etoolbox...) – clemens Jul 14 '15 at 16:35

I suppose, the question environment has a counter named question, if this is different, change the name.

It's possible to prepend code before \section is started either with \pretocmd (etoolbox) or \xpretocmd (xpatch) commands. The later works in more sophisticated cases, but xpatch calls etoolbox anyway.

\documentclass{scrartcl}

\newcounter{question}[section]

\usepackage{xpatch}

\xpretocmd{\section}{\setcounter{question}{0}}{}{} % This does not mean any harm, since it is reset by \section anyway, so \section* is correct here too.

\begin{document}

\setcounter{question}{5}

\section*{First starred section}

The value of question is now \thequestion

\end{document}

• Indeed, the counter is named “question” :-) – Chris Jul 14 '15 at 16:40
• The solution works, and I've learned a lot (e.g. the commands \(x)pretocmd), thank you! The solution with \pretocmd worked as well, can you give some details on the benefits of using “xpatch”? – Chris Jul 14 '15 at 16:44
• @Chris in this case there aren't any benefits, really. But if you want to patch commands defined with \DeclareRobustCommand for example it is easier with xpatch IIRC – clemens Jul 14 '15 at 17:05
• @clemens: Yes, that's true -- in this case I could restrict to etoolbox -- I just use xpatch always, since etoolbox is loaded anyway then. – user31729 Jul 14 '15 at 19:54
• @Chris another example where xpatch is easier: tex.stackexchange.com/q/113147 – clemens Jul 14 '15 at 23:01