How to Define a LaTeX Command for a Ket-Bra
At a glance
- 1. Load amsmath
Include the amsmath package for the delimiter commands used in the definition.
- 2. Define the macro
Use two arguments: the first fills the ket and the second fills the bra.
- 3. Use the outer product
Write the command in math mode, for example with psi and phi as its arguments.
To typeset a ket-bra outer product repeatedly, define a command that takes two arguments: the first goes inside the ket and the second inside the bra. The definition below also tightens the space between the adjacent delimiters so the expression reads as one operator.
A minimal ket-bra command
Put this definition in your document preamble, after loading amsmath:
\documentclass{article}
\usepackage{amsmath}
\newcommand{\ketbra}[2]{\lvert #1\rangle\!\langle #2\rvert}
\begin{document}
The outer product is $\ketbra{\psi}{\phi}$.
\end{document}
The command \newcommand{\ketbra}[2]{...} creates a macro named \ketbra with two arguments. In its replacement text, #1 is the first argument and #2 is the second. Thus \ketbra{\psi}{\phi} produces , or in Dirac notation.
The \! inserts a small negative space between the ket and bra. Without it, the gap between the two angle delimiters can look too wide. The \lvert and \rvert commands make the vertical bars behave as mathematical delimiters; amsmath provides these commands.
A scalable version
If the arguments may contain tall expressions, use \left and \right so the delimiters grow to fit:
\newcommand{\ketbra}[2]{\left|#1\right\rangle\!\left\langle#2\right|}
For example, this version can accommodate a fraction or a large subexpression inside either argument. Automatically scaled delimiters can look oversized in some contexts, though, so the fixed-size definition is often more consistent in running text. Choose the version that best fits your document, and use it consistently.
Optional ket and bra commands
If you also write kets and bras on their own, define them separately and build the outer product from them:
\newcommand{\ket}[1]{\lvert #1\rangle}
\newcommand{\bra}[1]{\langle #1\rvert}
\newcommand{\ketbra}[2]{\ket{#1}\!\bra{#2}}
Then \ket{\psi} gives , \bra{\phi} gives , and \ketbra{\psi}{\phi} gives their outer product. This version is convenient if you need all three forms. If you use a package that already defines commands such as \ket or \bra, avoid defining them again with \newcommand; choose different names or use the package’s definitions.
Common mistakes
- Forgetting math mode: Use
\ketbra{a}{b}inside$...$,\(...\), or a displayed equation. The delimiters and their contents are mathematical notation. - Using one argument instead of two: The definition has
[2], so call it with two braced arguments:\ketbra{a}{b}. Braces keep multi-token arguments together. - Using
\newcommandfor an existing name: LaTeX reports an error if the command is already defined. Check package conflicts before defining common names like\ketand\bra. - Assuming the macro changes mathematical meaning: It only typesets the expression. In quantum mechanics, is an operator; applied to , it gives .
For a short document, the two-argument definition is usually all you need. Use the scalable version only when fixed-size delimiters do not comfortably enclose the expressions you place inside them.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.