[Tex/LaTex] Removing double negative signs in equations

catcodesmacrosmath-mode

Intro:

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:

\newcommand{\A}{15.2}
\newcommand{\B}{-3.1}
$A \pluss{\B}$


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

\newcommand{\A}{5}
\newcommand{\B}{\frac{-3}{12}}
$A \pluss{\B}$


Question:

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…

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

\documentclass{article}

\newcommand{\pluss}{\begingroup\mathcode-="8000 \plussaux}
\mathchardef\mathminus=\mathcode-
\newcommand{\plussaux}[1]{%
\sbox0{\global\minuses=0 \minuscounts$#1$}%
\ifodd\minuses\mathminus\else+\fi
\minusignored#1\endgroup}
\newcount\minuses
\def\minuscounts{%
\begingroup\lccode~=- \lowercase{\endgroup
\def\minusignored{%
\begingroup\lccode~=- \lowercase{\endgroup
\def~}{}}

\begin{document}

\parbox{8cm}{
\newcommand\test[1]{\texttt{\detokenize{#1}}\hfill#1}

\test{$1.2\pluss{5.7}$}

\test{$1.2\pluss{-5.7}$}

\test{$1.2\pluss{\alpha}$}

\test{$1.2\pluss{-\alpha}$}

\test{$1.2\pluss{-2.3\beta}$}

\test{$1.2\pluss{-\frac{2}{3}}$}

\test{$1.2\pluss{\frac{-2}{3}}$}

\test{$1.2\pluss{\frac{2}{-3}}$}

\test{$1.2\pluss{\frac{-2}{-3}}$}

\test{$1.2\pluss{-\frac{-2}{-3}}$}

}

\end{document}


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