How can I typeset the increment "+=" symbol? It should act as a relation (like =), but should not have extra space between the "+" and "=".

migrated from stackoverflow.com Sep 16 '15 at 18:12

This question came from our site for professional and enthusiast programmers.

  • I don't understand the question, you mean inline? Like \verb|+=|? – Dervin Thunk Sep 15 '15 at 12:38
  • 1
    If you are printting code, I think listings or minted would definitely help you. If you want the basics, use a verbatim environment or inline \verb, and, if you really need this in math mode use \mathrel+= may be in a macro like \newcommand*\cppincr{\mathrel+=}. – Manuel Sep 16 '15 at 18:20

It depends on what you consider right. The first thing to note is that the symbol should represent a relation. TeX puts no space between two consecutive relations (and no line break either). So



$x\pluseq 2$

$x\eqplus 2$

will produce

enter image description here

If you prefer a (font dependent) negative kerning, just add it; changing the above definitions into


you'd get

enter image description here

Experiment and decide. Having a macro means you can do whatever you want of the symbol, without changing anything in the document body.


Donald Knuth and Silvio Levy's CWEB system uses plain TeX to "pretty-print" C or C++ listings as part of a literate program.

The CWEB macros use \mathrel{+{=}} to typeset this operator. The example below demonstrates how.

Take this source file plus.w:

@* Increment operator in \.{CWEB}.

int main(void)
    int x = 2;
    x += 2;
    printf("x + 2 = %d\n", x);

Convert this to a .tex file by running cweave plus, and then typeset that with pdftex plus. You get this typesetting:

enter image description here

So how does CWEB do this in TeX? Look at the generated file plus.tex:

\input cwebmac

\N{1}{1}Increment operator in \.{CWEB}.

\Y\B\8\#\&{include} \.{<stdio.h>}\7
\&{int} \\{main}(\&{void})\1\1\2\2\6
\&{int} \|x${}\K\T{2};{}$\7
${}\\{printf}(\.{"x\ +\ 2\ =\ \%d\\n"},\39\|x);{}$\6
\&{return} (\T{0});\6


The relevant line is ${}\|x\MRL{+{\K}}\T{2};{}$\6.

These macros are defined in cwebmac.tex, in the TeXLive distribution (on a standard Linux install it is /usr/local/texlive/2015/texmf-dist/tex/plain/cweb/cwebmac.tex. These two lines are the macros used:

(l. 85) \def\MRL#1{\mathrel{\let\K==#1}} 

(l. 308) \let\K== % assignment operator 

So the program uses \MRL{+\K} for +=, and that expands first to \mathrel{\let\K== +{\K}}, then finally to \mathrel{+{=}}.

Therefore, CWEB uses \mathrel{+{=}}, QED.

  • Why should one consider to use this instead of minted / listings? It seems to me that it is much more complicated to use, has less options, it's harder to find examples in the web and the example looks much worse (but that's personal preference, of course). – Martin Thoma Sep 17 '15 at 7:38
  • 2
    I'm not at all suggesting that you use CWEB unless you want to use it as a system for literate programming. I just thought it would be interesting to see how the creator of TeX chose to typeset this operator in his own programming system. – musarithmia Sep 17 '15 at 13:12

Use \mathrel{+=} to get the spacing right.

  • 1
    The result is exactly what the OP doesn't want, see picture: “no extra space between + and =”. – egreg Sep 17 '15 at 0:02

minted or listings should be used when displaying code. See How to print Source Code with LaTeX.

If you only have a single line, I would prefer egregs solution. But that's totally personal preference. As he said, if you use a macro you can easily change that (consistently) in your document. Just try what you like most.


The example from Andrew Cashner looks like this when printed with minted:

enter image description here

LaTeX source:

\usepackage{amssymb} % needed for math
\usepackage{amsmath} % needed for math
\usepackage[utf8]{inputenc} % this is needed for german umlauts
\usepackage[ngerman]{babel} % this is needed for german umlauts
\usepackage[T1]{fontenc}    % this is needed for correct output of umlauts in pdf
\usepackage[margin=2cm]{geometry} %layout
\usepackage{minted} % needed for the inclusion of source code

\section{Increment operator}

\inputminted[numbersep=5pt, tabsize=4, frame=none, label=main.c]{c}{main.c}


Source file main.c:


int main(void)
    int x = 2;
    x += 2;
    printf("x + 2 = %d\n", x);

Compiled with pdflatex -shell-escape $(SOURCE).tex -output-format=pdf.


Adjust the manual spacing in the \hspace to your taste.



$x \plusequals y$

$x = y$

$x + y$

enter image description here

  • 1
    This is a bad idea. How do you know that 1pt is "right"? And what if you use it in a title (larger font), or in a subscript (smaller font and completely different spacing)? – yo' Sep 16 '15 at 18:16
  • would you recommend something like \hspace*{-\dimexpr\f@size\p@/12\relax} or something? – 1010011010 Sep 16 '15 at 18:54
  • I believe there's something called "math units" which is dependent on the font and which you're supposed to be able to use as a length, but Latex didn't recognise it, and I was too lazy to look into why not. I didn't take the time to carefully decide what was the right amount of space, so 1pt very likely isn't the right amount; that's why it says to alter it to taste. Basically this answer is throwing an idea out and I don't care that it isn't thoroughly thought out or good quality. – Hammerite Sep 16 '15 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.