6
$\begingroup$

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 ?

$\endgroup$
1

3 Answers 3

2
$\begingroup$

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.

$\endgroup$
6
  • $\begingroup$ 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 ? $\endgroup$
    – Frotaur
    Commented Dec 26, 2016 at 22:20
  • 1
    $\begingroup$ Just beware of the fact that renewcommand can be dangerous $\endgroup$
    – Kyle Kanos
    Commented Dec 26, 2016 at 22:47
  • $\begingroup$ @Frotaur I clicked "edit" but didn't change anything to see the source. $\endgroup$
    – rob Mod
    Commented Dec 27, 2016 at 1:38
  • $\begingroup$ That renewcommand explains why I was able to use \ket in some places but not others... Good to know $\endgroup$
    – Frotaur
    Commented Dec 27, 2016 at 1:45
  • 1
    $\begingroup$ Weird, I could have sworn | and \lvert were not the same height... $\endgroup$
    – David Z
    Commented Dec 27, 2016 at 2:10
  • 1
    $\begingroup$ @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. $\endgroup$ Commented Apr 1, 2020 at 12:57
3
$\begingroup$

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.

$\endgroup$
2
$\begingroup$

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.

$\endgroup$

You must log in to answer this question.

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