How to Write the erf Function in LaTeX
At a glance
- 1. Load amsmath
Include the amsmath package in the document preamble.
- 2. Use an operator
Write \operatorname{erf}(x) so “erf” appears upright with correct mathematical spacing.
- 3. Define a shortcut
Declare \erf once in the preamble if you use the function repeatedly.
The error function should be typeset as an upright named operator, not as three italic variables. In LaTeX, the clearest one-off form is
\operatorname{erf}(x)
This produces with upright letters and appropriate spacing before the argument. If you use the function several times, define a command in the preamble with \DeclareMathOperator.
The simplest form
For a single occurrence, use \operatorname:
\[
\operatorname{erf}(x) = \frac{2}{\sqrt{\pi}}\int_0^x e^{-t^2}\,\mathrm{d}t
\]
The \operatorname{erf} command is provided by the amsmath package. It treats erf as the name of a mathematical operator, similar to \sin, \log, or \det.
The rendered expression is conventionally written as
The differential \mathrm{d}t is also set upright in this example. Some authors instead write dt; follow the notation convention used in your field or document.
A complete minimal example
Here is a compilable document using amsmath:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
The error function is
\[
\operatorname{erf}(x)
= \frac{2}{\sqrt{\pi}}\int_0^x e^{-t^2}\,\mathrm{d}t.
\]
For example, the Gaussian cumulative distribution function can be written as
\[
\Phi(x) = \frac{1}{2}\left(1 + \operatorname{erf}\left(\frac{x}{\sqrt{2}}\right)\right).
\]
\end{document}
The package required is amsmath, which is part of the standard LaTeX distribution. The nested \left( and \right) commands in the second formula make the parentheses adjust to the fraction inside them.
Define a reusable \erf command
If the error function appears throughout a document, declare it once in the preamble:
\documentclass{article}
\usepackage{amsmath}
\DeclareMathOperator{\erf}{erf}
\begin{document}
\[
\erf(x) = \frac{2}{\sqrt{\pi}}\int_0^x e^{-t^2}\,\mathrm{d}t
\]
\end{document}
After the declaration, \erf(x) is equivalent to \operatorname{erf}(x). The command name begins with a backslash, while the text supplied as the second argument is the printed operator name:
\DeclareMathOperator{\erf}{erf}
This approach is convenient because you can change the notation in one place if your document later requires a different convention.
Why not write erf(x) directly?
In ordinary LaTeX math mode, letters are italicized because they are interpreted as variables. Thus,
erf(x)
is treated as three adjacent variables, , rather than as the name of one function. It also does not receive the same operator spacing as standard functions such as \sin(x) or \exp(x).
Similarly, \mathrm{erf}(x) makes the letters upright but does not explicitly declare erf as a mathematical operator. It is acceptable for a quick expression, but \operatorname{erf} or a declared operator better communicates the intended mathematical role.
Common mistakes
Using \text{erf} without loading the right package
The \text command is associated with amsmath and is intended for text inside mathematics. It can produce upright letters, but it is not the usual tool for named mathematical functions:
\text{erf}(x)
Prefer \operatorname{erf}(x) for mathematical notation.
Declaring the command inside the document body
Put \DeclareMathOperator in the preamble, before \begin{document}. Defining it there makes the command available consistently throughout the document.
Confusing the function with its complementary version
The complementary error function is conventionally written \operatorname{erfc} or, with a declaration,
\DeclareMathOperator{\erfc}{erfc}
Then use \erfc(x). It is a different function, defined by
If both functions are needed, declare both commands:
\DeclareMathOperator{\erf}{erf}
\DeclareMathOperator{\erfc}{erfc}
For most documents, the recommended reusable notation is therefore:
\usepackage{amsmath}
\DeclareMathOperator{\erf}{erf}
followed by \erf(x) wherever the error function occurs.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.