When I want to iterate a counter using \stepcounter
inside \mathllap
, \mathclap
or \mathrlap
the counter iterates by 4 instead of by 1. E.g, the following MWE
\documentclass{article}
\usepackage{mathtools}
\newcounter{mycounter}
\begin{document}
$\mathclap{\stepcounter{mycounter}xyz}$
\arabic{mycounter}
\end{document}
outputs
xyz
4
while
\documentclass{article}
\usepackage{mathtools}
\newcounter{mycounter}
\begin{document}
\stepcounter{mycounter}$\mathclap{xyz}$
\arabic{mycounter}
\end{document}
outputs the desired
xyz
1
as expected.
In this simple example the second MWE is an easy work-around, but how can I deal with this problem in a more elegant ways, when it is not so easy to move the \stepcounter
outside of the \mathclap
like in the following MWE:
\documentclass{article}
\usepackage{mathtools}
\newcounter{mycounter}
\newcommand{\printMyNextCounter}{\stepcounter{mycounter}\arabic{mycounter}}
\begin{document}
$\mathclap{\printMyNextCounter xyz}$
\printMyNextCounter
\end{document}
outputs
2xyz
5
I think this can be almost seen as a bug. Would it be possible to fix this bug?
\textrm{..}
and lots of other commands, what is possible (but somewhat costly) is to save and restore all counters at each evaluation (tabularx does that when doing trial layouts) It is really very rare to be doing anything more complicated thanx=0
in a \mathclap certainly counters I have never seen. If you separate out your increment and printing and only have the printing part in the mathclap then it will work better, if you really need the set reset all counters version I could post an answer later probably – David Carlisle Jul 30 '20 at 23:11