# Removing double negative signs in equations

## 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...
• Welcome to TeX.SE! Does the fraction have to be stated as \frac{-3}{4} in the \B macro, or would it be OK to state it as -\frac{3}{4}? Could a minus sign be tucked away in the denominator as well? – Mico Jun 30 '12 at 0:17
• Unfortunately, minus sign can be anywhere... – JAD Jun 30 '12 at 0:43
• Is 1 \pplus{1-2} allowed? – Caramdir Jun 30 '12 at 0:54
• Can you create a list of test cases that you would like this to work for. I have an idea, but need to know what kind of expressions you are thinking of and to test that it works for the cases you are thinking of. – Peter Grill Jun 30 '12 at 1:21
• The assumption is also that you don't want two negatives to turn into a positive "automagically", right? – Werner Jun 30 '12 at 1:25

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}{%
\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{\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.

Here is a solution that redefines the \frac macro within the \pluss macro and evaluates the ratio to test if it is negative.

This seems to work for all the cases I could come up with. I attempted to format the output so that it clearly shows the input and output, but there are a few cases (noted with a red asterix) where you need to refer to the code to actually see what it is. ## Code:

\documentclass{article}
\usepackage{etoolbox}
\usepackage{pgf}
\usepackage{amsmath}
\usepackage{xcolor}

\let\OldFrac\frac
\newtoggle{EncouteredFrac}
\newcommand{\Neumerator}{}%
\newcommand{\Denominator}{}%
\newsavebox\ExpresionBox

\newcommand\pluss{%
\renewcommand{\frac}{%
\global\toggletrue{EncouteredFrac}%
\xdef\Neumerator{##1}%
\xdef\Denominator{##2}%
}%
\global\togglefalse{EncouteredFrac}%
\sbox\ExpresionBox{\ensuremath{#1}}% set EncountredFraction
%
\iftoggle{EncouteredFrac}{%
\renewcommand{\frac}{(##1 / ##2)}%
\pgfmathsetmacro{\Var}{#1}%
\pgfmathtruncatemacro{\AbsNeumerator}{abs(\Neumerator)}%
\pgfmathtruncatemacro{\AbsDenominator}{abs(\Denominator)}%
\pgfmathparse{ifthenelse(\Var>=0,"+","-")}%
\pgfmathresult\OldFrac{\AbsNeumerator}{\AbsDenominator}%
}{%
\pgfmathsetmacro{\Var}{#1}%
\pgfmathsetmacro{\AbsVar}{abs(\Var)}%
\pgfmathparse{ifthenelse(\Var>=0,"+","-")}%
\pgfmathresult\AbsVar%
}%
}%

\newcommand{\A}{15.2}
\newcommand{\PositiveNumber}{3.1}
\newcommand{\NegativeNumber}{-3.1}

\newcommand{\PositiveFracA}{\frac{1}{2}}
\newcommand{\PositiveFracB}{\frac{-1}{-2}}

\newcommand{\NegativeFracA}{\frac{-3}{4}}
\newcommand{\NegativeFracB}{\frac{3}{-4}}

\newcommand{\Note}{\textcolor{red}{$\ast$}}%

\newcommand{\Test}{$#1~\text{\texttt{\textbackslash pluss}} \{#2\} \to #1 \pluss{#2}$}%

\begin{document}
\section*{These  work}
\par\noindent
\textit{Numbers:}\par

\Test{\A}{3.1}\par
\Test{\A}{-3.1}

\bigskip\par\noindent
\textit{Macros:}\par
\Test{\A}{\PositiveNumber}\par
\Test{\A}{-\PositiveNumber} \Note\quad Subtract a postive number\par
\Test{\A}{-\NegativeNumber} \Note\quad Subtract a negative number\par

\bigskip\par\noindent
\Test{\A}{\PositiveFracA}\medskip\par
\Test{\A}{\PositiveFracB}

\bigskip\par\noindent
\Test{\A}{\NegativeFracA}\medskip\par
\Test{\A}{\NegativeFracB}

%----------------------------------

\bigskip\par\noindent
\textit{Subtract a positive frac:}\par
\Test{\A}{-\PositiveFracA}\medskip\par
\Test{\A}{-\PositiveFracB}

\bigskip\par\noindent
\textit{Subtract a negative frac:}\par
\Test{\A}{-\NegativeFracA}\medskip\par
\Test{\A}{-\NegativeFracB}
\end{document}