Physics bra-ket symbols in IPython

2019-06-04 02:15发布

问题:

I am trying to have the ket symbol which is usually written in latex as \ket{\psi}. However, this doesn't work when written within the $$ ... $$. This also doesn't work when written using IPython.display.

回答1:

$ latex
** \documentclass{article}\usepackage{amsmath}\begin{document}
... chatter chatter ...
* \show\ket
> \ket=undefined.

So your \ket macro is coming from some package or other which I don't know what it is. This doesn't explain why it doesn't work in $$ ... $$ -- you have to work at it to get things to not work in display math -- but it does explain why it doesn't work in IPython: whichever package this is isn't being loaded in IPython's canned TeX environment (or) IPython's emulation of TeX math input language does not include this macro.

Here's a reasonable definition in terms of primitive math symbols, which should work fine in display mode. I don't know if you can put this into IPython verbatim, but you should at least be able to write out the expansion longhand when needed (e.g. $$ \left| \psi \right\rangle $$)

\newcommand{\ket}[1]{\left|{#1}\right\rangle}
\newcommand{\bra}[1]{\left\langle{#1}\right|}

EDIT: define macros using LaTeX preferred interface, rather than primitive \def.



回答2:

IPython uses MathJax to render LaTeX in the notebook. MathJax is great but only supports a subset of LaTeX hence there are some limitations. See MathJax homepage for details.

A new command like proposed by @Zack will work fine though. Be aware that such custom commands will likely lead to some issues when converting to LaTeX (nbconvert).



回答3:

I was able to extend @zwol 's answer to create a \braket command as well for use in a Jupyter notebook:

$$\newcommand{\braket}[2]{\left\langle{#1}\middle|{#2}\right\rangle}$$
$$\braket{\Psi^*}{\Psi}$$

$$\braket{\frac{\Psi^*}{2}}{\Psi}$$

Gives: