1

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}

output

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.

0

1 Answer 1

1

A test such as \ifnum1.2mu<0 will fail. You can use \ifdim, but you need to change mu units into pt (you just want to see whether the given length is positive or negative), which is achieved with \mutoglue:

\newcommand{\flexMSpace}[1]{%
  \mskip#1
  plus \muexpr(\ifdim\mutoglue\muexpr#1<0pt -\fi#1)/2\relax
  minus \muexpr(\ifdim\mutoglue\muexpr#1<0pt -\fi#1)/2\relax
  \relax
}
6
  • Got it. Works like a charm; thx!!!
    – user282514
    Dec 27, 2022 at 21:17
  • I guess, two minuses cancel out in the above context, don't they? I.e., --#1 is the same as #1?
    – user282514
    Dec 27, 2022 at 21:44
  • @AlbertNash Yes, they do because of the syntax for a <number>
    – egreg
    Dec 27, 2022 at 21:47
  • Something is wrong: $V\flexMSpace{-.5mu},$ should reduce the space between V and the comma but increases it instead. More precisely, running pdflatex from the current Debian stable on pastebin.com/H1Zq0h3i yields i.ibb.co/syy1Nzg/mwe.png. Might I kindly ask you if it's possible for you to take a look?
    – AlMa0
    Aug 28 at 16:32
  • @AlMa0 Nothing wrong, I'd say: without the explicit glue, TeX would insert a negative kern between V and the comma, but with the glue in between it doesn't.
    – egreg
    Aug 28 at 16:57

You must log in to answer this question.