0

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}
$\expectation{text}$

What is the correct way of formatting this?

4
  • \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

2

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.

1

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

\documentclass{article}

\usepackage{mathtools}

\DeclarePairedDelimiterX{\braket}[3]{\langle}{\rangle}{#1
\,\delimsize\vert\,\mathopen{} #2 \,\delimsize\vert\, \mathopen{}
#3}

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

\begin{document}

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

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

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

Automatic sizing
\begin{equation*}
  \braket*{\psi}{\sum_{i=1}^{n}}{\phi}
  \expectation*{\sum_{i=1}^{n} B_{i}}
\end{equation*}

\end{document}
0

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 .