14

I'm using the package braket for my physics notes, and this speeds things up a great deal but I'm constantly writing \ket{\psi} or \bra{\psi}, and because this specific pair of bra and ket appears so often, this gets repetitive and I would like a macro to speed this up. However since the braket package works only in math mode, my attempt \newcommand{\ketp}{$\ket{\psi}}$ work when I'm writing in text mode, but when I'm writing in mathmode it breaks (unsurprisingly).

Is there any way I can get this command to work in both mathmode and textmode without having to define two different macros, one for each?

Thanks in advance for any help.

P.S. I'm certain this is a very simple question but I could find little to no explanation of this feature.

22

You can use

\newcommand{\ketp}{\ensuremath{\ket{\psi}}}

However it isn't clear that it's always a good idea to do this. TeX markup is based on a design that has a clear distinction between text and math commands, the assumption is that the author always knows which mode is in use, and typically similar commands for use in text and math have different names, compare \^ and \hat for example.

That said, \ensuremath was added to LaTeX to do exactly what you ask, so if you want to use it, it's there...

15

There is a command exactly for this purpose: \ensuremath{…}. If used in math mode, it gives its argument unchanged; if used in text mode, it gives its argument, but put into math mode.

So \newcommand{\ketp}{\ensuremath{\ket{\psi}}} will do just what you ask for.

Some good guidance on its usage: Proper way to use ensuremath; When not to use ensuremath.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.