TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


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...
share|improve this question
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:


\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.

share|improve this answer

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.

enter image description here





    \sbox\ExpresionBox{\ensuremath{#1}}% set EncountredFraction
        \renewcommand{\frac}[2]{(##1 / ##2)}%





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

\section*{These  work}


\Test{\A}{-\PositiveNumber} \Note\quad Subtract a postive number\par
\Test{\A}{\NegativeNumber}  \Note\quad Add a negative number\par
\Test{\A}{-\NegativeNumber} \Note\quad Subtract a negative number\par

\textit{Add a positive frac:}\par

\textit{Add a negative frac:}\par


\textit{Subtract a positive frac:}\par

\textit{Subtract a negative frac:}\par
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.