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

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}. \]

I'd agree that the AMS text command is the way to go, but don't be afraid to re-enter,

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

so that the spacing is handled correctly.


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.

Tags:

Math Mode