Uploaded by Christopher Ji

LaTeX physics package

advertisement
The physics package
Sergio C. de la Barrera
physics.tex@gmail.com
December 12, 2012
Contents
1 Before you start
1.1 The purpose of this package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2 Other required packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.3 Using physics in your LATEX document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
1
1
2
2 List
2.1
2.2
2.3
2.4
2.5
2.6
2.7
2
2
3
4
5
5
6
7
1
1.1
of commands
Automatic bracing . .
Vector notation . . . .
Operators . . . . . . .
Quick quad text . . .
Derivatives . . . . . .
Dirac bra-ket notation
Matrix macros . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Before you start
The purpose of this package
The goal of this package is to make typesetting equations for physics simpler, faster, and more humanreadable. To that end, the commands included in this package have names that make the purpose of each
command immediately obvious and remove any ambiguity while reading and editing physics code. From a
practical standpoint, it is handy to have a well-defined set of shortcuts for accessing the long-form of each of
these commands. The commands listed below are therefore defined in terms of their long-form names and
then shown explicitly in terms of the default shorthand command sequences. These shorthand commands
are meant make it easy to remember both the shorthand names and what each one represents.
1.2
Other required packages
The physics package requires xparse and amsmath to work properly in your LATEX document. The amsmath
package comes standard with most LATEX distributions and is loaded by physics for your convenience. You
may also already have xparse installed on your system as it is a popular package for defining LATEXmacros,
however, if you are unsure you can either install it again using your local package manager (comes with most
distributions) or by visiting the CTAN online package database, or you could even just try to use physics
1
without worrying about it. Many modern LATEXcompilers will locate and offer to download missing packages
for you.
1.3
Using physics in your LATEX document
To use the physics package, simply insert \usepackage{physics} in the preamble of your document, before
\begin{document} and after \documentclass{class}:
\documentclass{class}
...
\usepackage{physics}
...
\begin{document}
content...
\end{document}
2
2.1
List of commands
Automatic bracing
\quantity
\qty(\typical) → ( )
automatic ( ) braces
\qty(\tall) →
\qty(\grande) →
\qty[\typical] → [ ]
\qty|\typical| → | |
\qty{\typical} → { }
\qty\big{} → no
automatic [ ] braces
automatic | | braces
automatic { } braces
manual sizing (works with any of the
above bracket types)
\qty\Big{} →
\qty\bigg{} →
()
\qty\Bigg{} →
\absolutevalue
↔ \qty()
↔ \qty[]
↔ \qty||
↔ \qty{}
→ |a|
alternative syntax; robust and more
LATEX-friendly
\abs\Big{a} → a
inherits manual sizing syntax from \qty
\pqty{}
\bqty{}
\vqty{}
\Bqty{}
\abs{a}
\abs*{\grande} → |
\norm
automatic sizing; equivalent to \qty|a|
|
star for no resize
\norm{a} → kak
automatic sizing
\norm\Big{a} → a
manual sizing
\norm*{\grande} → k
k
star for no resize
∞
\evaluated
\eval{x}_0^\infty → x
vertical bar for evaluation limits
0
2
∞
\eval(x|_0^\infty →
alternate form
x
\eval[x|_0^\infty → x
0
∞
alternate form
0
∞
"
\eval[\venti|_0^\infty →
automatic sizing
0
\order
\eval*[\venti|_0^\infty →
\order{x^2} → O x2
∞
star for no resize
0
order symbol; automatic sizing and
space handling
\order\Big{x^2} → O x2
\order*{\grande} → O(
\commutator
manual sizing
)
star for no resize
\comm{A}{B} → [A, B]h
i
\comm\Big{A}{B} → A, B
automatic sizing
manual sizing
\comm*{A}{\grande} → [A,
\anticommutator
\poissonbracket
2.2
]
star for no resize
\acomm{A}{B} → {A, B}
\pb{A}{B} → {A, B}
same as \poissonbracket
same as \anticommutator
Vector notation
~ by
The default del symbol ∇ used in physics vector notation can be switched to appear with an arrow ∇
including the option arrowdel in the document preamble → \usepackage[arrowdel]{physics}.
\vectorbold
\vectorarrow
\vectorunit
\dotproduct
\crossproduct
\gradient
\divergence
\curl
\vb{a} → a
\vb*{a}, \vb*{\theta} → a, θ
\va{a} → ~a
\va*{a}, \va*{\theta} → ~a, ~θ
\vu{a} → â
\vu*{a}, \vu*{\theta} → â, θ̂
\vdot → · as in a · b
\cross → × as in a × b
\cp → × as in a × b
\grad → ∇
\grad{\Psi} → ∇Ψ
upright/no Greek
italic/Greek
upright/no Greek
italic/Greek
upright/no Greek
italic/Greek
note: \dp is a protected TEX primitive
alternate name
shorthand name
default mode
\grad(\Psi+\tall) → ∇ Ψ +
h
\grad[\Psi+\tall] → ∇ Ψ +
\div → ∇·
i
\div{\vb{a}} → ∇ · a
\div(\vb{a}+\tall) → ∇ · a +
h
\div[\vb{a}+\tall] → ∇ · a +
\curl → ∇×
\curl{\vb{a}} → ∇ × a
\curl(\vb{a}+\tall) → ∇ × a +
3
long-form (like \qty but also handles
spacing)
note: amsmath symbol ÷ renamed
\divisionsymbol
default mode
long-form
i
default mode
long-form
h
i
\curl[\vb{a}+\tall] → ∇ × a +
\laplacian → ∇2
\laplacian{\Psi} → ∇2 Ψ
\laplacian(\Psi+\tall) → ∇2 Ψ +
h
i
\laplacian[\Psi+\tall] → ∇2 Ψ +
\laplacian
2.3
default mode
long-form
Operators
The standard set of trig functions is redefined in physics to provide automatic braces that behave like
\qty(). In addition, an optional power argument is provided. This behavior can be switched off by including
the option notrig in the preamble → \usepackage[notrig]{physics}.
Example trig redefinitions:
\sin
\sin(\grande) → sin
automatic braces; old \sin renamed \sine
2
\sin[2](x) → sin (x)
\sin x → sin x
optional power
can still use without an argument
The full set of available trig functions in physics includes:
\sin(x)
\cos(x)
\tan(x)
\csc(x)
\sec(x)
\cot(x)
\sinh(x)
\cosh(x)
\tanh(x)
\csch(x)
\sech(x)
\coth(x)
\arcsin(x)
\arccos(x)
\arctan(x)
\arccsc(x)
\arcsec(x)
\arccot(x)
sin(x)
\asin(x)
cos(x)
\acos(x)
tan(x)
\atan(x)
⇒
csc(x)
\acsc(x)
sec(x)
\asec(x)
cot(x)
\acot(x)
sinh(x)
cosh(x)
tanh(x)
csch(x)
sech(x)
coth(x)
arcsin(x)
arccos(x)
arctan(x)
arccsc(x)
arcsec(x)
arccot(x)
asin(x)
acos(x)
atan(x)
acsc(x)
asec(x)
acot(x)
The standard trig functions (plus a few that are missing in amsmath) are available without any automatic
bracing under a new set of longer names:
\sine
\cosine
\tangent
\cosecant
\secant
\cotangent
\hypsine
\hypcosine
\hyptangent
\hypcosecant
\hypsecant
\hypcotangent
\arcsine
\arccosine
\arctangent
\arccosecant
\arcsecant
\arccotangent
Similar behavior has also been extended to the
\exp(\tall) exp \log(\tall) log \ln(\tall)
ln
old definitions ⇒
\det(\tall) det \Pr(\tall)
Pr
New operators:
\trace or \tr
\Trace or \Tr
\rank
\erf
\Res
\principalvalue
\asine
\acosine
\atangent
\acosecant
\asecant
\acotangent
following functions:
\exponential
\logarithm
\naturallogarithm
\determinant
\Probability
\tr\rho → tr ρ also \tr(\tall) → tr
\Tr\rho → Tr ρ
\rank M → rank M
\erf(x)→ erf(x)
\Res[f(z)]→ Res[f (z)]
R
\pv{\int f(z) \dd{z}}→ P fR(z) dz
\PV{\int f(z) \dd{z}}→ P.V. f (z) dz
4
trace; same bracing as trig functions
alternate
matrix rank
Gauss error function
residue; same bracing as trig functions
Cauchy principal value
alternate
\Re{z} → Re{z}
\Im{z} → Im{z}
\Re
\Im
2.4
old \Re renamed to \real → <
old \Im renamed to \imaginary → =
Quick quad text
This set of commands produces text in math-mode padded by \quad spacing on either side. This is meant
to provide a quick way to insert simple words or phrases in a sequence of equations. Each of the following
commands includes a starred version which pads the text only on the right side with \quad for use in aligned
environments such as cases.
General text:
\qqtext
\qq{}
\qq{word or phrase} → word or phrase
\qq*{word or phrase} → word or phrase
Special macros:
\qcomma or \qc →,
\qcc → c.c.
\qif → if
general quick quad text with argument
normal mode; left and right \quad
starred mode; right \quad only
right \quad only
complex conjugate; left and right \quad unless starred \qcc* → c.c.
left and right \quad unless starred \qif* → if
Similar to \qif:
\qthen, \qelse, \qotherwise, \qunless, \qgiven, \qusing, \qassume, \qsince,
\qlet, \qfor, \qall, \qeven, \qodd, \qinteger, \qand, \qor, \qas, \qin
2.5
Derivatives
The default differential symbol d which is used in \differential and \derivative can be switched to an
italic form d by including the option italicdiff in the preamble → \usepackage[italicdiff]{physics}.
\differential
\derivative
\partialderivative
\dd → d
\dd x → dx
\dd{x} → dx
\dd[3]{x} → d3 x
\dd(\cos\theta) → d(cos θ)
d
\dv{x} →
dx
df
\dv{f}{x} →
dx n
d f
\dv[n]{f}{x} →
dxn d
\dv{x}(\grande) →
dx
\dv*{f}{x} → df /dx
∂
\pderivative{x} →
∂x
∂
\pdv{x} →
∂x
∂f
\pdv{f}{x} →
∂x n
∂ f
\pdv[n]{f}{x} →
∂xn
5
no spacing (not recommended)
automatic spacing based on neighbors
optional power
long-form; automatic braces
one argument
two arguments
optional power
long-form; automatic braces, spacing
inline form using \flatfrac
alternate name
shorthand name
two arguments
optional power
∂
∂x
∂2f
\pdv{f}{x}{y} →
∂x∂y
\pdv*{f}{x} → ∂f /∂x
\var{F[g(x)]} → δF [g(x)]
\var(E-TS) → δ(E − T S)
δ
\fdv{g} →
δg
δF
\fdv{F}{g} →
δg
δ
\fdv{V}(E-TS) →
(E − T S)
δV
\fdv*{F}{x} → δF /δx
\pdv{x}(\grande) →
\variation
\functionalderivative
2.6
long-form
mixed partial
inline form using \flatfrac
functional variation (works like \dd)
long-form
functional derivative (works like \dv)
long-form
inline form using \flatfrac
Dirac bra-ket notation
The following collection of macros for Dirac notation contains two fundamental commands, \bra and \ket,
along with a set of more specialized macros which are essentially combinations of the fundamental pair. The
specialized macros are both useful and descriptive from the perspective of generating physics code, however,
the fundamental commands are designed to contract with one another algebraically when appropriate and
are thus suggested for general use. For instance, the following code renders correctly1
\bra{\phi}\ket{\psi} → hφ|ψi
as opposed to
hφ| |ψi
whereas a similar construction with higher-level macros will not contract in a robust manner
\bra{\phi}\dyad{\psi}{\xi} → hφ| |ψihξ| .
On the other hand, the correct output can be generated by sticking to the fundamental commands,
\bra{\phi}\ket{\psi}\bra{\xi} → hφ|ψi hξ|
allowing the user to type out complicated quantum mechanical expressions without worrying about bra-ket
contractions. That being said, the high-level macros do have a place in convenience and readability, as long
as the user is aware of rendering issues that may arise due to an absence of automatic contractions.
E
\ket
\ket{\tall} →
automatic sizing
\bra
\innerproduct
\ket*{\tall} →D |
i
no resize
\bra{\tall} →
automatic sizing
\bra*{\tall} → h |
\bra{\phi}\ket{\psi} → hφ|ψi
D
E
\bra{\phi}\ket{\tall} → φ
no resize
automatic contraction
\bra{\phi}\ket*{\tall} → hφ| i
\bra*{\phi}\ket{\tall} → hφ| i
\bra*{\phi}\ket*{\tall} → hφ| i
\braket{a}{b} → ha|bi
\braket{a} → ha|ai
a star on either term in the contraction
prohibits resizing
contraction inherits automatic sizing
two-argument braket
one-argument (norm)
1 Note the lack of a space between the bra and ket commands. This is necessary is order for the bra to find the corresponding
ket and form a contraction.
6
D
\braket{a}{\tall} → a
\outerproduct
\expectationvalue
E
\braket*{a}{\tall} → ha|
\ip{a}{b} → ha|bi
\dyad{a}{b} → |aihb|
\dyad{a} → |aiha|
ED
automatic sizing
i
no resize
shorthand name
two-argument dyad
one-argument (projector)
\dyad{a}{\tall} → a
automatic sizing
\dyad*{a}{\tall} → |aih |
\ketbra{a}{b} → |aihb|
\op{a}{b} → |aihb|
\expval{A} → hAi
\expval{A}{\Psi} → hΨ|A|Ψi
\ev{A}{\Psi} → hΨ|A|Ψi
no resize
alternative name
shorthand name
implicit form
explicit form
shorthand name
\ev{\grande}{\Psi} → hΨ|
|Ψi
\ev*{\grande}{\tall} → h |
\ev**{\grande}{\Psi} → Ψ
\matrixelement
default sizing ignores middle argument
|
i
Ψ
double star resizes based on all parts
\matrixel{n}{A}{m} → hn|A|mi
\mel{n}{A}{m} → hn|A|mi
requires all three arguments
shorthand name
\mel{n}{\grande}{m} → hn|
default sizing ignores middle argument
|mi
\mel*{n}{\grande}{\tall} → hn| | i
\mel**{n}{\grande}{m} → n
m
2.7
single star does no resizing whatsoever
single star does no resizing whatsoever
double star resizes based on all parts
Matrix macros
The following matrix macros produce unformatted rows and columns of matrix elements for use as separate
matrices as well as blocks within larger matrices. For example, the command \identitymatrix{2} which
has also has the shortcut \imat{2} produces the elements of a 2 × 2 identity matrix 10 01 without braces or
grouping. This allows the command to also be used within another matrix, as in:


\begin{pmatrix}
1 0
 0 1
\imat{2} \\ a & b
⇒
\end{pmatrix}
a b
To specify elements on the right of left sides of our \imat{2} sub-matrix we use the grouping command
\matrixquantity or \mqty to effectively convert \imat{2} into a single matrix element of a larger matrix:


\begin{pmatrix}
1 0 a
0 1 b 
\mqty{\imat{2}} & \mqty{a\\b} \\ \mqty{c & d} & e
⇒
\end{pmatrix}
c d e
The extra \mqty groups were required in this case in order to get the a and b elements to behave as a single
element, since \mqty{\imat{2}} also acts like a single matrix element (the same can be said of the grouped
c and d elements). Finally, the outermost pmatrix environment could have also been replaced with the
physics macro \mqty(), allowing the above example to be written on one line:


1 0 a
0 1 b 
\mqty(\mqty{\imat{2}} & \mqty{a\\b} \\ \mqty{c & d} & e) ⇒
c d e
7
\matrixquantity
\smallmatrixquantity
\matrixdeterminant
\identitymatrix
a b
c d
a b
\mqty(a & b \\ c & d) →
c d 

a b


\mqty*(a & b \\ c & d) → 


c d
a b
\mqty[a & b \\ c & d] →
c d
a b
\mqty|a & b \\ c & d| →
c d
\pmqty{} ↔ \mqty()
\Pmqty{} ↔ \mqty*()
\bmqty{} ↔ \mqty[]
\vmqty{} ↔ \mqty||
\smqty{a & b \\ c & d} → ac db
\smqty() or \spmqty{}
\smqty*() or \sPmqty{}
\smqty[] or \sbmqty{}
\smqty|| or \svmqty{}
a b
\mdet{a & b \\ c & d} →
c d
\smdet{a & b \\ c & d} → ac db
\imat{n}
\mqty{a & b \\ c & d} →
groups a set of matrix elements into a
single object
parentheses
alternate parentheses
square brackets
vertical bars
alternative syntax; robust and more
LATEX-friendly
the smallmatrix form of \mqty
small version of \mqty()
small version of \mqty*()
small version of \mqty[]
small version of \mqty||
matrix determinant
small matrix determinant
elements of n × n identity matrix
100
\xmatrix
\smqty(\imat{3}) → 0 1 0
001
\xmat{x}{n}{m}
1 1 1)
\smqty(\xmat{1}{2}{3}) → ( 111
\smqty(\xmat*{a}{3}{3}) →
\zeromatrix
\paulimatrix
\diagonalmatrix
\antidiagonalmatrix
a11 a12 a13
a21 a22 a23
aa31
a32 a33
1
a2
a3
( a1 a2 a3 )
\smqty(\xmat*{a}{3}{1}) →
\smqty(\xmat*{a}{1}{3}) →
\zmat{n}{m}
\smqty(\zmat{2}{2}) → ( 00 00 )
\pmat{n}
\smqty(\pmat{0}) → ( 10 01 )
\smqty(\pmat{1}) → ( 01 10 ) \smqty(\pmat{2}) → 0i −i
0 0
\smqty(\pmat{3}) → 10 −1
\dmat{a,b,c,...}

1
\mqty(\dmat{1,2,3}) →  2
formatted with \mqty or \smqty
elements of n × m matrix filled with x
formatted with \mqty or \smqty
as a vector with indices
n × m matrix filled with zeros
equivalent to \xmat{0}{n}{m}
nth Pauli matrix
n ∈ {0, 1, 2, 3 or x, y, z}


3
1 0
\mqty(\dmat[0]{1,2}) →
2
0 

1
2 3
\mqty(\dmat{1,2&3\\4&5}) → 
4 5
\admat{a,b,c,...}


1

\mqty(\admat{1,2,3}) →  2
3
8
star for element indices
specify up to eight diagonal or block diagonal elements
optional argument to fill spaces
enter matrix elements for each block as
a single diagonal element
same as syntax as \dmat
Download