# $\LaTeX$ macros for expectation, variance and covariance?

Is it possible for Stack Exchange to make available $\LaTeX$ abbreviations for \mathrm{E}, \mathrm{Var}, \mathrm{Cov} (possibly with new commands \E, \Var and \Cov)? The problem is that things like $\mathrm{Var}[X\mid Y]$ look much better than $Var[X\mid Y]$, but we end up losing a lot of time typing that.

• Just FYI, you can also try: \$\text{Var}[X|Y]\$ (ie $\text{Var}[X|Y]$), if that's more convenient. Oct 5 '12 at 18:08
• gung's suggestion saves two keystrokes, which is good, but I was wondering if \Var would make life easier for us. I understand that it would not be universally used, since it is not plain $\LaTeX$.
– Zen
Oct 5 '12 at 18:13
• In my answers I often declare such a macro using \newcommand at the beginning and then use the macro in the rest of the answer. That's often helpful, especially if you need to use it many times. Oct 5 '12 at 18:46
• (+1) Can I use \newcommand in my answers?!?!?!?!? Whoooaaaa! Problem solved. I'll setup a txt with definitions I normally use. Thank you a lot, Prof.!
– Zen
Oct 5 '12 at 19:03
• @cardinal, I'm not familiar w/ \newcommand, since it seems that is the answer to this Q, can you make it official, perhaps w/ a brief tutorial re how it would work? Oct 5 '12 at 20:18


$\newcommand{\E}{\mathrm{E}}$

$\newcommand{\Var}{\mathrm{Var}}$

$\newcommand{\Cov}{\mathrm{Cov}}$

After that, just use it. For example:

$$\Var[X] = \E[\Var[X\mid Y]] + \Var[[\E\mid X]]$$

$$\Cov[X,Y] = \E[XY] - \E[X]\E[Y]$$

$\newcommand{\Expect}{{\rm I\kern-.3em E}}$

$$\Expect[X]$$

which looks pretty.

• A somewhat fancier solution is $\newcommand{\Expect}{{\rm I\kern-.3em E}}$ that produces $\Expect[X]$. Unfortunately, you cannot easily extend it to the variance operator. Oct 6 '12 at 2:28
• @Stas: That's an old hack for \mathbb E, i.e., $\mathbb E$. This does extend to variance $\mathbb V(X)$ or similar, as you like. The latter doesn't seem too popular though. :-) Oct 6 '12 at 13:59
• Yay! Thanks, cardinal. I need to learn my $\LaTeX$ better, apparently :) Oct 6 '12 at 15:08
• Neat solution. :) But I for one would prefer if these commands were available out-of-the-box. Oct 8 '12 at 12:12
• (+1 a couple days ago) As a side note, if you want to avoid the empty space at the top of the answer caused by the macro definitions, you can just slip the \newcommand calls in at the beginning of the first math-mode block you use in your answer. :-) Oct 8 '12 at 17:35
• (+1) Another great tip!
– Zen
Oct 8 '12 at 20:15
• How do you use this? I have $\Var{\beta}$ but it misses those square brackets. How can you have them? Feb 28 '15 at 21:21
• @Masi: \newcommand{\Var}[1]{\operatorname{Var}\left[#1\right]} Sep 30 '15 at 11:52
• Beware Although \newcommand is elegant and convenient, it appears to affect all $\TeX$ markup on the entire page--not just your individual post! This raises the prospect of bad interactions arising from conflicting commands in different posts (and even in comments). I suspect their processing might depend on the sequence in which the posts appear on the page. Thus, both arbitrary and future developments beyond your control could ruin your post!
– whuber Mod
Jul 8 '16 at 18:48

To typeset an operator, it is much better to use \operatorname instead of \mathrm, as explained on tex.SE: What's the difference between \mathrm and \operatorname?

Compare (first line uses \mathrm, second line uses \operatorname): $$\mathrm{Var}[x] + 2\mathrm{Var}[y] - \mathrm{E}[x]\mathrm{E}[y]$$ $$\operatorname{Var}[x] + 2\operatorname{Var}[y] - \operatorname{E}[x]\operatorname{E}[y]$$ and notice small white spaces before the operator name in the second line.


$$\newcommand{\Var}{\operatorname{Var}} \Var[x]+\Var[y]$$


and this will be displayed as

$$\newcommand{\Var}{\operatorname{Var}} \Var[x]+\Var[y]$$

All subsequent formulas can simply use \Var in them, e.g. $\Var[z]$ will become $\Var[z]$.

For Corr, Cov and Var I usually use \DeclareMathOperator{\Corr}{Corr}, which, done once in an answer, allows me to just write \Corr(X,Y) whenever I need it later, which displays:

$$\DeclareMathOperator{\Corr}{Corr} \Corr(X,Y)$$


But note I can still do \DeclareMathOperator{\E}{\mathbb{E}} \E(X,Y) and produce:

$$\DeclareMathOperator{\E}{\mathbb{E}} \E(X,Y)$$

And of course I can reuse these in a later piece of $\LaTeX$ too: $\E(X)+\Corr(X,Y)$

(In a document DeclareMathOperator can only be used in a preamble but this doesn't seem to cause any difficulty on here.)

• +1. This is perhaps a better way because it's slightly shorter: \DeclareMathOperator{\Corr}{Corr} vs. \newcommand{\Corr}{\operatorname{Corr}}. Sep 13 '15 at 21:40