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

math-mode

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?

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.

\documentclass{article}
\usepackage{mathtools}

\newcommand{\expect}{\operatorname{E}\expectarg}
\DeclarePairedDelimiterX{\expectarg}{[}{]}{%
\ifnum\currentgrouptype=16 \else\begingroup\fi
\activatebar#1
\ifnum\currentgrouptype=16 \else\endgroup\fi
}

\newcommand{\innermid}{\nonscript\;\delimsize\vert\nonscript\;}
\newcommand{\activatebar}{%
\begingroup\lccode\~=\|
\lowercase{\endgroup\let~}\innermid
\mathcode|=\string"8000
}

\begin{document}

$\expect{X|Y}$

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

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

\end{document}


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. ## UPDATE

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

\documentclass{article}
\usepackage{mathtools}

\NewDocumentCommand{\expect}{ e{^} s o >{\SplitArgument{1}{|}}m }{%
\operatorname{E}%     the expectation operator
\IfValueT{#1}{{\!}^{#1}}% the measure of the expectation
\IfBooleanTF{#2}{% *-variant
\expectarg*{\expectvar#4}%
}{% no *-variant
\IfNoValueTF{#3}{% no optional argument
\expectarg{\expectvar#4}%
}{% optional argument
\expectarg[#3]{\expectvar#4}%
}%
}%
}
\NewDocumentCommand{\expectvar}{mm}{%
#1\IfValueT{#2}{\nonscript\;\delimsize\vert\nonscript\;#2}%
}
\DeclarePairedDelimiterX{\expectarg}{[}{]}{#1}

\begin{document}

$\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}$

\end{document}
` 