
答案1
答案2
運算子+
and -
in+=
和-=
應該是關係運算符,如egreg 所示回答,因為 TeX 沒有在+
/-
和之間設定空格,=
並且整個表達式+=
/-=
成為關於前後間距的關係符號。
這個答案嘗試了一個自動解決方案。+
和符號-
僅在數學模式下有效。然後活動字元可以檢查它後面是否跟著等號。如果為正,則使用+
and運算子的關係版本-
,否則採用原始二進位版本。
需要做一些額外的工作才能相容amsmath
:
\documentclass{article}
\usepackage{amsmath}
% save original binary + and - as \binplus and \binminus
\mathchardef\binplus=\the\mathcode`+
\mathchardef\binminus=\the\mathcode`-
% define relational + and -
\mathchardef\relplus=\numexpr\the\binplus + "1000\relax
\mathchardef\relminus=\numexpr\the\binminus + "1000\relax
% define active + and -, which check for a following =
\makeatletter
\catcode`+=\active
\catcode`-=\active
\def+{\@ifnextchar=\relplus\binplus}
\def-{\@ifnextchar=\relminus\binminus}
\@makeother\+
\@makeother\-
\makeatother
% enable active + and - for math mode
\AtBeginDocument{%
\mathcode`+="8000\relax
\mathcode`-="8000\relax
}
% patch \newmcodes@ of package `amsopn.sty'
\usepackage{etoolbox}
\makeatletter
\@ifpackageloaded{amsopn}{%
\patchcmd\newmcodes@{%
\mathchardef\std@minus\mathcode`\-\relax
}{%
\let\std@minus\binminus
}{}{\errmessage{\noexpand\newmcodes@ could not be patched}}%
}{}
\makeatother
\begin{document}
\[
a += b + c \qquad x -= y - z \qquad \operatorname{foo-bar}
\]
\end{document}