# Expectation Operator

I use the following command to typeset E[A|B]

\documentclass{article}
\usepackage{mathtools}

\DeclareMathOperator{\ExpOp}{E}
\DeclarePairedDelimiterX{\ExpArg}[1]{[}{]}{#1}
\newcommand{\Exp}{\ExpOp\ExpArg*}

\begin{document}
% usage:
$\Exp{A \mid B}$
\end{document}


Is it possible to define \Exp to accept an optional size parameter like \bigg for the brackets?

• \mid is not a good symbol, 2 min and I'll post what I recommend to our users. BTW: always post full minimal examples. – daleif Feb 19 '15 at 14:12

## 1 Answer

Since \mid does not make much sense then you read \Exp{A \mid B} (and cannot be scaled), I suggest hiding the | inside a specially crafted macro giving us a macro that support conditionals including scaling.

\documentclass[a4paper]{memoir}
% requires 2014 edition of mathtools
\usepackage{amsmath,amssymb,mathtools,bm,etoolbox}

\providecommand\given{}
\DeclarePairedDelimiterXPP\Aver[1]{\mathbb{E}}{[}{]}{}{
\renewcommand\given{  \nonscript\:
\delimsize\vert
\nonscript\:
\mathopen{}
\allowbreak}
#1
}

\begin{document}

\begin{align*}
\Aver{A}
\\
\Aver{A \given B }
\\
\Aver*{\frac{A^2}{B^2+1}}
\\
\Aver[\bigg]{\frac{A^2}{B^2+1} \given c }
\end{align*}

\end{document}


Addition, if you do not have the 2014 version of mathtools, you can use this

\DeclarePairedDelimiterX\AverX[1]{[}{]}{
\renewcommand\given{  \nonscript\:
\delimsize\vert
\nonscript\:
\mathopen{}
\allowbreak}
#1
}

\newcommand\Exp{\mathbb{E}\AverX}


I'd might even consider using \operatorname{\mathbb{E}}

As far as I understand \nonscript\: makes \: disappear in a script context. Since \vert is a symbol not a fence, \vert - B does not give the minus sign but rather an addition minus (wrong spacing). \mathopen makes a potential - behave as a sign rather than an addition. \allowbreak just allows line breaks after \given. Remove if not relevant (ie all conditionals are short).

• Could you explain why \nonscript, \mathopen and \allowbreak (and what are they supposed to do). Nice answer, by the way. – Manuel Feb 19 '15 at 14:22
• @Manuel, see update – daleif Feb 19 '15 at 14:28
• Thank you! I suppose you use more operators like this. If you ever publish these please let me know. – Manuel Schmidt Feb 19 '15 at 16:38
• @ManuelSchmidt, lots, it is also useful for a probability operator (it is obvious from the code how to do that) and a \Set macro that use the same syntax \Set{ x\in A \given x > 0} – daleif Feb 19 '15 at 16:46
• Regarding the second part of the answer (if you do not have the 2014 version of mathtools), where exactly would you place that snippet? (preamble? inside the document body? i.e. before or after \begin{document}?) Sorry, just run into this solution and am trying to familiarize myself with these types of declarations. – Josh Apr 30 '15 at 13:02