Skip to content

Instantly share code, notes, and snippets.

@sametz
Created June 9, 2018 17:38
Show Gist options
  • Save sametz/6db971a36d7bc3acdd169ade80c5a7ee to your computer and use it in GitHub Desktop.
Save sametz/6db971a36d7bc3acdd169ade80c5a7ee to your computer and use it in GitHub Desktop.
Dirac notation (bra/ket) LaTeX commands for use in Jupyter notebooks
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@sametz
Copy link
Author

sametz commented Jun 9, 2018

This won't look right in the GitHubGist preview, but if you download and run the following you should see:

screen shot 2018-06-09 at 1 42 12 pm

@cgreil
Copy link

cgreil commented May 11, 2021

Sorry to bother you with that, but could you advise me how to add this configuration into jupyter notebook? I assume I need the nbextension latex_envs which i do have, but I am not sure which json file i can add this configuration to and where to find that.
Kind regards!

@sametz
Copy link
Author

sametz commented May 12, 2021

Been a while since I looked at this. I tested this again with Jupyter Notebook, with default Conda installation. Nothing special was required...just entering the code into markdown worked. The \newcommand lines create new bra, ket, and brakes LaTeX commands that you can then use in your own markdown. As long as those \newcommand lines appear in your notebook before the commands are invoked, it should just work.

To see the Markdown in action, you should download the .ipynb and view it, but I'm copying all the markdown below so you can see the Markdown that was rendered in the notebook.

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

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

\begin{align}
\braket{Paw_L}{Paw_R} = 0
\end{align}

@cgreil
Copy link

cgreil commented May 12, 2021

Thank you for your quick reply, I was able to make it work thankfully.

@westurner
Copy link

Looks like this renders correctly with GitHub's .ipynb gist preview MathJax now?
screenshot of <psi|omega|psi> and then <psi|bar|psi> and theb <(psi/2)|psi> as mathjax in .ipynb gist

@sametz
Copy link
Author

sametz commented Jul 18, 2023

@westurner apparently!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment