# How to properly write kets

I noticed asking various questions regarding quantum physics, that sometimes, the latex command \ket works (here, for example), and other times, it does not, and I am obliged to use \left | \psi \right>. Is there a special way to enable its use or did I miss something ?

The source for this answer to the question you link includes, at the top,

 $\newcommand{\Ket}[1]{\left|#1\right>}$


while this other answer includes, between "the three vectors" and the first displayed equation,

 $\newcommand{\ket}[1]{|#1\rangle}$


So on that page both $\ket\psi$ and $\Ket\psi$ do what you want, but the spacing for \Ket is better because that author used \left and \right.

Moral of story: the definition is brief, so include it yourself if it makes your life easier.

• Great, I didn't know we could define new commands inside posts ! I'll sure use it. By the way, how do you look at the source of other posts ? – Frotaur Dec 26 '16 at 22:20
• Just beware of the fact that renewcommand can be dangerous – Kyle Kanos Dec 26 '16 at 22:47
• @Frotaur I clicked "edit" but didn't change anything to see the source. – rob Dec 27 '16 at 1:38
• That renewcommand explains why I was able to use \ket in some places but not others... Good to know – Frotaur Dec 27 '16 at 1:45
• Weird, I could have sworn | and \lvert were not the same height... – David Z Dec 27 '16 at 2:10
• @KyleKanos This is no longer the case. New and renewed MathJax commands are now scoped to individual posts and thus the danger listed there is no longer present. – Emilio Pisanty Apr 1 at 12:57

As an alternative to the other two answers: the bra-ket angles both have unicode incarnations, ⟨ and ⟩, and they are both rendered correctly by MathJax. Thus, if you type

$⟨a|b⟩$


it will render correctly. (On the other hand, this only works for the single-size version, i.e. \right⟩ won't work.) My solution is to have those characters hardwired into my software keyboard, and it really speeds up typing.

In addition, this usage is directly in the spirit of Markdown: having a lightweight source which renders into a nice formatting, but which is also human-readable directly from the source.

That said, it is important to remark that this usage is exclusive to MathJax and it does not work under standard TeX renderers. (In fact, MathJax will correctly render a bunch of off-track unicode symbols, like ≠, ±, ≃, ° and so on, that will send (La)TeX into a belly-up spin.) As such, I consider it impolite to use these characters when editing other people's posts.

Finally, a word of caution: all the posts on a page share the same MathJax environment, and in particular this means that any commands that you define or renew can and will affect other posts on the same page. This has been discussed before on this meta, with the specific point that renewcommand should not be used on this site.

Defining a new command for \kets and \bras is not terrible, partly because even if other people on the same thread define it differently, you wouldn't expect real conflicts in the output. However, it's important to keep in mind that any use of \newcommand, particularly beyond the simplest, mildest, most standard definitions, can produce unexpected behaviour like e.g. on this thread.

If you look at the source, you see that they include

\newcommand{\ket}[1]{|#1\rangle}

in their LaTeX. This defines a command \ket that makes a ket. So you have to define it on your own.