latex3 / mathtools

Mathematical tools to use with amsmath
LaTeX Project Public License v1.3c
57 stars 7 forks source link

Bug: Cannot have `\setcounter` in renewing `\thesection` #26

Closed UlyssesZh closed 3 years ago

UlyssesZh commented 3 years ago
\documentclass{article}
\usepackage{mathtools} % comment out to avoid the error
\renewcommand\thesection{\setcounter{equation}{0}}
\begin{document}
\section{test} % raises `undefined control sequence`
\end{document}
FrankMittelbach commented 3 years ago

\thesection is supposed to be an expandable command so assignments normally don't work (the basic \setcounter works but more by chance). mathools loads calc package so that changes \setcounterto be a slightly more complicated assignment and that then breaks. Try

 \renewcommand\thesection{\protect\setcounter{equation}{0}}

instead.

u-fischer commented 3 years ago

Use \counterwithin{equation}{section} if you want to reset the equation counter at sections instead of misusing \thesection.

u-fischer commented 3 years ago

Try

 \renewcommand\thesection{\protect\setcounter{equation}{0}}

@FrankMittelbach that gives an rather interesting label: \newlabel{abc}{{\setcounter {equation}{0}}{1}} ;-) (without the protect and calc it is naturally not better).

FrankMittelbach commented 3 years ago

You get the same label form the example if calc is not loaded (without the protect) and as far as I can see that works (even though it is not really meant to be and sort of by chance) but yes, the correct solution is your suggestion of resetting the counter per section counter.

daleif commented 3 years ago

As already mentioned this is not really related to mathtools but rather to the fact that it loads the calc package.

Additionally as others have mentioned, you should use the proper methods if you want to reset a counter at certain points. \thesection should not have any sideeffects like you introduce here.