有沒有一種方法可以將−=寫成公式中的符號

有沒有一種方法可以將−=寫成公式中的符號

在程式設計中-=+=和其他類似的運算子相當流行。但我無法在乳膠方程式中很好地編寫這樣的運算子。

例如,當我使用時,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

運算子+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}

結果

相關內容