수식에서 −=를 하나의 기호로 쓰는 방법이 있나요?

수식에서 −=를 하나의 기호로 쓰는 방법이 있나요?

프로그래밍에서는 -=, +=및 기타 유사한 연산자가 꽤 유명합니다. 그러나 나는 Latex 방정식에서 그러한 연산자를 훌륭하게 작성할 수 없습니다.

예를 들어 사용하면 x += \frac{\partial C}{\partial x}다음 공식을 얻습니다.

여기에 이미지 설명을 입력하세요

문제는 +=너무 멀리 떨어져 있다는 것입니다. 서로 더 가깝게 작성할 수 있는 라텍스 연산자가 있습니까?

답변1

당신이 사용할 수있는

\[
x \mathrel{+}= \frac{\partial C}{\partial x}
\]

그러면 +와 = 기호 사이에 공백이 추가되지 않고 전체 블록이 단일 관계 기호로 처리됩니다.

물론 정의가 더 나을 것입니다:

\newcommand{\pluseq}{\mathrel{+}=}
\newcommand{\minuseq}{\mathrel{-}=}

그리고

a \pluseq b \minuseq c

인쇄할 것이다

여기에 이미지 설명을 입력하세요

답변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}

결과

관련 정보