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.

  • 5
    $\begingroup$ Just FYI, you can also try: \$\text{Var}[X|Y]\$ (ie $\text{Var}[X|Y]$), if that's more convenient. $\endgroup$ Oct 5 '12 at 18:08
  • $\begingroup$ 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$. $\endgroup$
    – Zen
    Oct 5 '12 at 18:13
  • 15
    $\begingroup$ 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. $\endgroup$
    – cardinal
    Oct 5 '12 at 18:46
  • 4
    $\begingroup$ (+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.! $\endgroup$
    – Zen
    Oct 5 '12 at 19:03
  • 2
    $\begingroup$ @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? $\endgroup$ Oct 5 '12 at 20:18

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

Here is cardinal's solution. At the beginning of your answer type.




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] $$

Stask suggested

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


which looks pretty.

  • 5
    $\begingroup$ 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. $\endgroup$
    – StasK
    Oct 6 '12 at 2:28
  • 12
    $\begingroup$ @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. :-) $\endgroup$
    – cardinal
    Oct 6 '12 at 13:59
  • $\begingroup$ Yay! Thanks, cardinal. I need to learn my $\LaTeX$ better, apparently :) $\endgroup$
    – StasK
    Oct 6 '12 at 15:08
  • $\begingroup$ Neat solution. :) But I for one would prefer if these commands were available out-of-the-box. $\endgroup$
    – MånsT
    Oct 8 '12 at 12:12
  • 8
    $\begingroup$ (+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. :-) $\endgroup$
    – cardinal
    Oct 8 '12 at 17:35
  • 1
    $\begingroup$ (+1) Another great tip! $\endgroup$
    – Zen
    Oct 8 '12 at 20:15
  • $\begingroup$ How do you use this? I have $\Var{\beta}$ but it misses those square brackets. How can you have them? $\endgroup$ Feb 28 '15 at 21:21
  • 5
    $\begingroup$ @Masi: \newcommand{\Var}[1]{\operatorname{Var}\left[#1\right]} $\endgroup$ Sep 30 '15 at 11:52
  • 4
    $\begingroup$ 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! $\endgroup$
    – 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.

As noted by @cardinal, MathJax supports \newcommand. The best way to use it is to put into the formula where the operator first appears, because putting it into a separate formula creates an annoying empty line (see @Zen's answer). So whenever you first need variance, you can type \newcommand{\Var}{\operatorname{Var}}, e.g.

$$\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)$$

Like Amoeba's answer, this ensures spacing is done correctly in a way that \mathrm doesn't. But see newcommand vs. DeclareMathOperator on TEX.SE: Amoeba's answer apparently has the advantage that newcommand gives more flexibility.

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.)

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .