I would like to display a = b .+ 1
But it is displayed as a = b. + 1
(moves .
next to b).
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityI would like to display a = b .+ 1
But it is displayed as a = b. + 1
(moves .
next to b).
\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.
If this is MATLAB code, and not regular math, then the lstinline
command from the listings package would be useful:
\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.