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.
3 Answers
$\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.
$\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] $$
Stask suggested
$\newcommand{\Expect}{{\rm I\kern-.3em E}}$
$$\Expect[X]$$
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$– StasKCommented Oct 6, 2012 at 2:28 -
13$\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$– cardinalCommented Oct 6, 2012 at 13:59 -
$\begingroup$ Yay! Thanks, cardinal. I need to learn my $\LaTeX$ better, apparently :) $\endgroup$– StasKCommented Oct 6, 2012 at 15:08
-
$\begingroup$ Neat solution. :) But I for one would prefer if these commands were available out-of-the-box. $\endgroup$– MånsTCommented Oct 8, 2012 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$– cardinalCommented Oct 8, 2012 at 17:35 -
1
-
$\begingroup$ How do you use this? I have $\Var{\beta}$ but it misses those square brackets. How can you have them? $\endgroup$ Commented Feb 28, 2015 at 21:21
-
5$\begingroup$ @Masi:
\newcommand{\Var}[1]{\operatorname{Var}\left[#1\right]}
$\endgroup$– Scortchi ModCommented Sep 30, 2015 at 11:52 -
6$\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 ModCommented Jul 8, 2016 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$– amoebaCommented Sep 13, 2015 at 21:40 -
$\begingroup$ This appealing answer; however, I've noticed that declaring operator through
\DeclareMathOperator{\Cov}{Cov}
printsCov
using the document font (Arial in my case), it would be more presentable if theCover
would be printed with the same font as the text generated via\log
and other similar commands. $\endgroup$– KonradCommented Feb 28, 2022 at 17:06
\Var
would make life easier for us. I understand that it would not be universally used, since it is not plain $\LaTeX$. $\endgroup$\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$\newcommand
in my answers?!?!?!?!? Whoooaaaa! Problem solved. I'll setup a txt with definitions I normally use. Thank you a lot, Prof.! $\endgroup$\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$