294

Lets say I have something like:

\[ Let x = number of cats \]

(This is a contrived example, but pretend we are actually inside a long align environment block...) "Let" and "number of cats" need to be written like normal text, while x= needs to be math type text.

I've seen this:

\[ \mbox{Let } x = \mbox{ number of cats} \]

and this:

\[ \textrm{Let } x = \textrm{ number of cats} \]

They seem to produce equivalent output, but I'm not sure which is the "correct" one. Also, they are not ideal in that the extra space needs to be placed inside the text mode block, otherwise it is ignored as math mode and butts the text right up against the math.

Ideally there would be some command which allowed embedding text into an equation, which correctly inserted leading and trailing space around the block as required without adding space inside the text block.

How does one do that "correctly"?

4 Answers 4

277

Another option is to use the \text{} command provided by the amsmath package.

Having said that, I doubt that it's possible to have a single format that is "correct" for all possible requirements of leading and trailing spaces. For instance, I think you'll find that your example looks better without the space before "number" but with the space after "Let."

If there's some reason that you really want to not have the space inside whatever text environment you choose, you can force a space in math mode using a single backslash followed by a space (i.e. \<space>). For example,

\[ \text{Let}\ x=\text{number of cats}. \]
5
  • Note, however, that the option with \ is not exactly the same: ,\ in math mode produces a larger space than \text{, } since the comma behaves differently in math mode. (I prefer the ,\ version.) Commented Sep 23, 2010 at 19:02
  • 11
    \text is almost identical to \mbox. The only difference I'm aware of (besides the more semantic name) is that \text will produce subscript-size text if you use it inside a subscript. Commented Sep 23, 2010 at 20:03
  • 1
    Yes, it's impossible to do the right spacing thing in all cases, but putting the space there might not look the best, but it will never be wrong. Not having any space there, however, results in output that is wrong no matter how one looks at it. Commented Sep 24, 2010 at 15:11
  • 1
    I've just noticed that \text produces crashes used with knitr while \textrm and \mathrm don't (the second one allows for subscripts when used like this x_\mathrm{ Commented Sep 2, 2015 at 10:24
  • 4
    \text changes style with the surrounding context, e.g. in theorem statements it may produce italic. To avoid that, \textnormal may be preferable. Commented Mar 4, 2021 at 7:41
51

I'd agree that the AMS \text command is the way to go. Note that you can also re-enter \text within \text so that the spacing is handled correctly:

\[ 
  \text{Let $x=\text{number of cats}$} 
\]
30

I know this is an old question. But I've been lately thinking about this.

I don't think that using \text is the ideal solution. I think we need to differentiate math mode from text mode. That's all.

For me \text should only have portions of text that, because of its nature within display math can't be typed naturally. All the rest, which is math mode, should be typed with another command. In this “rest” we have at least two kinds of text, for example: \{ x,\ \firstkind{such that $x$ is son of Julia} \} and x = \secondkind{number of cats}. For me, none of this two ones should be written with the outer text mode that \text provides.

\[ 
  \text{Let } x = \var{number of cats}
  \text{, and also }
  \var{Overlap Area} = \frac{\Area(\var{Detection} \cap \var{Ground Truth})}
                            {\Area(\var{Detection} \cup \var{Ground Truth})}
\]

enter image description here

If we think in terms of {center} and $, I think it's clear that

\begin{center}
  Let $x = \var{number of cats}$, and also
  $\var{Overlap Area} = \frac{\Area(\var{Detection} \cap \var{Ground Truth})}
                             {\Area(\var{Detection} \cup \var{Ground Truth})}$
\end{center}

Now the question is how to define \var or whatever name we choose for that thing that is kind of text, but is really math mode and not just text linking math parts, may be one wants \textmath, \mtext, \mthtxt, etc. I used \var for clarity, but may be that command is used so one needs to define another name.

I think it should be typed in the math font \mathrm (ideally) or in the closest to it (which means no \text that changes depending on the outer text font) like \textnormal if the text font is from the family of the math font. The problem with \mathrm is the spacing, but that could be sorted out.

The other case \{ x,\ \mathtext{such that $x$ is son of Julia} \} should also not depend of the outer font, so we should define \mathtext probably with \textnormal or something like that.

I hope this different forms get differentiated in LaTeX3, which would bring more “robustness” and less ambiguities to the process of writing documents with math involved.

Last, imagine a beamer presentation with sans serif font for text, and the usual Computer Modern for math mode; and think about which parts should be in sans serif and which not:

\[
  \text{Let } x = \text{number of cats} \text{, and also } \{ x,\ \text{such that $x$ is son of Julia} \}
\]

I do think it's clear that only two of those four \text should be in sans serif.

8

Which command is best is subject to an open discussion. See for example the FAQ, but also this thread. Concerning the space, I think the manual solution that you posted is indeed the best one; after all, the normal case is to enter spaces manually, and every attempt to introduce some automatic behavior would break the strict separation concerning space handling in text and math mode.

2
  • But there is a space -- it's between the ending } and the next math character. :/ Commented Sep 24, 2010 at 12:45
  • 1
    At that place math mode is active, so spaces are ignored. If the space after the } were significant, the treatment of spaces in math mode would be inconsistent.
    – Philipp
    Commented Sep 24, 2010 at 15:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .