[Tex/LaTex] Automatically Sized bra/ket in Quantum Physics

braket

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?

Best Answer

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{x|\psi(t)}\quad\braket{x|y|\psi(t)} \\
\braket[\big]{x|\psi(t)}\quad\braket[\big]{x|y|\psi(t)} \\
\braket[\Big]{x|\psi(t)}\quad\braket[\Big]{x|y|\psi(t)} \\
\braket*{\frac{x}{y}|z}+\braket*{x|\frac{a}{b}|z} \\
\braket{a||b}
\end{gather*}

\end{document}

enter image description here