[Tex/LaTex] Removing double negative signs in equations



I am attempting to write a macro which can remove any extra negative signs in a general equation:

15.2 + -3.1

would be changed to:

15.2 – 3.1

when using the commands:

\[ A \pluss{\B} \]

My problems start when defining the macro \pluss{} for cases such as:

\[ A \pluss{\B} \]


I thought that in the pluss#1{} macro I would just count the number of negative
signs in #1. If it is odd then the result is negative but I am having two problems:

  1. How to count negative signs when the input to the macro is \frac{-3}{12}
  2. How to convert the catcode of the negative signs in \frac{-3}{12} so that
    they don't print…

Best Answer

The argument to \pluss is scanned twice, one for counting the minus signs, the second time ignoring them:


\newcommand{\pluss}{\begingroup\mathcode`-="8000 \plussaux}
  \sbox0{\global\minuses=0 \minuscounts$#1$}%
  \begingroup\lccode`~=`- \lowercase{\endgroup
  \def~}{\global\advance\minuses1 }}
  \begingroup\lccode`~=`- \lowercase{\endgroup















enter image description here

The argument to \pluss should be a monomial expression: something like \pluss{1-2} would produce a weird result.