[Tex/LaTex] Difference between various methods for producing text in math mode

comparisonmath-moderomantext-mode

What is the difference between \text, \textrm, \textnormal, and \mbox (and possibly other commands commonly used to produce text in math mode)?

Various (La)TeX tutorials teach different methods, and I am sure that there is either a best one or there are subtly different usage scenarios.

Best Answer

Update: Example added without amstext to show the effect of this package on the other \text... commands.

Example file without package amstext (or amsmath):

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{tgpagella}

\newcommand*{\test}[1]{%
  $\csname #1\endcsname{#1}^{\csname #1\endcsname{#1}}$%
}
\renewcommand*{\arraystretch}{1.5}

\begin{document}
\sffamily\bfseries\itshape Text mode: sans serif, bold, italics.

\bigskip
\begin{tabular}{@{}l@{}}
  \test{mbox}\\
  \test{textrm}\\
  \test{textnormal}\\
  \test{mathrm}
\end{tabular}

\end{document}

Differences without amstext

Example file with package amstext:

\documentclass{article}
\usepackage{amstext}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{tgpagella}

\newcommand*{\test}[1]{%
  $\csname #1\endcsname{#1}^{\csname #1\endcsname{#1}}$%
}
\renewcommand*{\arraystretch}{1.5}

\begin{document}
\sffamily\bfseries\itshape Text mode: sans serif, bold, italics.

\bigskip
\begin{tabular}{@{}l@{}}
  \test{mbox}\\
  \test{text}\\
  \test{textrm}\\
  \test{textnormal}\\
  \test{mathrm}
\end{tabular}

\bigskip
$ \textnormal{[a b c \"a \ss] becomes }
  \mathrm{[a b c \"a \ss]}
  \textnormal{ in mathrm}
$

\end{document}

Differences with amstext

  • \mbox uses the current text font. The size is constant and does not change in subscripts, superscripts, fractions, …

  • \text of package amstext (or amsmath) uses the current text font, but adapt the size according to the current math style. Therefore it needs \mathchoice that has an efficiency impact that the text is set four times for all math styles and later, when TeX knows the math style, it chooses the right version.

  • \textrm uses the text roman font, but the other font parameters like encoding, shape and series are taken from the current text font. Font size is only adapted to the current math style if package amstext (or amsmath) is loaded.

  • \textnormal uses \normalfont, the size is only adapted to the currrent math style if package amstext (or amsmath) is loaded.

  • \mathrm uses the math roman font that might differ from the text roman font. Also the spacing is done according to math and the encoding differs from the text encoding.

For text in math I would load package amstext (or amsmath) and use \text or \textnormal. \text is shorter to type, but the dependency on the current text font might be sometimes an advantage, sometimes a disadvantage. For subscripts I would prefer \textnormal:

v_{\textnormal{car}} \ge v_{\textnormal{bicycle}}

But additional text might be better set in the current text font:

a \stackrel{\text{according to \dots}}{=} \frac{b}{c} \quad \text{for $c \ne 0$}
Related Question