# Declare a flexible math operator (such as expectation operator)

I would like to define a math operator, such as the expectation operator, which will be able to optionally receive subscript and/or superscript.

Currently what I have is

\newcommand{\E}[1]{\mathbb{E}\left(#1\right)}


and I call it with \E{X}. This is nice, but when I wish to add a subscript I fall to \mathbb{E}_x\left(X\right) and it's quite annoying.

I therefore defined another command:

\newcommand{\Ex}[2]{\mathbb{E}_{#1}\left(#2\right)}


but that's not really helpful for two reasons: (a) I need to remember two commands, and (b) the usage is not intuitive (I use it with \E{x}{X} instead of something like \E_x{X}.

Can I define a math operator whose usage will be either of the following:

• \E{X}
• \E_x{X}
• \E^2{X}

and whose output in each case would be

• \mathbb{E}\left(X\right)
• \mathbb{E}_x\left(X\right)
• \mathbb{E}^2\left(X\right)

It this possible?

Note that DeclareMathOperator* is not good for the task, as in display mode it puts the sub/superscripts below/above the operator.

• Have you considered using \DeclareMathOperator (without the asterisk)? – Mico Sep 8 '15 at 15:35

Implementation for \E, which has optional sub- or superscripts before the mandatory argument. It uses \mleft and \mright to avoid additional horizontal spacing.

\documentclass{article}
\usepackage{mleftright}
\usepackage{amssymb}

\makeatletter
\newcommand*{\E}{%
\def\E@sub{}%
\def\E@sup{}%
\E@scripts
}
\newcommand*{\E@scripts}{%
\@ifnextchar_\E@subscript{%
\@ifnextchar^\E@supscript\E@finish
}%
}
\def\E@subscript_#1{%
\ifx\E@sub\@empty
\def\E@sub{#1}%
\else
\fi
\E@scripts
}
\def\E@supscript^#1{%
\ifx\E@sup\@empty
\def\E@sup{#1}%
\else
\fi
\E@scripts
}
\newcommand*{\E@finish}[1]{%
\mathbb{E}%
\ifx\E@sub\@empty\else _{\E@sub}\fi
\ifx\E@sup\@empty\else ^{\E@sup}\fi
\mleft(#1\mright)%
}
\makeatother

\begin{document}
$\E{X}, \E_x{X}, \E^2{X}, \E_x^2{X}, \E^2_x{X}$
\end{document}


• Wow, I wasn't expecting such a long implementation for such a common task... Thanks! – Bach Sep 8 '15 at 14:34

I wouldn't make the macro \E take an explicit argument. Instead, I would define \E via a \DeclareMathOperator statement and then write \E(X), \E X, etc. That way, you can decide "on the fly" whether or not to use parentheses.

Note that this approach requires no extra typing during input: You simply replace \E{X} with \E(X) -- exact same number of characters. :-)

\documentclass{article}
\usepackage{amsmath,amssymb}
\DeclareMathOperator{\E}{\mathbb{E}}
\begin{document}
$\E(K)$, $\E_x(K)$, $\E^2(K)$, $\E_x^2(K)$

\medskip
$\E K$, $\E_x K$, $\E^2 K$, $\E_x^2 K$
\end{document}

• I couldn't agree more. I would argue that this is actually even more flexible than what the OP wanted. And it its implementation is much, much easier (though less interesting from a TeXnical point of view ;-)). What one loses though is the ability to change the brackets after the expectation globally (there seem to be people who prefer \E(X) and some that prefer \E[X]). – moewe Sep 8 '15 at 17:14
• Thanks for another point of view! However, I tend to use \left(... or rather \mleft(... and I never use these operators without parenthesis. So Heiko's answer is more appropriate in my case (even though the implementation is really crazy). – Bach Sep 8 '15 at 17:15