Spacing around \text{} in mathmode

Inspired by this question, here's my mathmode spacing bugbear.

I quite often have equations that look like this:

$$\text{If}\ B_1 \succeq B_2 \ \text{and}\ B_2 \succeq B_3 \ \text{then}\ B_1 \succeq B_3$$


Now, you might notice that before and after each bit of \text{...} there is a "normal sized space". I can't imagine why I wouldn't want that space there, but I have to add it manually. Surely this should be the default behaviour of mathmode's text macro?

Why isn't it, and how could I make it so?

-
I usually add a space inside the \text (like \text{ and }). Is this wrong? –  Caramdir Dec 31 '10 at 19:07
I read somewhere that given that it's wrapped in mathmode, it's preferable to let mathmode do the spacing. Either way, you're adding the space explicitly... –  Seamus Dec 31 '10 at 19:12

It's not the default behavior because you don't always want space. For instance, in your example, you don't want space before \text{If}, since that would indent your equation. Or consider

$x = \begin{cases} 1 & \text{if f(x)} 0 & \text{otherwise} \end{cases}$


Again, that shouldn't be spaced out. Also, like in Caramdir's answer, I re-entered math mode within the \text, which often solves the spacing problems as well. You could define an \stext command for spaced out text:

\newcommand*{\stext}[1]{\text{ #1 }}


(Literal spaces inside are the same as \  outside, I believe.) Another option might be

\newcommand*{\textop}[1]{\mathbin{\text{#1}}}
\newcommand*{\textrel}[1]{\mathrel{\text{#1}}}


Then \textop{foo} would set foo as a binary operator, and \textrel{bar} would set bar as a binary relation. I'm not sure how this spacing would look, though, but it might work for what you want.

-

It probably doesn't add space because it is harder to remove space than to add space. One possible usage is something like 3-\text{connected graphs}.

Side remark: It is also possible to write your example as

$$\text{If B_1 \succeq B_2 and B_2 \succeq B_3 then B_1 \succeq B_3.}$$

-
Maybe even better: replace equation with center and remove the \text. Unless the OP really wants this sentence numbered like an equation. –  Matthew Leingang Dec 31 '10 at 19:49
I do, it's a property of a relation that I want to refer back to by a \tag –  Seamus Jan 1 '11 at 15:27

If you want to introduce automatic spacing before or after \text, your best option is probably to make \text behave like an operator (\mathop), which is the closest thing to text inside math mode. The spacing introduced will be slightly smaller than normal text, but should still be acceptable. Here's the code (making sure that \text still works in text mode):

\newcommand{\textop}[1]{\relax\ifmmode\mathop{\text{#1}}\else\text{#1}\fi}


If you compare this to the respective commands \textbin and \textrel which are of type \mathbin and \mathrel (suggested in Antal's answer), you see that it works quite well in these typical uses of \text, even if the spacing is tight:

These commands also all fail at the begining of an {align} (e.g. \begin{align} & \text{...}):

(The order of each command is the same as above.)

-