Expected value, variance and probability in LaTeX
The expected value of a random variable X is usually written \mathbb{E}[X] in LaTeX, a blackboard-bold E from amssymb; variance, covariance and probability have upright operators of their own.
LaTeX command
\mathbb{E}[X]Not the symbol you meant? Draw it to find the command →
Typing up handwritten notes? Convert a whole page of handwriting to LaTeX →
Typing E[X] works but sets an italic E that reads as a variable. \mathbb{E}[X] is the most common modern form; \mathrm{E}[X] and \operatorname{E}[X] give a plain upright E. \mathbb needs the amssymb or amsfonts package. For tall contents, let the brackets grow: \mathbb{E}\left[ \frac{X}{n} \right].
LaTeX has \Pr built in, an upright Pr spaced as an operator, and many texts write \mathbb{P}(A) instead. Variance and covariance have no built-in command: \operatorname{Var}(X) and \operatorname{Cov}(X, Y) set them upright with the right spacing. If you use them often, define \Var and \Cov once in the preamble, as below.
For a conditional expectation or probability, put \mid between the parts: \mathbb{E}[] and \Pr(). \mid adds the spacing of a relation around the bar, which a plain | does not.
Define \E, \Var and \Cov once
With amsmath and amssymb loaded, these preamble lines give short commands for the whole document. \DeclareMathOperator makes Var and Cov upright operators with the right spacing.
\newcommand{\E}{\mathbb{E}}\DeclareMathOperator{\Var}{Var}\DeclareMathOperator{\Cov}{Cov}Other forms
\operatorname{Var}(X)variance\Pr(A)probability, built in\mathbb{P}(A)probability, blackboard P\operatorname{Cov}(X, Y)covariance\mathbb{E}[X \mid Y]conditional expectationExamples
$\mathbb{E}[X] = \sum_{x} x \, \Pr(X = x)$the expectation of a discrete variable
$\mathbb{E}[X] = \int_{-\infty}^{\infty} x f(x) \, dx$the expectation of a continuous variable
$\operatorname{Var}(X) = \mathbb{E}\left[(X - \mu)^2\right]$variance, with brackets that grow
$\operatorname{Cov}(X, Y) = \mathbb{E}[XY] - \mathbb{E}[X]\,\mathbb{E}[Y]$covariance
$\mathbb{E}[X \mid Y = y]$a conditional expectation
$\Pr(A \mid B) = \frac{\Pr(A \cap B)}{\Pr(B)}$conditional probability
Tips and common mistakes
- Use
\mathbb{E}or\mathrm{E}, not a bare E, so the expectation does not read as a variable. - Write Var and Cov with
\operatorname, or define them with\DeclareMathOperator. \Pris built in;\mathbb{P}needs amssymb, like\mathbb{E}.- Use
\midfor conditioning, not a bare |, so the bar gets space on both sides. - Let the brackets grow with
\left[ and\right] when the contents hold a fraction or a sum.
Working from handwritten notes?
LatexNotes turns a photo or PDF of handwritten mathematics into LaTeX, graphs and all. It is built with real pdflatex before you see it, and the .tex file is yours to edit or take to Overleaf. Free to try, no sign-up.
See how handwriting to LaTeX works, step by step, or go from GoodNotes to LaTeX if you write on an iPad (Notability works the same way).
Convert your maths to LaTeX →FAQ
How do I write expected value in LaTeX?
Use \mathbb{E}[X] with the amssymb package, or \mathrm{E}[X] for a plain upright E.
How do I write variance in LaTeX?
Write \operatorname{Var}(X), or define \Var with \DeclareMathOperator{\Var}{Var} in the preamble.
How do I write probability in LaTeX?
\Pr(A) is built in; many texts use \mathbb{P}(A) with amssymb. Write conditional probability as \Pr().
How do I make the brackets bigger?
Put \left and \right before them: \mathbb{E}\left[ \frac{X}{n} \right].