我有一个程序,可以生成如下的 Tex 代码片段:
$\product{a}{\add{b}{c}}\leftrightarrow \add{\product{a}{b}}{\product{a}{c}}$
$\add{a}{\add{b}{c}}\leftrightarrow \add{\add{a}{b}}{c}$
$\product{a}{b}\leftrightarrow \product{b}{a}$
$\product{a}{\product{b}{c}}\leftrightarrow \product{\product{a}{b}}{c}$
$\subtract{\power{x}{\two}}{\power{y}{\two}}\leftrightarrow \product{\add{x}{y}}{\subtract{x}{y}}$
目前,我已经定义了
\newcommand\add[2]{({#1}+{#2})}
\newcommand\subtract[2]{({#1}-{#2})}
\newcommand\product[2]{({#1}{#2})}
但这会导致产生过多的括号——例如\sum{\product{a}{b}}{c}
呈现为((ab)+c)。
我如何定义\add
和\product
其他算术运算以便添加最少数量的必要括号? 澄清一下,我想\product{a}{\add{b}{c}}
生成a(b+c),但\sum{\product{a}{b}}{c}
要生产AB + C。
注意:如果在生成 TeX 的代码中可以直接执行此操作,我就会这么做!
答案1
可能我没有你所需要的优先规则,但是
\def\p{0}
\def\power#1#2{%
\ifnum\p>20(\fi
{\def\p{20}#1}^{\def\p{0}#2}%
\ifnum\p>20)\fi}
\def\product#1#2{%
\ifnum\p>20(\fi
{\def\p{20}#1#2}%
\ifnum\p>20)\fi}
\def\add#1#2{%
\ifnum\p>10(\fi
{\def\p{10}#1+#2}%
\ifnum\p>10)\fi}
\def\subtract#1#2{%
\ifnum\p>8(\fi
{\def\p{9}#1-#2}%
\ifnum\p>8)\fi}
\def\two{2}
$\product{a}{\add{b}{c}}\leftrightarrow \add{\product{a}{b}}{\product{a}{c}}$
$\add{a}{\add{b}{c}}\leftrightarrow \add{\add{a}{b}}{c}$
$\product{a}{b}\leftrightarrow \product{b}{a}$
$\product{a}{\product{b}{c}}\leftrightarrow \product{\product{a}{b}}{c}$
$\subtract{\power{x}{\two}}{\power{y}{\two}}\leftrightarrow \product{\add{x}{y}}{\subtract{x}{y}}$
$\subtract{a}{\subtract{b}{c}}$
$\subtract{\subtract{a}{b}}{c}$
\bye
答案2
这个答案最初是出于大卫的回答。但要解决我在评论中提出的问题,需要从不同的角度来看待问题。
我还没有彻底测试过代码在所有情况下是否都能达到预期的效果。目的是尽可能少使用括号。
更新:关于不同的视角并不完全准确。我一开始的观点不同,但最后,稍后再看结果,我发现代码只是大卫的回答。因此,我用更接近该答案的风格重写了它。
\def\p{0}
\def\power #1#2{%
\ifnum\p>3(\fi
{\def\p{4}#1}^{\def\p{0}#2}%
\ifnum\p>3)\fi
}
\def\product #1#2{%
\ifnum\p>2(\fi
{\def\p{2}#1#2}% perhaps #1\cdot #2 would be better
\ifnum\p>2)\fi
}
\def\add #1#2{%
\ifnum\p>1(\fi
{\def\p{0}#1+#2}%
\ifnum\p>1)\fi
}
\def\subtract #1#2{%
\ifnum\p>0(\fi
{\def\p{0}#1-\def\p{2}#2}%
\ifnum\p>0)\fi
}
\def\two{2}
$\product{a}{\add{b}{c}}\leftrightarrow \add{\product{a}{b}}{\product{a}{c}}$
$\add{a}{\product{b}{c}}$
$\add{a}{\product{b}{\subtract{c}{d}}}$
$\add{a}{\add{b}{c}}\leftrightarrow \add{\add{a}{b}}{c}$
$\product{a}{b}\leftrightarrow \product{b}{a}$
$\product{a}{\product{b}{c}}\leftrightarrow \product{\product{a}{b}}{c}$
$\subtract{\power{x}{\two}}{\power{y}{\two}}\leftrightarrow \product{\add{x}{y}}{\subtract{x}{y}}$
$\subtract{a}{\subtract{b}{c}}$
$\subtract{\subtract{a}{b}}{c}$
$\product{a}{\add{b}{c}}$
$\add{\product{a}{b}}{c}$
$\subtract{a}{\subtract{b}{\subtract{c}{d}}}$
$\subtract{\subtract{\subtract{a}{b}}{c}}{d}$
$\add{a}{\subtract{b}{c}}$
$\subtract{\add{a}{b}}{\add{c}{d}}$
$\add{\subtract{\add{a}{b}}{\add{c}{d}}}{\add{e}{f}}$
$\power{x}{\power{y}{z}}$
$\power{\power{x}{y}}{z}$
$\power{x}{\add{y}{z}}$
$\power{x}{\power{y}{\add{z}{w}}}$
$\subtract{\power{x}{\product{\power{y}{\add{z}{w}}}{\subtract{z}{w}}}}{\subtract{d}{e}}$
\nopagenumbers
\bye
我最初是如何编写的:(对于分支内容,我心中有一个更通用的想法,但最终它们仅在(
和的存在上有所不同)
)
\catcode`@ 11
\long\def\@firstoftwo #1#2{#1}
\long\def\@secondoftwo #1#2{#2}
\def\power #1#2{%
\ifnum\p>3 \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{{({\def\p{4}#1}^{\def\p{0}#2})}}{{\def\p{4}#1}^{\def\p{0}#2}}}
\def\product#1#2{%
\ifnum\p>2 \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{{(\def\p{2}#1#2)}}{{\def\p{2}#1#2}}}
\def\add#1#2{%
\ifnum\p>1 \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{{(\def\p{0}#1+#2)}}{{\def\p{0}#1+#2}}}
\def\subtract#1#2{%
\ifnum\p>0 \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{{(\def\p{0}#1-\def\p{2}#2)}}{{\def\p{0}#1-\def\p{2}#2}}}
\catcode`@ 12