[Tex/LaTex] vertical bar for absolute value and conditional expectation


Now I use "mid" for all vertical bars, but I wonder what's the proper ways.

The first scenario is vertical bar for absolute value. Now I just right $x\leq \mid x \mid$. but is this correct?

The second scenario is in conditional expectation. Now I write E[x \mid y], but sometimes if the "x" part is complex, it might be quite "tall", I wonder if there's a way to have a "automatically adjusted" vertical bar. Is there?

Best Answer

As others have remarked, vertical bars can be obtained with different commands and one should use the correct one in each case:

  • \mid when it's a relation symbol, for instance in set notation or for “divides”;

  • \lvert or \rvert when it's a (left or right) delimiter; note that this requires amsmath that's recommended anyway when a document needs math.

Just typing | can work, but there are some subtleties, so it's better to use the above commands. Similarly, for the double bar is

  • \parallel when it's a relation symbol;

  • \lVert or \rVert when it's a delimiter.

You can exploit mathtools for your symbol for expectation, but with some more tricks in order to make the bar doing the right thing.


  \ifnum\currentgrouptype=16 \else\begingroup\fi
  \ifnum\currentgrouptype=16 \else\endgroup\fi







In the same style as macros declared with \DeclaredPairedDelimiter, you can give \expect an optional argument that can be one among \big, \Big, \bigg or \Bigg for manually sizing the delimiters or use \expect* in order to get automatic sizing (use it sparingly).

Here you can use | for conditional expectation, because the macros take care of its relation nature.

enter image description here


A revamped definition that uses new features of expl3 and xparse. This also allows to specify the measure of the expectation.


\NewDocumentCommand{\expect}{ e{^} s o >{\SplitArgument{1}{|}}m }{%
  \operatorname{E}%     the expectation operator
  \IfValueT{#1}{{\!}^{#1}}% the measure of the expectation
  \IfBooleanTF{#2}{% *-variant
  }{% no *-variant
    \IfNoValueTF{#3}{% no optional argument
    }{% optional argument



$\expect{X}$ $\expect^P{X}$

$\expect{X|Y}$ $\expect^P{X|Y}$

$\expect[\big]{X|Y}$ $\expect^P[\big]{X|Y}$

$\expect*{\dfrac{1}{2}X|Y}$ $\expect^P*{\dfrac{1}{2}X|Y}$


enter image description here