Home > Tips and tricks [en] > Resetting the exercise counter
Resetting the exercise counter
Extend your knowledge of LaTeX
Thursday 11 July 2024, by
All the versions of this article: [English] [français]
The \exo
command is used to enter numbered exercises (the starred version is unnumbered). One of the problems that can arise is that the counter is never reset throughout the document, regardless of its length. Another choice was of course possible, but this seemed the simplest to me.
Of course, it’s always possible to reset this counter “by hand” by typing where it seems necessary: \setcounter{tgoexo}{0}
, since the documentation specifies that the counter name is tgoexo
. This method has no disadvantages if used only occasionally (e.g. for a two-part worksheet), but can become tedious if you expect the counter to be reset more systematically. “More systematic” may mean that you want the counter to be reset for each new chapter (for those using the book version) or each new section.
However, there is a LaTeX command (of general application, which is why it is not documented in the tango user manual) that allows you to set one counter to another. This command is \counterwithin
(or its starred version \counterwithin*
).
Please note: this can only work with a command that has an operational counter. In particular, starred sectioning commands such as section*
will not work!
Syntax
\counterwithin{<counter>}{<parent-counter>}
or
\counterwithin*{<counter>}{<parent-counter>}
In both cases, once the command has been executed, <counter>
is reset to zero each time <parent-counter>
is incremented.
The starred version does not change the default display of \the<counter>
, while the non-starred version transforms \the<counter>
into \the<parent-counter>.\arabic{<counter>}
.
Note that there are \counterwithout
and \counterwithout*
commands that do the exact opposite of the above.
Finally, it is possible to define a command which (for example) introduces the different parts of an exercise sheet, provided with a counter visible or not and makes the resetting of counter tgoexo
dependent on the counter of this new command.
Example 1 : \counterwithin
% !TEX TS-program = LuaLaTeX
\documentclass[english,ColorTheme=Red]{tango}
\counterwithin{tgoexo}{section}
\begin{document}
\section{Geometry exercises}
\exo Prove that the perpendicular bisectors of the three sides of a triangle are concurrent.
\exo Prove that the three medians of a triangle are concurrent.
\section{Arithmetic Exercises}
\exo Prove that the sum of two even integers is an even integer.
\exo Prove that the sum of two odd integers is an even integer.
\end{document}
Example 2 : \counterwithin*
% !TEX TS-program = LuaLaTeX
\documentclass[english,ColorTheme=Red]{tango}
\counterwithin*{tgoexo}{section}
\begin{document}
\section{Geometry exercises}
\exo Prove that the perpendicular bisectors of the three sides of a triangle are concurrent.
\exo Prove that the three medians of a triangle are concurrent.
\section{Arithmetic Exercises}
\exo Prove that the sum of two even integers is an even integer.
\exo Prove that the sum of two odd integers is an even integer.
\end{document}
Example 3 : user defined command with visible counter
% !TEX TS-program = LuaLaTeX
\documentclass[english,ColorTheme=Red]{tango}
\newcounter{partexo}
\newcommand\partexo[1]{%
\vspace{12pt plus 3pt minus 2pt}\par
\refstepcounter{partexo}
{\color{ColorOne}\bfseries\thepartexo.\hspace{0.5em}\MakeUppercase{#1}}
\vspace{2pt minus 1pt}\par\nobreak
}
\counterwithin*{tgoexo}{partexo}
\begin{document}
\partexo{Geometry exercises}
\exo Prove that the perpendicular bisectors of the three sides of a triangle are concurrent.
\exo Prove that the three medians of a triangle are concurrent.
\partexo{Arithmetic exercises}
\exo Prove that the sum of two even integers is an even integer.
\exo Prove that the sum of two odd integers is an even integer.
\end{document}
Example 4 : user defined command with hidden counter
% !TEX TS-program = LuaLaTeX
\documentclass[english,ColorTheme=Red]{tango}
\newcounter{partexo}
\newcommand\partexo[1]{%
\vspace{12pt plus 3pt minus 2pt}\par
\stepcounter{partexo}
{\color{ColorOne}\bfseries\MakeUppercase{#1}}
\vspace{2pt minus 1pt}\par\nobreak
}
\counterwithin*{tgoexo}{partexo}
\begin{document}
\partexo{Geometry exercises}
\exo Prove that the perpendicular bisectors of the three sides of a triangle are concurrent.
\exo Prove that the three medians of a triangle are concurrent.
\partexo{Arithmetic exercises}
\exo Prove that the sum of two even integers is an even integer.
\exo Prove that the sum of two odd integers is an even integer.
\end{document}