# 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?

• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Jun 29, 2014 at 11:25
• this appears to be a duplicate of teh referenced question tex.stackexchange.com/questions/498/… \mid is used as an infix relation, use \lvert \rvert for left and right Jun 29, 2014 at 11:28
• See this answer. Jun 29, 2014 at 12:01

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}[1]{[}{]}{%
\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]{[}{]}{#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}


• Why do you say that the automatic sizing should be used sparingly? Feb 13, 2016 at 19:55
• @Charlie Because in several cases it gives too big delimiters. Feb 13, 2016 at 20:34
• @egreg, nice piece of code! Could you explain the workings of "\activatebar" and the commands used in its definition? Cheers! Jan 20, 2019 at 10:32
• @pglpm It makes | a “math active” character inside the current group (initiated by either \left or \begingroup, so this status will be reverted at group end; the meaning assigned to | is \innermid. Jan 20, 2019 at 10:35
• @egreg Is there a way to include the option to specify the measure for the expectation? Something like, if I type \expect[P]{X}, it would be equivalent to E^P[X]? Feb 8, 2021 at 17:30

You should definitely not use \mid to denote all vertical bars. In fact, I'd say that using \mid everywhere is as bad as (or maybe even worse than) typing | or \vert to denote each and every vertical bar.

• The macro \mid has a specific use to denote conditioning information. E.g.,

$\{\, x \mid x>5 \,\}$


denotes the set of all numbers x that are greater than 5. Observe the amount of whitespace around the vertical bar. (This example is, by the way, courtesy of the TeXbook, p. 174.)

• To denote the absolute value of some number z, you could type |z|. However, typing \lvert z \rvert is marginally better, as in

a \lvert b \rvert c


Observe that there's now no extra whitespace on either side of the bars.

Summing up: the vertical heights of the bars produced by \mid on the one hand and \lvert and \rvert on the other are identical. It's in the amounts of horizontal whitespace that's inserted around them that they differ.

If you find yourself needing to type a lot of these macros, and especially if you need to have their sizes adapt to their associated material, it's highly advisable to create separate macros called, say, \abs{...} and \set{...}{...}. For much more on set-related notation that uses curly braces and middle vertical bars see, e.g., the posting Why don't the curly braces and the mid bar become bigger?

• I don't think \mid is actually meant to be used in this context, ie in set building . Mostly because it is not a delimiter and cannot be scaled. Jun 29, 2014 at 12:38
• @daleif - Do check out the examples at the bottom of p. 174 and the top of p. 175 of the TeXbook.
– Mico
Jun 29, 2014 at 12:41
• My comment still stands. I do not think it is a good idea to tell (especially new) users to use one macro in one condition and another in a different one, especially when the two macros should mean the same (just with different sizes). Jun 29, 2014 at 13:55
• Btw: i have been using the \Set{}{} notation and have abandoned it again. It is not natural to read a set construction as a two arg macro. These days I recommend a\Set{... \given... }  syntax with build in fence scaling. Done right the symbol behind \given can also be made to scale. I think this gives it a syntax much closer to spoken English (inspired by siunitx) Jun 29, 2014 at 15:16
• @gen - In a word: Yes.
– Mico
Feb 1, 2018 at 7:05

Based on the examples in the mathtools documentation, here are some easy-to-use macros for absolute values and set builders. In addition to mathtools, they also use etoolbox and xparse.

For sets, only one argument is necessary: the syntax is \set{x ; P(x)}, producing {x|P(x)}, where the braces and the vertical bar are adjusted to the size of the contents, automatically with the \Set command (a more natural and easy to remember notation for \set* from mathtools, in my opinion), manually with the \set command. Another difference with the mathtools version is that size of the manual version defaults to \big, as I find the non adjusted version most of the time looks too small. If you prefer the original version, it suffices to comment the (very classical) patch.

Of course, as the semi-colon is used as a separator between the elements and the properties that defines them, any other ; should be written {;}. This is unlikely to happen very often.

The macro also works for sets defined as lists (no defining property).

Here is an illustration:

\documentclass[10pt]{report}

\usepackage[utf8]{inputenc}
\usepackage{lmodern}

\usepackage{mathtools,nccmath}%
\usepackage{ etoolbox, xparse}

\DeclarePairedDelimiterX{\abs}[1]\lvert\rvert{\ifblank{#1}{\,\cdot\,}{#1}}

\let\oldabs\abs
\def\abs{\futurelet\testchar\MaybeOptArgAbs}
\def\MaybeOptArgAbs{\ifx[\testchar\let\next\OptArgAbs
\else \let\next\NoOptArgAbs\fi \next}
\def\OptArgAbs[#1]#2{\oldabs[#1]{#2}}
\def\NoOptArgAbs#1{\ifblank{#1}{\oldabs{}}{\oldabs[\big]{#1}}}

\def\Abs{\oldabs*}

\DeclarePairedDelimiterX{\set}[1]\{\}{\setargs{#1}}
\NewDocumentCommand{\setargs}{>{\SplitArgument{1}{;}}m}
{\setargsaux#1}
\NewDocumentCommand{\setargsaux}{mm}
{\IfNoValueTF{#2}{#1}{\nonscript\,#1\nonscript\;\delimsize\vert\nonscript\:\allowbreak #2\nonscript\,}}
%%% Syntaxe : \set{x ; P(x)})
\let\oldset\set
\def\set{\futurelet\testchar\MaybeOptArgSet}
\def\MaybeOptArgSet{\ifx[\testchar \let\next\OptArgSet
\else \let\next\NoOptArgSet \fi \next}
\def\OptArgSet[#1]#2{\oldset[#1]{#2}}
\def\NoOptArgSet#1{\OptArgSet[\big]{#1}}

\def\Set{\oldset*}%

\begin{document}

Let $\abs{}$ denote the \emph{absolute value} function. We have

$\Abs{\frac{x}{y}} =\frac{\abs{x}}{\abs{y}}$%

$\abs[]{z}\quad \abs{z}\quad \abs[\Big]{z}\quad \abs[\bigg]{z}\quad \abs[\Bigg]{z}$%

Let $\mathbf{ U} = \set{z \in \mathbf C ; \abs[]{z} = 1}$ and $\mathbf U_3 = \Set{1,\dfrac{-1 + i\sqrt{3}}{2},\dfrac{-1-i\sqrt{3}}{2}} = \set{1,\frac{1}{2}(-1 + i\sqrt{3}),\frac{1}{2}(-1-i\sqrt{3})} = \set[]{1,\mfrac{1}{2}(-1 + i\sqrt{3}),\mfrac{1}{2}(-1-i\sqrt{3})}$.

\end{document}


As long as you have the amsmath package enabled, you could try the commands \left| and \right| in math mode, just like with braces and parenthesis.

• There is no need for amsmath if you want \left|...\right|`. They work out-of-the-box.
– Werner
Oct 9, 2017 at 4:41