# Automatically Sized bra/ket in Quantum Physics

I got to know that \left and \right allow us to automatically adjust the sizes of brackets. I'm currently working on some documents of Quantum Physics with so-called Dirac notations of bra and ket, made of \langle, | and \rangle.

Typically, there are 5 types of combinations: |a>, <b|, <a>, <a|b> and <a|c|b>. It is easy to use \left and \right for the first 3 types, how can I make automatic adjustment for all the symbols?

You could use braket, where the commands \bra, \ket and \braket have the “automatic extended” variants \Bra, \Ket and \Braket.

Or you can make your own commands. Here each of the \bra, \ket and \braket command accepts an optional size argument (\big, \Big, \bigg or \Bigg), or a * for the automatically extensible version.

\documentclass{article}

\usepackage{mathtools,xparse}

\DeclarePairedDelimiterX{\bra}[1]{\langle}{\rvert}{#1\,}
\DeclarePairedDelimiterX{\ket}[1]{\lvert}{\rangle}{\,#1}
\DeclarePairedDelimiterX{\makebraket}[1]{\langle}{\rangle}{#1}

\NewDocumentCommand{\braket}{som}{%
\begingroup\activatebraketbar
\IfBooleanTF{#1}
{\makebraket*{#3}}
{\IfNoValueTF{#2}{\makebraket{#3}}{\makebraket[#2]{#3}}}%
\endgroup
}

\makeatletter
\newcommand{\braketbar}{%
\,\delimsize\vert\@ifnextchar|{\!}{\,}%
}
\makeatother
\newcommand{\activatebraketbar}{%
\begingroup\lccode~=|\lowercase{\endgroup\let~}\braketbar
\mathcode|="8000
}

\begin{document}

\begin{gather*}
\bra{x}+\ket{\psi(t)}+
\bra[\big]{x}+\ket[\big]{\psi(t)}+
\bra[\Big]{x}+\ket[\Big]{\psi(t)} \\
\braket*{\frac{x}{y}|z}+\braket*{x|\frac{a}{b}|z} \\
\braket{a||b}
\end{gather*}

\end{document}


The use of \middle can help here.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\def\z{\frac{x}{y}}
$\left\lvert\z\right\rangle\quad \left\langle\z\right\rvert\quad \left\langle\z\right\rangle\quad \left\langle\z\middle\vert b\right\rangle\quad \left\langle\z\middle\vert b\middle\vert c\right\rangle$
\end{document}


The package physics has few commands for this purpose.

1. \bra

$\bra{a} \quad \bra{\frac{a}{b}} \quad \bra*{\frac{a}{b}}$

1. \ket

$\ket{b} \quad \ket{\frac{a}{b}} \quad \ket*{\frac{a}{b}}$

1. \braket

$\braket{a}{b} \quad \braket{a}{\frac{x}{y}} \quad \braket*{a}{\frac{x}{y}}$

1. \expectationvalue or \ev

$\ev{a} \quad \ev{H}{a}$

1. \matrixelement or \mel

$\mel{a}{H}{b} \quad \mel{a}{\hat{H}}{b} \quad \mel**{a}{\hat{H}}{b}$


## MWE

\documentclass{article}
\usepackage{physics}

\begin{document}
$\bra{a} \quad \bra{\frac{a}{b}} \quad \bra*{\frac{a}{b}}$
$\ket{b} \quad \ket{\frac{a}{b}} \quad \ket*{\frac{a}{b}}$
$\braket{a}{b} \quad \braket{a}{\frac{x}{y}} \quad \braket*{a}{\frac{x}{y}}$
$\ev{a} \quad \ev{H}{a}$
$\mel{a}{H}{b} \quad \mel{a}{\hat{H}}{b} \quad \mel**{a}{\hat{H}}{b}$
\end{document}
`