Text in math mode in LaTeX (text, mathrm)
\text{...} from amsmath puts normal text into a formula. The text keeps its spaces, uses the font of the surrounding text and shrinks in subscripts and superscripts.
Preamble
The snippets on this page need these lines before \begin{document}:
\usepackage{amsmath}Words inside a formula
Outside \text, math mode ignores spaces and sets letters as italic variables. Inside \text spaces count, so put the space next to the formula inside the braces: \text{ if }.
\[
x^2 \ge 0 \quad \text{for all } x \in M
\qquad
f(x) = 0 \text{ if and only if } x = 0
\]Change the formula
The preview is drawn in your browser by KaTeX, which knows the common math commands. The editor compiles the formula with pdfTeX.
Words as subscripts
\text adapts to the size of a subscript or superscript. \mbox{...}, which works without amsmath, stays at full size there.
\[
T_{\text{max}}
\qquad
k_{\text{B}}
\qquad
x_{\text{old}}
\]Change the formula
The preview is drawn in your browser by KaTeX, which knows the common math commands. The editor compiles the formula with pdfTeX.
When to use mathrm instead
\mathrm{...} sets upright math letters: it ignores spaces and stays upright whatever the surrounding text looks like. Use it for symbols such as an upright d, e or i, and \text for words.
\[
\mathrm{e}^{\mathrm{i}\pi} = -1
\qquad
\text{speed} = \frac{\text{distance}}{\text{time}}
\]Change the formula
The preview is drawn in your browser by KaTeX, which knows the common math commands. The editor compiles the formula with pdfTeX.
Bold or italic text in a formula
The text commands \textbf, \textit and \texttt work in math mode in the same way as \text.
\[
a = b \quad \textbf{(by definition)}
\qquad
x \in A \;\textit{or}\; x \in B
\]Change the formula
The preview is drawn in your browser by KaTeX, which knows the common math commands. The editor compiles the formula with pdfTeX.
Pitfalls
\texttakes the font of the surrounding text. In a theorem that is set in italics,\text{if}is italic too;\textnormal{if}stays upright.\textwithoutamsmathstops with Undefined control sequence.- A formula inside
\textneeds math mode again:\text{for $x > 0$}.
Compiled with pdfLaTeX from TeX Live 2023.
Related pages
- Package
amsmath - How-to Piecewise functions in LaTeX
- How-to Math fonts in LaTeX
- How-to Spacing in math mode in LaTeX
- Error Undefined control sequence