There are several commands you can use to insert text into equations, with two of the most common being the text
and mathrm
commands.
To get a clearer picture, let’s go over the syntax and how each of these commands is used.
Using the text command from the amsmath package
The \text
command, which comes from the amsmath
package, lets you easily insert regular text while you’re in math mode.
\documentclass[12pt]{article}
\usepackage{amsmath,lipsum}
\begin{document}
\begin{align*}
x &= \text{some text} + y \\
x &= \text{Velocity} \times \text{Time} \\
&= v \cdot t
\end{align*}
\lipsum[1][1-4]
\begin{align*}
a &= \frac{2}{3} \times 5 \\
&= \text{two-thirds of five} \\
&= \text{approximately} \, 3.33
\end{align*}
\end{document}
Output :
In the output above, you can see how text is placed directly within equations.
Using mathrm command for insert text in equation
The \mathrm
command is great for writing text in a roman font while in math mode. It’s especially handy when labeling things like variables or constants.
\documentclass[12pt]{article}
\usepackage{amsmath,lipsum}
\begin{document}
Let \( f(x) = \mathrm{sin}(x) + \mathrm{cos}(x) \) be a function.
\begin{align*}
F &= m \cdot a \\
&= \mathrm{mass} \times \mathrm{acceleration} \\
&= \mathrm{kg} \cdot \mathrm{m/s}^2 \\
&= \mathrm{Newton}
\end{align*}
\lipsum[2][1-4]
\begin{align}
E &= m \cdot c^2 \\
&= \mathrm{mass} \times \mathrm{speed~of~light}^2 \\
&= \mathrm{kg} \cdot (\mathrm{m/s})^2 \\
&= \mathrm{Joule}
\end{align}
\end{document}
Output :
As shown in the example above, you can include text in equations with the \mathrm
command, which also works well for defining mathematical functions.
Using mbox command
The \mbox{}
command is typically used inside math environments, like equations or alignments, to add descriptive text or labels.
However, it doesn’t automatically add any space around the text, so you might need to add spacing manually with commands like \quad
or \,
when necessary.
\documentclass[12pt]{article}
\usepackage{amsmath}
\begin{document}
\begin{align}
x &= \mbox{Initial position} + \mbox{Velocity} \times \mbox{Time} \\
&= x_0 + v \cdot t \\[8pt]
v &= \frac{\mbox{Total displacement}}{\mbox{Total time}} \\
&= \frac{x_f - x_i}{t} \\[8pt]
F &= \mbox{Mass} \times \mbox{Acceleration} \\
&= m \cdot a
\end{align}
\end{document}
Output :
Using textnormal command
The \textnormal{}
command is used when you want to insert text in its regular font style, even while staying within math mode.
\documentclass[12pt]{article}
\usepackage{amsmath}
\begin{document}
\begin{align*}
x &= \textnormal{Initial position} + \textnormal{Velocity} \times \textnormal{Time} \\
&= x_0 + v \cdot t
\end{align*}
\begin{align*}
\textnormal{Given the function} \quad f(x) &= x^2 + 3x - 2 \\
\textnormal{Find the derivative:} \quad f'(x) &= \frac{d}{dx}(x^2 + 3x - 2) \\
&= \frac{d}{dx}x^2 + \frac{d}{dx}(3x) - \frac{d}{dx}(2) \\
&= 2x + 3 \\
\\
\textnormal{Given the function} \quad g(x) &= \sin(x) + \cos(x) \\
\textnormal{Find the derivative:} \quad g'(x) &= \frac{d}{dx}(\sin(x) + \cos(x)) \\
&= \frac{d}{dx}\sin(x) + \frac{d}{dx}\cos(x) \\
&= \cos(x) - \sin(x) \\
\\
\textnormal{Given the function} \quad h(x) &= e^x \cdot \ln(x) \\
\textnormal{Find the derivative:} \quad h'(x) &= \frac{d}{dx}(e^x \cdot \ln(x)) \\
&= \frac{d}{dx}(e^x) \cdot \ln(x) + e^x \cdot \frac{d}{dx}(\ln(x)) \\
&= e^x \cdot \ln(x) + \frac{e^x}{x} \\
\end{align*}
\end{document}
Output :