
답변1
답변2
연산자 +
및 -
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}