86

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.

1

3 Answers 3

72

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$}
7
  • In my \text can be noticeably slower than \hbox/\mbox. Is this so? Of course, when proper scaling is needed, choosing \text might simply be necessary. Jun 7, 2013 at 21:45
  • @LoverofStructure: Yes, thanks, there is an efficiency impact, because \text needs the service of \mathchoice to get the right math style. I have updated the answer. Jun 7, 2013 at 22:00
  • 1
    @LoverofStructure: Package amstext contains \let\nfss@text\text. Thus all commands that use \nfss@text work with correctly sized text in math, e.g. all commands defined via \DeclareTextFontCommand. Jul 4, 2013 at 12:56
  • 1
    And in latex.ltx the following commands are defined using \DeclareTextFontCommand: \textrm, \textsf, \texttt, \textnormal, \textbf, \textmd, \textit, \textsl, \textsc, \textup, \emph Dec 26, 2013 at 18:46
  • 1
    @cfr None does support line breaking in math mode. Either the text ends up in text mode (\mbox or \nfss@text, both are using the unbreakable \hbox) or the material goes into a unbreakable math subformula (\mathrm). Jul 23, 2015 at 13:30
19

I personally consider Herbert Voss' mathmode document one of the definitive guides to mathmode.

He covers the differences in Section 9, pg. 27

enter image description here

If you've installed TeX Live, you can access it on your machine using texdoc mathmode, in MiKTeX you will get it with texdoc voss-mathmode.

4
  • 5
    Actually \textrm doesn't force upright shape, it only sets the "roman family" attribute and leaves the others as they were (using those holding outside the formula, if it appears in math mode). So \textit{abc $\textrm{def}$} will print "def" in italics.
    – egreg
    Sep 8, 2012 at 7:36
  • 1
    Please also consider section 37.1 where he also considers amsmath's \text: "The \text macro can be used at any place [..]" Or just read his answer to this very question :-)
    – usr1234567
    Jan 12, 2016 at 7:23
  • Is this still up to date? The texdocs give me a different pdfs that are not the mathmode document.
    – Blubber
    Apr 2, 2021 at 15:49
  • @Blubber perhaps not, feel free to edit as you see fit. This answer is nearly 9 years old at time of writing. Do always feel free to make things better :)
    – cmhughes
    Apr 2, 2021 at 17:00
9

without using any package.

\documentclass{article}
\begin{document}

$Math - \mathrm{upright} - \textrm{upright} - \mbox{upright}$  \par\Large
$A^{\mathrm{text}}_{\mathrm{text}}$ \quad $A^{\textrm{text}}_{\textrm{text}}$\quad
$A^{\mbox{text}}_{\mbox{text}}$     \quad $A^{\textnormal{text}}_{\textnormal{text}}$

\end{document}

enter image description here

However, for text one should use amsmath and the \text command which uses the correct font size

You must log in to answer this question.

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