I have the following command for conditional expectations

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

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

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.

  • 2
    \middle? (tex.stackexchange.com/questions/3137/…) 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
    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
    Sep 5, 2017 at 13:24
  • @Tohiko Would you care to write up an answer?
    – ksgj1
    Oct 14, 2017 at 16:15
  • @Tohiko Would you accept an answer? You could also accept your own answer.
    – Bobyandbob
    Oct 18, 2017 at 15:47

3 Answers 3


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


  \nonscript\:#1\vert \allowbreak \nonscript\:\mathopen{}}

\[ \E*{\sum_{i=1}^N x_n \given Y} \]
  • 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 2, 2019 at 14:25

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.

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

% Expectation operator

%% Three auxiliary macros: \given, \SetSymbol, and \Set

%% Main user macro: \expect


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

  • 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, 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
    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
    Oct 16, 2017 at 15:48

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] 


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 .