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.

6
  • 2
    \middle? (tex.stackexchange.com/questions/3137/…) Commented Sep 5, 2017 at 12:15
  • 4
    One should define such a command with mathtools. See the package documentation, § 3.6, Paired Delimiters, pp. 25–29.
    – Bernard
    Commented Sep 5, 2017 at 12:54
  • Thank you both. I decided to go with mathtools. After using LaTeX for a few years, I am surprised I haven't come across it yet.
    – Tohiko
    Commented Sep 5, 2017 at 13:24
  • @Tohiko Would you care to write up an answer?
    – ksgj1
    Commented Oct 14, 2017 at 16:15
  • @Tohiko Would you accept an answer? You could also accept your own answer.
    – Bobyandbob
    Commented Oct 18, 2017 at 15:47

3 Answers 3

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}
1
  • 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ó
    Commented Oct 2, 2019 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}
3
  • 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
    Commented Oct 16, 2017 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
    Commented Oct 16, 2017 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
    Commented Oct 16, 2017 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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .