[Tex/LaTex] Expectation Operator

math-modemath-operators

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?

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

enter image description here

Related Question