14

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...
Caramdir
  • 89,023
  • 26
  • 255
  • 291
JAD
  • 141

2 Answers2

9

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~}{\global\advance\minuses1 }}
\def\minusignored{%
  \begingroup\lccode`~=`- \lowercase{\endgroup
  \def~}{}}

\begin{document}

\parbox{8cm}{
\newcommand\test[1]{\texttt{\detokenize{#1}}\hfill#1}
\linespread{1.5}\selectfont\parfillskip=0pt

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

enter image description here

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

egreg
  • 1,121,712
4

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

References:

Code:

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

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

\newcommand\pluss[1]{% \renewcommand{\frac}[2]{% \global\toggletrue{EncouteredFrac}% \xdef\Neumerator{##1}% \xdef\Denominator{##2}% }% \global\togglefalse{EncouteredFrac}% \sbox\ExpresionBox{\ensuremath{#1}}% set EncountredFraction % \iftoggle{EncouteredFrac}{% \renewcommand{\frac}[2]{(##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}[2]{$#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 Add a negative number\par \Test{\A}{-\NegativeNumber} \Note\quad Subtract a negative number\par

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

\bigskip\par\noindent \textit{Add a negative frac:}\par \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}

Peter Grill
  • 223,288