is there a way to write −= as one symbol in formulas

In programming -=, += and other similar operators are quite popular. But I am unable to write such operator nicely in Latex equations.

For example when I use x += \frac{\partial C}{\partial x} I get the following formula:

The problem is that += are kind of too far apart. Is there any latex operator which allows to write them closer together?

• \mathrel{+=} should work (or \mathbin) depending if you want the relation space from = or the binary operator space from + Commented Aug 3, 2015 at 7:33
• @DavidCarlisle thank you for such a quick reply. I use this site to quickly write formulas codecogs.com/latex/eqneditor.php, and I am unable to use your expression. Sorry for a noobish question. Commented Aug 3, 2015 at 7:36
• Problems with that site would be off topic here, but I can't test as it seems to be down I get 503 - Service Not Available for any expression just now. Commented Aug 3, 2015 at 7:38
• Another solution is to put + and = into braces: \mathrel{{+}{=}}. So you disable the natural kerning around the two symbol. Commented Aug 3, 2015 at 7:52
• If you want to use += as a relation symbol, which seems the right choice, you have to say \mathrel{+}= because TeX doesn't add space between relation symbols. Commented Aug 3, 2015 at 7:58

You can use

$x \mathrel{+}= \frac{\partial C}{\partial x}$


that will add no space between the + and the = signs and treat the whole block as a single relation symbol.

Of course a definition will do better:

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


and

a \pluseq b \minuseq c


would print

The operators + and - in += and -= should be relational operators as seen in egreg's answer, because TeX does not set a space between +/- and = and the whole expression +=/-= becomes a relational symbol regarding the spacing before and after.

This answer tries an automatic solution. The + and - symbols are made active in math mode only. Then the active character can check, if it is be followed by the equals sign. If positive, the relational version of the + and - operators is used, otherwise the original binary version is taken.

A little additional work is needed to get compatibility with 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
$a += b + c \qquad x -= y - z \qquad \operatorname{foo-bar}$