Generalized bra-ket notation in LaTeX

Published on 2024-05-15 (updated on 2024-05-19)

tags: latex

I wrote a small package, braketx, for generalized bra-ket notation in LaTeX (or TeX). It is based on Donald Arseneau’s braket package. The braket package provides a few macros, mostly one-liners, for the usual bra-ket and set notations. Donald suggests to copy his macros and replace \(\langle\) and \(\rangle\) with the required delimiters for other bra-ket notations. I did that, but then generalized the definitions and made them into a “package” so that I don’t have to copy the definitions every time I write a new document.

braketx provides 6 macros:

\xbra

This is a generalized bra. The delimiter is the first argument.

\def\xbra#1#2{\mathinner{#1{#2}|}}
\xket

This is a generalized ket. The delimiter is the last argument.

\def\xket#1#2{\mathinner{|{#1}#2}}
\xbraket

This is a generalized bra-ket inner product. The left delimiter is the first argument, and the right delimiter the second argument.

\def\xket#1#2{\mathinner{|{#1}#2}}
\xBra

Same as \xbra, but delimiter adjusts to the size of the label.

\def\xBra#1#2{\left#1{#2}\right|}
\xKet

Same as \xket, but delimiter adjusts to the size of the label.

\def\xKet#1#2{\left|{#1}\right#2}
\xBraket

Same as \xbraket, but delimiters and any vertical or double vertical lines within adjust to size of the labels. This is not an one-liner. I couldn’t have figured out the logic to adjust the size of any vertical lines between the delimiters. TeX is complex.

\let\protect\relax
%
{\catcode`\|=\active
  \xdef\xBraket{\protect\expandafter\noexpand\csname xBraket \endcsname}
  \expandafter\gdef\csname xBraket \endcsname#1#2#3{\begingroup
     \ifx\SavedDoubleVert\relax
       \let\SavedDoubleVert\|\let\|\BraDoubleVert
     \fi
     \mathcode`\|32768\let|\BraVert
     \left#1{#2}\right#3\endgroup}
}
\def\BraVert{\@ifnextchar|{\|\@gobble}% turn || into \|
     {\egroup\,\mid@vertical\,\bgroup}}
\def\BraDoubleVert{\egroup\,\mid@dblvertical\,\bgroup}
\let\SavedDoubleVert\relax

\begingroup
 \edef\@tempa{\meaning\middle}
 \edef\@tempb{\string\middle}
\expandafter \endgroup \ifx\@tempa\@tempb
 \def\mid@vertical{\middle|}
 \def\mid@dblvertical{\middle\SavedDoubleVert}
\else
 \def\mid@vertical{\mskip1mu\vrule\mskip1mu}
 \def\mid@dblvertical{\mskip1mu\vrule\mskip2.5mu\vrule\mskip1mu}
 \fi

With this you can easily create bra-ket notation like \((a|b\rangle\), \(\{\sigma|\), or even, \[\left\{\frac{1}{2}\frac{1}{2}\middle|O\middle|\alpha\beta\right).\] One caveat of the fixed-size delimiter macros is that they do not check if the arguments that are supposed to be delimiters are actually delimiters. \xket{a}{b} is as valid as \xket{a}{\rangle}. But then I don’t expect anyone to actually use \xket{a}{b}.

Admittedly this is a very niche package. But comes in quite handy when you are writing documents where you need generalized bra-ket notations to distinguish different types of quantum states. For example, distinguishing Slater determinants from product states.

To use it, you can just copy the definitions to a file braketx which you can then call with \usepackage{braketx}. Or clone the git repository and put it in your TeX path. On Linux machines I put custom packages under ~/.local/tex/latex/, and run sudo tlmgr conf auxtrees add ~/.local/texmf/. You may or may not have to use sudo. You can use this alongside the braket package. Or you can redefine the braket in terms of the braketx macros. For example, you can define \bra as:

\def\bra#1{\xbra{\langle}{#1}}