I would like to make a new command that allows me to quickly write the expectation value of an operator. It should put text in between other commands to make the following:

$\langle \psi | text | \psi \rangle$

through using a new command like this:

\newcommand{\expectation}{\langle \psi {text} \psi \rangle}

What is the correct way of formatting this?

  • \newcommand{\expectation}[1]{\langle \psi #1 \psi \rangle}
    – BambOo
    Sep 27, 2019 at 16:23
  • 1
    Have a look here for examples en.wikibooks.org/wiki/LaTeX/Macros
    – BambOo
    Sep 27, 2019 at 16:28
  • @BambOo, that worked. Thanks!
    – Bog Dan
    Sep 27, 2019 at 18:28
  • Have a look at the braket package. The \Braket macro makes sure that the size gets automatically adjusted.
    – user194703
    Sep 27, 2019 at 19:16

3 Answers 3


A syntax for a custom macro can be \newcommand{\<macroname>}[<number of arguments>]{<macro code with #1 as first argument, #2 second ...>}.

In your case, the macro can be defined as \newcommand{\expectation}[1]{\langle \psi #1 \psi \rangle}.

More information can be retrieved from wikibooks/latex/macros. You should also have a look at the xparse package which provides enhanced macro definition capabilities.


The mathtools package provides a way to define such commands. The documentation contains a definition for \braket and you can build on this to get an \expectation macro with similar properties, namely that \expectation* rescales automatically, and \expectation, \expectation[\big], \expectation[\Big], \expectation[\bigg], etc., produce specific sizes:

Sample output



\,\delimsize\vert\,\mathopen{} #2 \,\delimsize\vert\, \mathopen{}

\NewDocumentCommand{\expectation}{ s O{} m }{
  \IfBooleanTF {#1}
  { \braket*{\psi}{#3}{\psi} }
  { \braket[#2]{\psi}{#3}{\psi} }


\( \braket{\psi}{X}{\phi} \) \( \expectation{X} \)

\( \braket{\psi}{A^{2}}{\phi} \) \( \expectation{A^{2}} \)

Manual sizing
  \expectation[\bigg]{\sum_{i=1}^{n} B_{i}}
\( \braket[\big]{\psi}{A^{2}}{\phi} \) \( \expectation[\big]{A^{2}} \)

Automatic sizing
  \expectation*{\sum_{i=1}^{n} B_{i}}


It is much better to use the following definition: \newcommand{\expectation}[1]{\left\langle\psi\left\lvert{#1}\right\rvert\psi\right\rangle}. In this way the size of the angular brackets adapts to the size of the argument.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .