Relax and exhale
I'm accustomed to seeing \relax at the end of many macros. Often, I don't understand the rationale; sometimes the placement seems more or less arbitrary to me.
For example, computing a math rubber length with stretch and shrink being the half of the modulus of the natural space goes like this (based on https://tex.stackexchange.com/a/669865):
\newcommand{\flexibleMSkip}[1]{%%% 50 per cent of the modulus of the argument after plus and minus. The argument may be any integer or floating-point number.
\mskip#1
plus.5\muexpr\ifdim\mutoglue\muexpr#1<0pt -\fi#1\relax
minus.5\muexpr\ifdim\mutoglue\muexpr#1<0pt -\fi#1\relax
\relax
}
This macro is so \relaxed that it's unlikely to ever need to unload to a psychiatrist. Technically, without the first \relax, the “minus” part is ignored, but why do we need the second and third \relaxes?
Now consider the same exercise for the text mode (my own creation):
\newcommand{\flexibleHSkip}[1]{%
\hskip#1
plus.5\dimexpr\ifdim #1<0pt -\fi#1
minus.5\dimexpr\ifdim #1<0pt -\fi#1
}
This one seems not \relaxed at all but causes no stress to me on small examples. Here, putting a \relax at the end of the plus line or at the end of the minus line or at the end of the whole macro (just before }) causes no difference on my small examples. Would adding these \relaxes be necessary, or cause any joy or grievance, or would it, perhaps, speed up or slow down the computation?
You may claim that the exercise 27.4 from The TeXbook demonstrates the problem in general, but in our examples above, how can we possibly misconstruct the input to cause the occurrence of unintended meaning without error at compilation?
Here is a full, somewhat less \relaxed example for you to play with:
\documentclass{article}
%%% 50 per cent of the modulus of the argument after plus and minus. The argument may be any integer or floating-point number.
\newcommand{\flexibleMSkip}[1]{%
\mskip#1
plus.5\muexpr\ifdim\mutoglue\muexpr#1<0pt -\fi#1\relax
minus.5\muexpr\ifdim\mutoglue\muexpr#1<0pt -\fi#1
}
\newcommand{\flexibleHSkip}[1]{%
\hskip#1
plus.5\dimexpr\ifdim #1<0pt -\fi#1
minus.5\dimexpr\ifdim #1<0pt -\fi#1
}
\showoutput
\begin{document}
\(a\flexibleMSkip{-.5mu}b\)
c\flexibleHSkip{-.1em}d
\end{document}
And, by the way, in the plus and minus expressions, why do we multiply with .5 in the front rather than diving by 2 at the end? Does it make any difference?