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

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

You can try with `\vline`

MWE:

```
\documentclass[aps]{revtex4}
\usepackage{amsmath,amssymb}
\begin{document}
Your attempt
\[\left|\frac{S}{\sum\limits^{N}_{a=1}T_{a}}\right|\]
With \verb|\vline|
\[\vline\,\frac{S}{\sum\limits^{N}_{a=1}T_{a}}\,\vline\]
\end{document}
```

Output:

Some remarks:

- Use
`\[...\]\`

instead of `$$`

. See Why is \[ ... \] preferable to $$ ... $$?
- Use
`\limits`

instead of `\overset`

and `\underset`

to obtain subscripts and superscrips as in `\displaystyle`

.

## Best Answer

According to

`texdoc symbols`

:`\mvert`

and`\mid`

are identical and produce arelation.`\vert`

is a synonym for`|`

and both produce the same symbol, but should be used in the context of an ordinal, and should be used as anoperator,notas a delimiter (p54, bottom).`\divides`

once again produces the same symbol but should be used as a binary“divides” operator.`\lvert`

and`\rvert`

are left and right delimiters, respectively.