Is there a way to easily compute the erf function (or the cumulative distribution function of the normal law) in LaTeX?

Currently, I use pgf to make computation, but I did not find a way to compute erf using pgf.

I would be happy to use any package that is available to compute erf, or any custom solution to compute that function.

  • These references might help you: How to Draw CDF of normal distribution in Tikz and Draw a bivariate normal distribution in TikZ. Actually, there are other useful answers (I've read another interesting one by Jake, but I can't find it now) on the site: perhaps a deeper search is worth. Apr 7, 2013 at 18:50
  • I don't want to draw the function, I want to compute and typeset the values. I read that link, but it's not really useful for what I need. Thanks
    – Xoff
    Apr 7, 2013 at 18:54
  • 1
    @Xoff You can create a pgfmath function very easily with the Taylor expression of the erf function. The precision won't be that good anyway as TeX is not meant for math. (Although certain floating point packages/libraries may help here to a certain degree.) Apr 7, 2013 at 19:02
  • Sure, I can do it myself. But as it's a very common function in some mathematical area, I hope someone already did something good to compute it.
    – Xoff
    Apr 7, 2013 at 19:33

7 Answers 7


For precise values, I recommend externalizing the calculation, here gnuplot is used.

Code (needs --shell-escape enabled)

\def\qrr@split@result#1 #2\@qrr@split@result{\edef\erfInput{#1}\edef\erfResult{#2}}
    \immediate\write18{gnuplot -e "set print '#1'; print #2, erf(#2);"}%
    \edef\qrr@temp{\@@input #1 }%
\foreach \x in {-50,...,50}{%
$ x = \pgfmathresult = \erfInput, \erf(x) = \erfResult$\par


enter image description here

  • 3
    Here is a comparison of cjorssen’s (good!) answer and gnuplot’s values Apr 7, 2013 at 20:36
  • I get a strange error: runsystem(gnuplot -e "set print 'erf.eval'; print -50/50., erf(-50/50.);")...quotation error in system command (tl2012, debian)? Never had it before.
    – cjorssen
    Apr 7, 2013 at 20:55
  • 2
    It's fixed in the CVS version but pgfmath,pgffor is not enough to compile in v2.10 official release. One needs to load pgf,pgffor to get everything otherwise pgfkeys etc. don't work. There was a dependency leak which was fixed some time ago in the CVS version.
    – percusse
    Apr 7, 2013 at 21:31

Based on this answer.

    \pgfmathparse{#1 > 0 ? 1 : -1}%
      1 - (((((1.061405429*\t -1.453152027)*\t) + 1.421413741)*\t 
      -0.284496736)*\t + 0.254829592)*\t*exp(-(\x*\x))}%
\begin{tikzpicture}[yscale = 3]
  \draw[very thick,->] (-5,0) -- node[at end,below] {$x$}(5,0);
  \draw[very thick,->] (0,-1) -- node[below left] {$0$} node[at end,
  left] {$erf(x)$} (0,1);
  \draw[red,thick] plot[domain=-5:5,samples=200] (\x,{erf(\x)});

enter image description here

  • I up this answer, thank you. I was hoping something more precise was implemented.
    – Xoff
    Apr 7, 2013 at 21:21
  • @Xoff You can also use the gnuplot engine to plot in TikZ/pgfplots. Apr 8, 2013 at 21:31
  • 3
    @Xoff That formula (7.1.26. in A Handbook of Mathematical Functions by Abramowitz and Stegun) has an error bounded by $1.5 \times 10^{-7}$. I don't think it's possible to get closer in TeX. Apr 17, 2013 at 16:00
  • Please add the definition of error function you are plotting?
    – Leo
    Apr 11, 2017 at 19:23

Using the same approximation idea as cjorssen (I tried the Taylor series as Qrrbrbirlbel suggested but it's pretty hopeless to get a decent approximation this way) I rewrote the function without using low-level PGF. Because we have so many 2D plots here already, I'll just use my 3D plot that I had already.

colormap={bluewhite}{ color(0cm)=(rgb:red,18;green,64;blue,171); color(1cm)=(white)}
    declare function={erf(\x)=%
    declare function={erf2(\x,\y)=erf(\x)+erf(\y);}
    colormap name=bluewhite,
    y domain=-3:3,
    unit vector ratio*=1 1 1,
    colorbar style={
        anchor=south west,
        height=0.25*\pgfkeysvalueof{/pgfplots/parent axis height},
\addplot3 [surf,shader=faceted] {erf2(x,y)};

3D plot of erf(x)+erf(y)

The approximation has a maximum error of 1.5·10-7 (source).

Thanks to Jake for spotting and fixing the wrong syntax I first had in this code.


A little late, but here's a solution using the sagetex package, which gives you the power of the (free) computer algebra system known as Sage. Sage has an error function command which, according to the documentation is 1-erf. This code

Sage has an error function command that's equal to $1-\mbox{erf}$. Here's     $\sage{error_fcn(2)}$ and 
$\sage{1-error_fcn(2)}$. There is an erf command but it doesn't seem to be   working properly 
in my older version of Sage. The formatting in Sage goes like this:   $\sage{n(error_fcn(2),digits=6)}$ 
and $\sage{n((1-error_fcn(2)),digits=11)}$. 

This is a plot of $\mbox{erf}(x)$.

creates this output: enter image description here

  • I did not find a way to get this package with texlive. Did I miss something ?
    – Xoff
    Apr 10, 2013 at 16:00
  • You need to install Sage on your computer. The download link is here. Once you installed you just need to copy sagetex.sty to the place where you latex your document: see here and here for more details. Sagetex and TexLive mentioned at 2nd "here" link.
    – DJP
    Apr 10, 2013 at 22:51

Error function erf(x) computation and figure anatomy (axes,legends and labels) have been rendered in three approaches.

  1. Fully gnuplot
  2. pgfplots invokes gnuplot
  3. Fully Matlab

Already there are good answers for example by Qrrbrbirlbel and cjorssen, both exploit pgfmath at macro level.

1. Fully gnuplot

Error function erf(x) computation in gnuplot with axes, legends and labels rendered in gnuplot epslatex terminal. The gnuplot terminal output is embedded automatically with gnuplottex package. terminal=pdf does not render Math labels hence epslatex terminal was used.

  set grid
  set size square
  set key left 
  set title 'Error function in gnuplot  $ erf(x) = \frac{2}{\sqrt{\pi}} \int_{0}^{x}e^{-t^{2}}\, dt$'
  set samples 50
  set xlabel "$x$"
  set ylabel "$erf(x)$"
  plot [-3:3] [-1:1] erf(x) title 'gnuplot' linetype 1 linewidth 3

1) gnuplot output figure enter image description here

2. pgfplots invokes gnuplot

Error function erf(x) computation in gnuplot invoked by pgfplots and axes, legends, labels are rendered by pgfplots

\begin{axis}[xlabel=$x$,ylabel=$erf(x)$,title= {Error function in pgfplots $erf(x)=\frac{2}{\sqrt{\pi}}\int_{0}^{x}e^{-t^{2}}\, dt$},legend style={draw=none},legend pos=north west,grid=major,enlargelimits=false]
\addplot [domain=-3:3,samples=50,red,no markers] gnuplot[id=erf]{erf(x)};
% Note: \addplot function { gnuplot code } is alias for \addplot gnuplot { gnuplot code };

2. pgfplots(gnuplot backend)output figure

enter image description here

3) Fully Matlab

Error function $erf(x)$ computation in Matlab with axes,legends,labels rendered using matlabfrag(psfrag tag based) and mlf2pdf functions.

Note: Fonts are frozen in PDF figure unlike the above approaches, but can be changed in mlf2pdf.m before generating them.

**erf(x) Matlab Script using mlf2pdf(matlabfrag as backend) to generate pdf **

clear all
% Plotting section

% x and y data

grid on
axis([-3 3 -1 1]);
title('Error function in Matlab $erf(x)=\frac{2}{\sqrt{\pi}}\int_{0}^{x}e^{-t^{2}}\, dt$','Interpreter','none');

3. Output Figure enter image description here

gnuplot 4.4,pgfplots 1.8 and pdflatex -shell-escape engine were used.

  • I went ahead with the second implementation here. I had to figure out something very simple but that wasn't working at the beginning. You have to call pdflatex with a special option like this: pdflatex -shell-escape yourfile.tex; also, you need to have gnuplot installed, which I didn't know I didn't have. Other than that. A great solution. May 5, 2016 at 18:48

You can also use the FFI in LuaJITTeX (and LuaTeX ≥ 1.0.3) to access the erf function in libm directly. The performance and accuracy are simply amazing!



local ffi = require("ffi")
double erf(double arg);




    no marks, samples=101,
    \addplot {erf(x)};


enter image description here

  • Really nice trick! One naive question: how does luatex/ffi knows where to look for (here libm)?
    – cjorssen
    Jan 22, 2019 at 15:49
  • 1
    @cjorssen The FFI just resolves symbols like a compiler. Because libm is statically linked into LuaTeX (at least in vanilla TeX Live), you can access it via ffi.C. If the symbol comes from a DSO, you have to load it first using ffi.load and then resolve the symbol name through this handle. You can have a look at these two answers of mine for examples: tex.stackexchange.com/a/403794, tex.stackexchange.com/a/408014 Jan 22, 2019 at 20:18
  • Hi, can you write it to LuaLatex??? thanks!! Jan 24, 2021 at 20:38

PSTricks solution, using package pst-ode, command \pstODEsolve.

The integral is thought of as an initial value problem and solved (integrated) numerically between 0 and x using RKF45 method with automatic stepsize control.

enter image description here


% erf
% #1: x
% #2: PS variable for result list
  \pstODEsolve[algebraicAll]{retVal}{ %`retval' takes initial value and erf(#1)
    2/sqrt(Pi)*y[0] %post-multiply result of integration with 2/sqrt(Pi)
  }{0}{#1}{ % integration interval
    2 % two output points (at t=0 and t=#1)
    0.0 % initial value at t=0
  }{Euler^(-t^2)} %integrand
  %  initialise, if necessary, empty solution list given as arg #2
  \pstVerb{/#2 where {pop}{/#2 {} def} ifelse}
  %  From `retVal', we throw away result at t=0, and append x (arg #1) and erf(x) to our
  %  solution list (arg #2):
  \pstVerb{/#2 [#2 #1 retVal exch pop] cvx def}


%solve function integral, 121 plotpoints: -3, -2.95, ..., 2.95, 3
  \erf(\nx){erf} %result appended to solution list `erf'

%plot result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.