介绍:
我正在尝试编写一个宏,它可以删除一般方程中任何多余的负号:
15.2 + -3.1
将更改为:
15.2 - 3.1
使用以下命令时:
\newcommand{\A}{15.2}
\newcommand{\B}{-3.1}
\[ A \pluss{\B} \]
\pluss{}
当我为以下情况定义宏时,我的问题就开始了:
\newcommand{\A}{5}
\newcommand{\B}{\frac{-3}{12}}
\[ A \pluss{\B} \]
问题:
我以为在 pluss#1{} 宏中我只需计算其中负号的数量#1
。如果是奇数,则结果为负数,但我遇到了两个问题:
- 当宏的输入为
\frac{-3}{12}
- 如何转换负号的 catcode
\frac{-3}{12}
以便它们不会打印......
答案1
的参数\pluss
被扫描两次,第一次用于计算减号,第二次则忽略它们:
\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}
的参数应该\pluss
是一个单项式表达式:类似的表达式\pluss{1-2}
会产生奇怪的结果。
答案2
这是一个解决方案,它\frac
在\pluss
宏中重新定义宏并评估比率以测试它是否为负数。
这似乎适用于我能想到的所有情况。我尝试格式化输出,以便清楚地显示输入和输出,但有些情况(用红色星号标记)需要参考代码才能真正看到它是什么。
参考:
代码:
\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}