I would like to display a = b .+ 1

But it is displayed as a = b. + 1 (moves . next to b).


\( a=b\periodplus 1 \)

TeX (and so LaTeX) doesn't interpret spaces in the code for math formulas, but relies on its predefined rules. For TeX a, . and b represent "ordinary" symbols and + a binary operation symbol.

Consecutive ordinary symbols are set without any space between them; instead, the combination

ordinary binary-operation ordinary

will be set with spaces around the binary operation symbol.

Any subformula can be changed into a binary operation symbol using \mathbin{<subformula>}. I've put both characters between braces to be sure they are interpreted as ordinary symbols in the subformula (they would anyway).

Of course it's possible to say

\( a = b \mathbin{.+} 1 \)

each time, but it's best to have a command for this if it appears more than a couple of times.

  • Thank you for the clear explanation. It makes sense. What does the $ at the beginning and end of the line do? – B Seven Oct 29 '11 at 15:09
  • The $ symbols are to enter math mode; I'll change them into the "official" LaTeX way. – egreg Oct 29 '11 at 15:14

If this is MATLAB code, and not regular math, then the lstinline command from the listings package would be useful:

enter image description here

%\lstset{showstringspaces=false} % uncomment this if you don't want spaces in
                                 % strings to get their own symbols

The line of MATLAB code \lstinline|a = b .+ 1| should not look like the math
equation \( a=b\periodplus 1 \). And there's more difference between
\lstinline|s = sprintf('x = %d\n',x)| and
\(s = sprintf('x = \%d\backslash{}n',x) \).

I only mention this because I'd never seen .+ as a math operator, but it's used all the time in MATLAB.

  • It is for Octave code. – B Seven Oct 29 '11 at 16:45
  • 3
    Close enough. Using math mode for code may work in some cases where spaces or some other special characters aren't significant or used, but in general, you'll find it will create lots of extra work. – Mike Renfro Oct 29 '11 at 17:38
  • IMO, I prefer nicely typeset source code (e.g. the formated TeX WEB code) over typewriter font all over the place. – Khaled Hosny Oct 29 '11 at 19:19
  • Still possible, as I'm sure you know. Just change the basicstyle and similar settings as needed. I'm just used to seeing code in a monospaced font. – Mike Renfro Oct 29 '11 at 19:50
  • Can you show me where .+ is used in MATLAB? – Lorem Ipsum Oct 29 '11 at 22:40

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.