Text FormattingDepiction of Increment Operator '+='

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
arobase
Posts: 33
Joined: Sat Apr 14, 2012 7:46 pm

Depiction of Increment Operator '+='

Post by arobase »

Code: Select all

\newcommand{\incr}[2]{#1 += #2}
\newcommand{\decr}[2]{#1 -= #2}
\incr{x}{c}\\
\decr{x}{c}
displays
x += c
x -= c
but the minus sign seems smaller than the plus sign. Using $+=$ and same for '-' is balanced, but it puts an undesired space between '+' and '='. Is there a fix?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

Depiction of Increment Operator '+='

Post by Stefan Kottwitz »

arobase wrote:but the minus sign seems smaller than the plus sign.
This is a text plus and a text dash, not a math plus and a math minus.
arobase wrote:Using $+=$ and same for '-' is balanced, but it puts an undesired space between '+' and "=". Is there a fix?
There's a predefined spacing around relation operators. This is meaningful, but in your case when you combine two operators you get too much space inbetween. The space would go if you change the meaning of the operators: if you hide a relation operator in braces, it looses that meaning and works like an ordinary math expression.

So this would work:

Code: Select all

\newcommand{\incr}[2]{#1 ${+}{=}$ #2}
\newcommand{\decr}[2]{#1 ${-}{=}$ #2}
However, you are still working in text mode. I would use math mode for such expressions. And wenn all in math mode, I would define a relation operator +=, which automatically has the correct spacing to the left and to the right.

Stefan
LaTeX.org admin
Post Reply