数式で −= を 1 つの記号として記述する方法はありますか

数式で −= を 1 つの記号として記述する方法はありますか

プログラミングでは-=、 、+=およびその他の類似の演算子が非常に人気があります。 しかし、私はそのような演算子を LaTeX 方程式でうまく記述することができません。

たとえば、 を使用するとx += \frac{\partial C}{\partial x}次の式が得られます。

ここに画像の説明を入力してください

問題は、それら+=があまりにも離れすぎていることです。それらをより近づけて記述できる LaTeX 演算子はありますか?

答え1

使用できます

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

これにより、+ 記号と = 記号の間にスペースは追加されず、ブロック全体が単一の関係記号として扱われます。

もちろん、定義した方が良いでしょう:

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

そして

a \pluseq b \minuseq c

印刷する

ここに画像の説明を入力してください

答え2

およびの演算子+および-は、egregの+=-=答え+これは、TeX では/-との間にスペースが設定されず=、式全体の+=/ が-=前後のスペースに関する関係記号になるためです。

この回答は自動的な解決法を試みます。+および-記号は、数式モードでのみアクティブになります。次に、アクティブな文字は、等号が続くかどうかを確認できます。 正の場合、+および-演算子の関係バージョンが使用され、そうでない場合は元のバイナリ バージョンが採用されます。

以下との互換性を得るには、少しの追加作業が必要です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}

結果

関連情報