Code: Select all
\newcommand\braket[2][]{\langle\ifx\@empty#2#1\else#1\lvert#2\fi\rangle}
-
\braket{a}
gives me\langle a \rangle
-
\braket{a}{b}
gives me\langle a | b \rangle
Code: Select all
\newcommand\braket[2][]{\langle\ifx\@empty#2#1\else#1\lvert#2\fi\rangle}
\braket{a}
gives me \langle a \rangle
\braket{a}{b}
gives me \langle a | b \rangle
NEW: TikZ book now 40% off at Amazon.com for a short time.
#1
. You are testing argument #2
for \empty
.Code: Select all
\newcommand*{\braket}[2][]{\langle\ifx & #2\else #2\lvert #1\fi\rangle}
\ifx
test can be found in another topic [1]. Application of the command is like this.
Code: Select all
$\braket[b]{a}$
‹a|b›
. For others it should be noted that this needs the amsmath package for the \lvert
command.Code: Select all
\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand\braket{mg}{%
\langle #1%
\IfNoValueF{#2}{\vert#2}%
\rangle}
\begin{document}
$\braket{a}$ $\braket{a}{b}$
\end{document}
NEW: TikZ book now 40% off at Amazon.com for a short time.