vertical bar for absolute value and conditional expectation

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.

enter image description here

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}

\linespread{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}

enter image description here


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

enter image description here

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
    

enter image description here

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?


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}

enter image description here

Tags:

Math Mode