2

I have the following command for conditional expectations

\documentclass{article}
\newcommand{\E}[2]{E\left[ #1 \:|\: #2 \right]}

\begin{document}
\[ \E{\sum_{i=1}^N x_n}{Y} \]
\end{document}

The problem is that the | character looks ugly as it does not match the size of the surrounding square brackets. I would ideally put \big in front of it in this particular instance. I was wondering if there is a way to get the size of the text between \left and \right and use that to increase the size of | by some percentage.

Mico
  • 506,678
Tohiko
  • 1,789

3 Answers3

3

Adapted from documentation of mathtools , § 3.6, Paired Delimiters, pp. 25–29. (Thanks to @Bernard).

\documentclass{article}
\usepackage{mathtools}

\makeatletter
\providecommand\given{}
\newcommand\@given[1][]{%
  \nonscript\:#1\vert \allowbreak \nonscript\:\mathopen{}}
\DeclarePairedDelimiterXPP\E[1]{E}[]{}{%
\renewcommand\given[1][\delimsize]{\@given[##1]}#1}
\makeatother

\begin{document}
\[ \E*{\sum_{i=1}^N x_n \given Y} \]
\end{document}
Tohiko
  • 1,789
  • This solution resizes the pipe (|), but does not scale the brackets to enclose the contents the way \left[ and \right] would. Is there a way to also accomplish that? – László Oct 02 '19 at 14:25
1

Here's another solution, also based on the mathtools package, but now making use of the \DeclarePairedDelimiterX macro as well as the \given and \Set macros set up on page 27 of the package's user guide.

The main user macro in the code below is called \expect. Observe that it inserts half of a thinspace to the right of the opening square bracket and to the left of the closing square bracket.

Incidentally, I think the E symbol ("expectation", right?) should be typeset as an upright-Roman character since it represents a math operator.

\documentclass{article}
\usepackage{mathtools}% for "\DeclarePairedDelimiterX" macro

% Expectation operator
\DeclareMathOperator{\E}{E} 

%% Three auxiliary macros: \given, \SetSymbol, and \Set
\providecommand\given{}
\newcommand\SetSymbol[1][]{% 
    \nonscript\:#1\vert\allowbreak\nonscript\:\mathopen{}}
\DeclarePairedDelimiterX\Set[1]\lbrack\rbrack{%
   \renewcommand\given{\SetSymbol[\delimsize]}#1}

%% Main user macro: \expect
\newcommand\expect[2][]{\E\Set[#1]{\mkern1.5mu#2\mkern1.5mu}}

\begin{document}

\[ 
\expect[\bigg]{\sum_{i=1}^N x_n\given Y} 
\qquad 
\expect[\Big]{\sum_{i=1}^N x_n}
\]

\end{document}
Mico
  • 506,678
  • 1
    What Tohiko is doing is essentially the same thing. You could probably just wave added \operatorname to that solution. Btw: I've seen people use both upright, kursive and mathbb versions of E – daleif Oct 16 '17 at 15:42
  • @daleif - Good point about there being many options to denote "E" as the expectation operator. The common theme, I suppose, is that it's a good idea not to use a math-italic "E", in part as that letter might get confused with the name of an ordinary variable. – Mico Oct 16 '17 at 15:46
  • 1
    But by that account we should write f(x) with an upright f, but we do not. The rule I tend to adhere to is function names of two or more letters go upright. Single letter is let to the authors preferences of the tradition of the field. – daleif Oct 16 '17 at 15:48
1

You can simply use the \middle primitive from e-TeX:

\def\E[#1|#2]{E\left[ #1 \>\middle|\> #2 \right]}

$$
  \E [\sum_{i=1}^N x_n | Y] 
$$

\bye

Note that more readable syntax is used.

wipet
  • 74,238