Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to display a = b .+ 1

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

share|improve this question

2 Answers 2

up vote 21 down vote accepted
\newcommand{\periodplus}{\mathbin{{.}{+}}}

\( 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.

share|improve this answer
    
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

\documentclass{article}
\usepackage{listings,xcolor}
\lstset{language=MATLAB,basicstyle=\ttfamily,stringstyle=\color{red}}
%\lstset{showstringspaces=false} % uncomment this if you don't want spaces in
                                 % strings to get their own symbols
\newcommand{\periodplus}{\mathbin{{.}{+}}}

\begin{document}
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) \).
\end{document}

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

share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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