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?

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. • Why do you say that the automatic sizing should be used sparingly? – Charlie Feb 13 '16 at 19:55
• @Charlie Because in several cases it gives too big delimiters. – egreg Feb 13 '16 at 20:34
• @egreg, nice piece of code! Could you explain the workings of "\activatebar" and the commands used in its definition? Cheers! – pglpm Jan 20 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. – egreg Jan 20 at 10:35

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. – daleif Jun 29 '14 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 '14 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). – daleif Jun 29 '14 at 13:55
• @daleif - I'm puzzled over what you may be objecting to. The two examples I list produce vertical bars of exactly the same (viz., minimal) size; it's the horizontal whitespace on either side of the bars that differs considerably across the cases -- which is as it should be, because the bars mean something quite different across the examples. I will add a sentence to highlight this distinction. – Mico Jun 29 '14 at 14:53
• 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) – daleif Jun 29 '14 at 15:16

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}\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}\{\}{\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 '17 at 4:41