How to make pipe to grow with both sides?

How to make pipe (vertical bar) symbol to grow like in Dirac product notation but without outer angle brackets?

-
– Peter Grill Oct 21 '12 at 19:18
What is the purpose/function of the vertical bar? Can you compare it to other symbols/functions? The spacing around the symbol and around the expression depends on it. – Heiko Oberdiek Oct 21 '12 at 20:11

Usually LaTeX is used with e-TeX that provides \middle:

\documentclass{article}
\begin{document}
$\left. a \middle| b \right.$
\end{document}


The extra spaces of \left. and \right. would probably would make sense, but they can be reduced by package mleftright:

\documentclass{article}
\usepackage{mleftright}
\begin{document}
$\mleft. a \middle| b \mright.$
\end{document}


If the vertical bar is used like a relational symbol, then the spacing is achieved by

\left. a \mathrel{}\middle|\mathrel{} b \right.


e-TeX does not insert space between a math rel atom and the closing delimiter, the left side of \middle, and it does not insert space between the opening delimiter, the right side of \middle, and a math rel atom.

An alternative is

\left. a \nonscript\;\middle|\nonscript\; b \right.


\nonscript suppresses the following space if in \scriptstyle or \scriptscriptstyle, where TeX also would not set space around relational symbols. There are differences to the previous method for punctuation characters (unlikely here), other relational symbols (unlikely probably) and open/close delimiters, e.g.:

\documentclass{article}
\usepackage{mleftright}
\begin{document}
$\mleft.(\mathrel{}\middle|\mathrel{})\mright.$
$\mleft.(\nonscript\;\middle|\nonscript\;)\mright.$
\end{document}


For a binary operator the space would be \> (medium space) instead of \; (thick space):

\mleft. a \nonscript\;\middle|\nonscript\; b\mright.


The behaviour is a little closer to a binary operator with the following construct:

\left.a \mathbin{}{\nonscript\mkern-\medmuskip}\middle|{\nonscript\mkern-\medmuskip} \mathbin{} b\right.


Either way, it is a good idea to hide the implementation details in a markup macro.

-
The \middle command is new to me. Thanks! This is the better answer. – Anthony Oct 21 '12 at 18:34
Adding \; on both sides of | seems better. – egreg Oct 21 '12 at 19:46
@egreg It depends on the purpose and function of the vertical line. \; is thick space, the space that is added in non-script styles if | would be a relational operator. – Heiko Oberdiek Oct 21 '12 at 20:10
In general this "growing bar" is requested when | is a relation symbol. To be really picky it should be \nonscript\;\middle|\nonscript\;. :) – egreg Oct 21 '12 at 20:22
@egreg Thanks, I have added some spacing variants and some discussion to the answer. – Heiko Oberdiek Oct 21 '12 at 21:15

Package braket defines some macros that can be used here.

While it defines only commands that uses angle brackets or braces (for sets), one can easily copy-edit the specification for \Braket to make their own \Pipe.

Advantages over the very similar solution of Heiko Oberdiek is

1. The usage of a macro.
2. You don't need to provide the \middle command (it is used internally) and just use plain | or, for double pipes, || and \|.

Code

\documentclass{article}
\usepackage{braket}
\usepackage{amsmath}
\usepackage{mleftright} %

% the following lines are borrowed from braket
{\catcode\|=\active
\xdef\Pipe{\protect\expandafter\noexpand\csname Pipe \endcsname}
\expandafter\gdef\csname Pipe \endcsname#1{\begingroup
\ifx\SavedDoubleVert\relax
\fi
\mathcode\|32768\let|\BraVert
\left.{#1}\right.\endgroup}
}

% with package mleftright:
{\catcode\|=\active
\xdef\mPipe{\protect\expandafter\noexpand\csname mPipe \endcsname}
\expandafter\gdef\csname mPipe \endcsname#1{\begingroup
\ifx\SavedDoubleVert\relax
\fi
\mathcode\|32768\let|\BraVert
\mleft.{#1}\mright.\endgroup}
}
\begin{document}\noindent
$y \Pipe{\dfrac{1}{2} | x_2 } z$ \\
$y \mPipe{\dfrac{1}{2} | x_2 } z$ \\
\end{document}


Output

-

You can preface delimiters with \left and \right in order to have them automatically sized. If you only have one delimiter, you can use . in place of the other one, as in

\left. \frac{1}{2} \right|


So if you know which side of the bar is bigger, you can try something like this:

\left\langle \left. \frac{1}{2} \psi_1 \right| \psi_2 \right\rangle

-
For the latter expression, using \middle| seems more... better. – Werner Oct 21 '12 at 18:28
Yes, I just learned about it. A lot of my LaTeX knowledge comes from the wikibook, which mentions \left and \right but not \middle. I think I'll go change that. – Anthony Oct 21 '12 at 18:36