20

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:

enter image description here

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

5
  • 1
    \mathrel{+=} should work (or \mathbin) depending if you want the relation space from = or the binary operator space from + 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. 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. 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.
    – skevin93
    Aug 3, 2015 at 7:52
  • 2
    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.
    – egreg
    Aug 3, 2015 at 7:58

2 Answers 2

21

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

enter image description here

0
12

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
\@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}

Result

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .