I am trying to write a macro with variable number of inputs (this is important to me) that should return the maximum of the lengths of the different inputs. It uses the foreach macro from pgffor:
\documentclass{article}
\usepackage{pgffor}
%\usepackage{pgfpages}
\newcommand{\getmaxlength}[1]{%
\newlength{\Lmax}
\newlength{\Lcurr}
\setlength{\Lmax}{0pt}
\foreach \arg in {#1} {%
Previous max: \the\Lmax\par
\settowidth{\Lcurr}{\arg}
\ifdim \Lcurr>\Lmax \global\setlength{\Lmax}{\Lcurr} \fi
\arg\ is of length \the\Lcurr\par
New max: \the\Lmax\par
}
}
\begin{document}
\getmaxlength{A,AAA,AA}
\end{document}
Which works as expected on this toy example:
Previous max: 0.0pt
A is of length 7.50002pt
New max: 7.50002pt
Previous max: 7.50002pt
AAA is of length 22.50005pt
New max: 22.50005pt
Previous max: 22.50005pt
AA is of length 15.00003pt
New max: 22.50005pt
However, this solution is neither clean, nor robust:
- It requires to make a
globalassignment at line 13. I found this by trial-and-error, but I assume it's not clean, and I do not know the side effects. - The macro breaks down if I uncomment the
\usepackage(pgfpages)on line 4, another hint that it probably does something wrong.
In either cases (removing the global keyword, using package pgfpages), I obtain a behavior where Lmax is reset at every repetition of the loop:
Previous max: 0.0pt
A is of length 7.50002pt
New max: 7.50002pt
Previous max: 0.0pt
AAA is of length 22.50005pt
New max: 22.50005pt
Previous max: 0.0pt
AA is of length 15.00003pt
New max: 15.00003pt
I figure this has to do with the way foreach handles local vs. global variables, and that a potential solution resides in using its remember option (see here). However I could not get this to work. (Also remember that Lmax is not a `classic' variable, but a length variable).
So, what is the cleanest way of making my bit of code robust to these issues?
(BTW, the use case is the following: I am trying to build a 'local overprint' command for beamer equations (e.g. change x(x+1) to x^2+x inside the equation) that will respect a fixed width for all the overlays, in order to be more visible.)

\global\setlength{\Lmax}{\Lcurr}is absolutely not guaranteed to work and certainly will not work if thecalcpackage is loaded. (\setlengthis a macro not a primitive so the\globalwill do nothing if the first token in its expansion is not an assignment) – David Carlisle Jan 04 '21 at 11:46\newlengthshould be outside the macro otherwise you use up two registers on every use, and you are missing%from ends of lines, the current version will add multiple space tokens which may affect the poutput, depending where it is used. – David Carlisle Jan 04 '21 at 12:03\global\someMacro{...}is generally not robust and will work only by accident, whereas\global\someTexPrimitive{...}is ok. What I still don't understand is how the variable scoping works here. If \Lmax is defined outside theforeachloop, why is it reset at every iteration? – HowieR Jan 05 '21 at 08:54pgffor, the only way of making the assignment "espace the local group" is to use a global variable? – HowieR Jan 05 '21 at 09:06