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.

x2≥0for all x∈Mf(x)=0 if and only if x=0x^2 \ge 0 \quad \text{for all } x \in M \qquad f(x) = 0 \text{ if and only if } x = 0

Open this formula in the editor

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.

TmaxkBxoldT_{\text{max}} \qquad k_{\text{B}} \qquad x_{\text{old}}

Open this formula in the editor

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.

eiπ=−1speed=distancetime\mathrm{e}^{\mathrm{i}\pi} = -1 \qquad \text{speed} = \frac{\text{distance}}{\text{time}}

Open this formula in the editor

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.

a=b(by definition)x∈A  or  x∈Ba = b \quad \textbf{(by definition)} \qquad x \in A \;\textit{or}\; x \in B

Open this formula in the editor

Pitfalls

Compiled with pdfLaTeX from TeX Live 2023.

Related pages