Let's say we often need to add or remove space in math formulas and allow for some automatic stretching and shrinking on need. Having two commands works just fine (credit goes to https://tex.stackexchange.com/a/657446):
\documentclass{standalone}
% Adds space and allows for some flexibility:
\newcommand{\flexAddMSpace}[1]{%%% 50 per cent of the argument after plus and minus. The argument may be a nonnegative integer or a nonnegative floating-point number.
\mskip#1 plus.5\muexpr#1\relax minus.5\muexpr#1\relax\relax
}
% Removes space and allows for some flexibility:
\newcommand{\flexRemoveMSpace}[1]{%%% 50 per cent of argument after plus and minus. The argument may be a nonnegative integer or a nonnegative floating-point number.
\mskip-#1 plus.5\muexpr#1\relax minus.5\muexpr#1\relax\relax
}
% Adds space (if the argument is positive) or removes space (if the argument is negative) and allows for some stretching and shrinking:
\newcommand{\flexMSpace}[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\ifnum#1<0 -#1\else #1\fi\relax
minus.5\muexpr\ifnum#1<0 -#1\else #1\fi\relax
\relax
}
\begin{document}
\(a\flexAddMSpace{1.2mu}b\)
\(a\flexRemoveMSpace{1.2mu}b\)
%\(a\flexMSpace{1.2mu}b\)%% this one fails
%\(a\flexMSpace{-1.2mu}b\)%% this one fails, too
\end{document}
However, I'd like to have one command for "add space if the argument is positive and remove space if the argument is negative and stretch or shrink a bit if needed", and not two separate commands. However, my own attempts to compute the modulus have failed (see \flexMSpace
above): I get
Missing = inserted for \ifnum.`
What am I doing wrong? How to write a command that inserts an elastic space in math mode in the sense of \mskip(argument) plus |argument|/2 minus |argument|/2? We can agree to always have mu
as a unit if that helps.