I've experimented for hours and finally found a general macro to get
TeX or LaTeX to output Dirac bra-ket notation as used in quantum
physics. The problem is much subtler than it looks, as described
below. My solution is still messy, and I'd welcome suggestions which
simplify the macros. [There is a package on some sites called
"hep.tex" which does Dirac bra-kets, but it doesn't worry about
delimiter sizes---they're always small size---so it doesn't meet my
requirements.]
The goal is to write a macro so that
$\braket{a}{b}$
generates something like:
$\bigl< a \bigm| b \bigr>$
which of course looks like
<a|b>
when typeset, a notation used frequently in quantum physics. The big
problem (pun intended) is to get TeX to automatically choose the
correct size (big, bigg, Big, etc or some other size) of delimiter
satisfying: (1) appropriate to contain each of the two arguments 'a'
and 'b', which may be complex expressions, and (2) with all three
delimiters the same size. [There is a package on some sites called
"hep.tex" which does Dirac bra-kets, but it doesn't attempt to get the
right delimiter sizes---they're always small size---so it doesn't meet
my requirements.]
[What is really called for is:
\def\braket#1#2{\left< #1 \middle| #2 \right>}
but unfortunately there is no \middle operator.]
The first trick is to use \vphantom to force all of the delimiters to
be the same size:
\def\braket#1#2{\left< #1 \vphantom{#2} \right|
\left. #2 \vphantom{#1} \right>}
Well, that's almost it---the delimiters are the right size, and
everything's where it belongs...except that a null delimiter (the
"\left.") actually causes TeX to put a little space, of width
\nulldelimiterspace, in the output. So the result looks like:
<a| b>
The obvious thing is to set \nulldelimiterspace=0pt; how to do it is a
little less obvious, though. Simply putting it in the macro as
written fails, because math mode spacing is not computed until the
moment that math mode is left, which doesn't happen until after the
context of the macro has been left. By that time our value of
\nulldelimiterspace has been restored back to its old value, so the
space is still there. Still, following the example of the definitions
of \bigr, etc in plain.tex, we try the \hbox trick:
\def\braket#1#2{\hbox{$\left< #1 \vphantom{#2} \right|
\left. #2 \vphantom{#1} \right>
\nulldelimiterspace=0pt
\mathsurround=0pt$}}
(the \mathsurround=0pt is to prevent TeX adding additional space
because of the "$ $" pair). This is almost correct; the only
remaining problem is that the arguments are now set in \textstyle,
even if the original context was a \displaystyle. So if, for example,
a fraction is put in the bra-ket, it is typeset small rather than
large. That's not what I wanted. The only way I found to get around
this is to use the combersome \mathchoice command, which requires four
copies of the argument. To shorten the notation, I defined an
auxiliary function \layermath, which adds a layer of \hbox to a math
expression, and the following (final!) version of \braket, and a
couple more trivial analogous commands dealing with "bra"s and "ket"s.
(The only reason that the others are more trivial is that they have an
even number of delimiters and so don't need any null delimiters.)
================= The final version: ======================================
\def\bra#1{\left< #1 \right|}
\def\ket#1{\left| #1 \right>}
\def\layermath#1{
\mathchoice
{\hbox{$\displaystyle #1 \mathsurround=0pt$}}
{\hbox{$\textstyle #1 \mathsurround=0pt$}}
{\hbox{$\scriptstyle #1 \mathsurround=0pt$}}
{\hbox{$\scriptscriptstyle #1 \mathsurround=0pt$}}
}
% produce <#1|#2> (a Dirac "bra-ket" inner product):
\def\braket#1#2{\mathinner{\layermath{
\mathopen{\left< #1 \vphantom{#2} \right|}
\left. #2 \vphantom{#1} \right>
\nulldelimiterspace=0pt
}}}
% produce <#1|#2|#3> (a Dirac matrix element):
\def\matrixel#1#2#3{\mathinner{
\mathopen{\left< #1 \vphantom{#2#3} \right|}
#2
\mathclose{\left| #3 \vphantom{#1#2} \right>}
}}
% produce |#1><#2| (a Dirac projection operator):
\def\ketbra#1#2{
\mathord{\left| #1 \vphantom{#2} \right>}
\mathord{\left< #2 \vphantom{#1} \right|}
}
===========================================================================
Everything there has been explained above, except the use of
\mathinner, \mathopen, \mathclose, and \mathord, which ensure that the
spacing surrounding this expression is correct.
This solution seems to work; let me know if you find any problems.
However, it is quite inefficient, due to the fact that each of the
arguments is typeset 8 (!) times to produce a \braket. I suppose that
this could be improved by storing them in boxes during processing, but
the arguments are typically small so I didn't worry about it. (Let me
know if you make this modification.)
I hope this posting prevents at least one person from having to figure
these contortions out again. I will try to upload these macros to one
of the archive sites, too.
Michael
mha...@krl.caltech.edu
[macros with \phantom-s omitted]
>However, it is quite inefficient, due to the fact that each of the
>arguments is typeset 8 (!) times to produce a \braket.
That could be cut to 4 each, but there is a much more efficient way,
suitable only for | in the middle. The following should appear
"sometime soon" in TeX and TUG news...
\noindent {\bf Question:} Is it possible to automatically resize a group of
{\em three\/} delimiters the way \verb"\left" and \verb"\right" work for two?
\smallskip
\noindent {\bf Answer:} In general, no. \ But if the middle delimiter is a
vertical line it is possible to use the \verb"\vrule" command instead of the
vertical bar character. A vertical rule automatically expands to the height
and depth of its surroundings, as in
$$
\hbox{\verb"\left[ {\sum} \vrule {\int} \right]"}
\ \Longrightarrow\
\left[ {\sum} \vrule {\int} \right]\,.
$$
This is a bit cramped, so it is best to insert some space around the
\verb"\vrule" (using \verb"\," for example). It is even possible to get fancy
with macros and have the `\verb"|"' character interpreted as `\verb"\vrule"',
as in this macro for bra-ket notation:
\begin{verbatim}
\def\Braket#1{\left\langle \mathcode`\|"8000 {#1}\right\rangle}
{\catcode`\|=\active \gdef|{\egroup\,\vrule\,\bgroup}}
\end{verbatim}
\def\Braket#1{\left\langle \mathcode`\|"8000 {#1}\right\rangle}
{\catcode`\|=\active \gdef|{\egroup\,\vrule\,\bgroup}}
so that
$$
\hbox{\verb"\Braket{\phi|\frac{\partial}{\partial t}|\psi}"}
\ \Longrightarrow\
\Braket{\phi|\frac{\partial}{\partial t}|\psi}\,.
$$
The size of the vertical line may not be exactly the same as the left and right
delimiters, but it is never far off.
If the middle delimiter is not a vertical line, then you are stuck with
using multiple \verb"\left" and \verb"\right", and a few \verb"\phantom"s.
\end{document}
My whole suite of braket macros has lowercase versions for the small
size and capitalized versions for variable size:
\def\bra#1{\mathinner{\langle{#1}|}}
\def\ket#1{\mathinner{|{#1}\rangle}}
\def\braket#1{\mathinner{\langle{#1}\rangle}}
\def\Bra#1{\left\langle#1\right|}
\def\Ket#1{\left|#1\right\rangle}
\def\Braket#1{\left\langle \mathcode`\|"8000 {#1}\right\rangle}
{\catcode`\|=\active \gdef|{\egroup\,\vrule\,\bgroup}}
The last one would need to be changed if you use | as an active character
for indexing or verbatim.
Donald Arseneau as...@reg.triumf.ca
: The goal is to write a macro so that
: $\braket{a}{b}$
: generates something like:
: $\bigl< a \bigm| b \bigr>$
A small point: \langle and \rangle look slightly better than < and >
(they are more obtuse angles). Besides, they are the right kind of atom ---
Open and Close in stead of Rel.
Jeroen Nijhof
>Michael Haggerty (mha...@rigel.krl.caltech.edu) wrote:
>: $\bigl< a \bigm| b \bigr>$
>A small point: \langle and \rangle look slightly better than < and >
>(they are more obtuse angles). Besides, they are the right kind of atom ---
>Open and Close in stead of Rel.
Actually, things are set up (via \delcode) so that "\left<" produces
exactly the same result as \left\langle. And \bigl, as part of its
work, issues a \left. However, it is true that a bare "<" is quite
different from a bare "\langle"
Dan Luecking
--
luec...@comp.uark.edu | Anti-disclaimer:
Department of Mathematical Sciences | You are free to ascribe any
University of Arkansas | opinions herein contained
Fayetteville, AR 72701 USA | to anyone or anything.
> Date: Tue, 30 Aug 1994 03:48:09 PST
> From: Donald Arseneau <as...@erich.triumf.ca>
> Subject: Re: Dirac bra-kets: subtler than you might think
>
> That could be cut to 4 each, but there is a much more efficient way,
> suitable only for | in the middle. The following should appear
> "sometime soon" in TeX and TUG news...
>
> \noindent {\bf Question:} Is it possible to automatically resize a group of
> {\em three\/} delimiters the way \verb"\left" and \verb"\right" work
> for two?
>
> \smallskip
>
> \noindent {\bf Answer:} In general, no. [...]
I like your solution, especially the fact that you can put |'s
wherever you like within the \Braket macro and they come out right. I
would just like to point out, though, that "In general, yes" it's
possible to do this with any three delimiters (the middle one needn't
be a "|") even though it's cumbersome as indicated in my previous
post:
\def\layermath#1{
\mathchoice
{\hbox{$\displaystyle #1 \mathsurround=0pt$}}
{\hbox{$\textstyle #1 \mathsurround=0pt$}}
{\hbox{$\scriptstyle #1 \mathsurround=0pt$}}
{\hbox{$\scriptscriptstyle #1 \mathsurround=0pt$}}
}
% produce an expression with three matching delimiters:
\def\threedelims#1#2#3#4#5{\mathinner{\layermath{
\mathopen{\left#1 #2 \vphantom{#4} \right#3}
\mathclose{\left. #4 \vphantom{#2} \right#5}
\nulldelimiterspace=0pt
}}}
% produce <#1|#2> (a Dirac "bra-ket" inner product):
\def\braket#1#2{\threedelims<{#1}|{#2}>}
This works for me...
Yours,
Michael
mha...@krl.caltech.edu
But \left< is the same as \left\langle. Hmmm. It should also be more
efficient because < has an internal delimiter-code while \langle is
a macro for \delimiter"abcdef (what? You expect me to memorize a six-
digit (higit) number?) and the number must be parsed. I'll benchmark
methods and probably change my macros.
Donald Arseneau as...@reg.triumf.ca
I believe the second variation given in your post can work, if you
add a simple adjustment for \nulldelimiterspace:
\def\braket#1#2{\left< #1 \vphantom{#2} \right|
\kern-\nulldelimiterspace \left. #2 \vphantom{#1} \right>}
^^^^^^^^^^^^^^^^^^^^^^^^^
Regards, Michael Downes
>I've experimented for hours and finally found a general macro to get
>TeX or LaTeX to output Dirac bra-ket notation as used in quantum
>physics. The problem is much subtler than it looks, as described
>below.
....stuff deleted....
>The goal is to write a macro so that
>
> $\braket{a}{b}$
>
>generates something like:
>
> $\bigl< a \bigm| b \bigr>$
>
>which of course looks like
>
> <a|b>
>
....more deleted....
>The first trick is to use \vphantom to force all of the delimiters to
>be the same size:
>
> \def\braket#1#2{\left< #1 \vphantom{#2} \right|
> \left. #2 \vphantom{#1} \right>}
>
>Well, that's almost it---the delimiters are the right size, and
>everything's where it belongs...except that a null delimiter (the
>"\left.") actually causes TeX to put a little space, of width
>\nulldelimiterspace, in the output. So the result looks like:
>
> <a| b>
>
....lots of stuff about \mathchoice etc deleted....
That's nearly my solution which is:
\def\braket#1#2{\left< #1 \left|\vphantom{{#1}{#2}}\right.\! #2 \right> }
where the \! (negative thin space) gets rid of the \nulldelimiterspace.
Much simpler than all that stuff with \mathchoice (yuk!)
Andrew
Donald> My whole suite of braket macros has lowercase versions for the
Donald> small size and capitalized versions for variable size:
I like that \vrule solution. But while we're at physics: what do
people use for `Feynman slashes'? I confess to using the horrible
\def\fmslash#1{#1\hskip -0.5em/}
which looks adequate (but not more) for most characters. Short of
defining an individually tuned skip amount for each character, I
haven't found a better solution yet. Any takers?
Cheers,
-Thorsten
--
/// Thorsten Ohl, TH Darmstadt, Schlossgartenstr. 9, D-64289 Darmstadt, Germany
//////////////// net: o...@crunch.ikp.physik.th-darmstadt.de, o...@gnu.ai.mit.edu
/// voice: +49-6151-16-3116, secretary: +49-6151-16-2072, fax: +49-6151-16-2421
For use with LaTeX2e, the macros need some slight modification, if I find the
time to do it, I should polish them and republish, eventually as a minor package
for LaTeX2e. But dont't give too much on this word...
--J"org Knappen.
Could some kind soul post these macros ?
Maurizio Loreti
Un. of Padova, Dept. of Physics -- Padova, Italy
I have dug out the reference again, here it is:
TeXhax Digest Friday, 25 Sep 1992 Volume 92 : Issue 017
You can retrieve the TeXhax issue from the CTAN archives, I think it is stored
as file texhax92.017.
--J"org Knappen.
:
:Maurizio Loreti
Well, I have grabbed these macros: here they are -- but they do
not solve my problem (what exactly is my problem, I'll say later).
------------------------------begin quoted text
% Four slash macros:
\def\slashi#1{\rlap{\sl/}#1}
% slashi works best for:
%a,c,e,f,g,h,k,o,r,s,u,v,x,y,z,I,J,S,T,V,Y,Z,\epsilon,\varepsilon,\partial
%
\def\slashii#1{\setbox0=\hbox{$#1$} % set a box for #1
\dimen0=\wd0 % and get its size
\setbox1=\hbox{\sl/} \dimen1=\wd1 % get size of /
\ifdim\dimen0>\dimen1 % #1 is bigger
\rlap{\hbox to \dimen0{\hfil\sl/\hfil}} % so center / in box
#1 % and print #1
\else % / is bigger
\rlap{\hbox to \dimen1{\hfil$#1$\hfil}} % so center #1
\hbox{\sl/} % and print /
\fi} %
% \slashii works best for:
% b,i,l,n,t,w,x,y,z,A,B,E,L,Q,W
%
\def\slashiii#1{\setbox0=\hbox{$#1$}#1\hskip-\wd0\hbox to\wd0{\hss\sl/\/\hss}}
% \slashiii works best for:
% d,f,j,l,\ell,m,w,x,z,C,D,F,G,H,K,L,M,N,O,P,Q,U,W,X,\Nabla,\partial
%
\def\slashiv#1{#1\llap{\sl/}}
% \slashiv works best for:
% e,g,p,q,y,z
------------------------------end quoted text
I'm trying to define a "slashed" symbol that has to be used also in
fractions ans sub/superscripts; e.g. like
\def\ebar{{\slashiii{E}}}
...
I have found that $\frac{\ebar}{c}$ is 2
$$ A_\ebar = 0 $$
\def\ebar{{\rlap/E}} works, but the placement of the / is UGLY.
When I try something different, like \hbox{E\kern-0.5em\lower-0.1ex\hbox{/}}
does not work for fractions; I have tried with boxes, etc. to no
avail - and studied the definition of \notin in the TeXbook without success.
QUESTION: has someone a macro for a "slashed" character, where the
placement of the slash can be adjusted to fit nicely over that
particular character, and that cen be used in fractions, subscripts
and superscripts?
\left\langle {a}
\mathrel{\left|{\vphantom{a b}}
\right.\kern-\nulldelimiterspace} {b} \right\rangle
Does anyone see anything wrong or undesirable in this.
If so, please get in touch with me so that I can change
the MathType translator.
Comments on other MathType TeX translations are also welcome.
I have some doubts about long division, repeated integrals,
and a few others.
thanks
george