What is the "correct" way of embedding text into math mode?

(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 x = \mbox < number of cats>\] 
\[ \textrm 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"?

253k 70 70 gold badges 730 730 silver badges 796 796 bronze badges asked Sep 23, 2010 at 17:59 Billy ONeal Billy ONeal 10.7k 9 9 gold badges 30 30 silver badges 28 28 bronze badges

4 Answers 4

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. \ ). For example,

\[ \text\ x=\text. \] 
answered Sep 23, 2010 at 18:19 Michael Underwood Michael Underwood 21.8k 13 13 gold badges 53 53 silver badges 41 41 bronze badges

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

\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

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

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

\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

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$> \] 
414 1 1 gold badge 5 5 silver badges 15 15 bronze badges answered Oct 8, 2010 at 15:05

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\> and x = \secondkind . For me, none of this two ones should be written with the outer text mode that \text provides.

\[ \text x = \var \text \var = \frac <\Area(\var\cap \var)> <\Area(\var\cup \var)> \] 

enter image description here

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

\begin Let $x = \var$, and also $\var = \frac <\Area(\var\cap \var)> <\Area(\var\cup \var)>$ \end

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\> 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 x = \text \text \ < x,\ \text\> \] 

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