summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/physics/physics.sty
blob: 30205692737a09b8cb030b40b6d8cc3d8a6d262b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
\ProvidesPackage{physics}
% physics 1.10
% This material is subject to the LaTeX Project Public License.
% See http://www.ctan.org/tex-archive/help/Catalogue/licenses.lppl.html for the details of that license.
%
% Handy commands for physicists including macros for vectors, calculus, and bra-ket (Dirac) notation
% Requires xparse package, which comes bundled with l3packages and l3kernel
% This package loads amsmath, which comes standard with most latex distributions
% The commands defined in this package will silently overwrite previous commands with the same name, if such commands exist
%
% Created by Sergio C. de la Barrera
% Updated on October 10, 2012
% Uploaded on October 11, 2012

\RequirePackage{xparse}
\RequirePackage{amsmath}

% Vector notation
\DeclareDocumentCommand\vectorbold{ s m }{\IfBooleanTF{#1}{\boldsymbol{#2}}{\mathbf{#2}}} % Vector bold [star for Greek and italic Roman]
\DeclareDocumentCommand\vb{}{\vectorbold} % Shorthand for \vectorbold

\DeclareDocumentCommand\vectorarrow{ s m }{\IfBooleanTF{#1}{\vec{\boldsymbol{#2}}}{\vec{\mathbf{#2}}}} % Vector arrow + bold [star for Greek and italic Roman]
\DeclareDocumentCommand\va{}{\vectorarrow} % Shorthand for \vectorarrow

\DeclareDocumentCommand\vectorunit{ s m }{\IfBooleanTF{#1}{\boldsymbol{\hat{#2}}}{\mathbf{\hat{#2}}}} % Unit vector [star for Greek and italic Roman]
\DeclareDocumentCommand\vu{}{\vectorunit} % Shorthand for \vectorunit

\DeclareDocumentCommand\dotproduct{}{\boldsymbol\cdot} % Vector dot product symbol
\DeclareDocumentCommand\vdot{}{\dotproduct} % Shorthand for \dotproduct [note that the command sequence \dp is protected]

\DeclareDocumentCommand\crossproduct{}{\boldsymbol\times} % Vector cross product symbol
\DeclareDocumentCommand\cross{}{\crossproduct} % Shorthand for \crossproduct
\DeclareDocumentCommand\cp{}{\crossproduct} % Shorthand for \crossproduct

\DeclareDocumentCommand\vnabla{}{\boldsymbol\nabla} % Vector bold \nabla symbol
\DeclareDocumentCommand\gradient{ s g }{\IfNoValueTF{#2}{\vnabla}{\IfBooleanTF{#1}{\vnabla\!\left(#2\right)}{\vnabla #2}}} % Gradient [star for parentheses]
\DeclareDocumentCommand\grad{}{\gradient} % Shorthand for \gradient

\DeclareDocumentCommand\divergence{ s g }{\IfNoValueTF{#2}{\vnabla \vdot}{\IfBooleanTF{#1}{\vnabla \vdot \left(#2\right)}{\vnabla \vdot #2}}} % Divergence [star for parentheses]
\let\divisionsymbol\div % Rename \div [division symbol] in order to free up control sequence for \divergence
\let\div\relax
\DeclareDocumentCommand\div{}{\divergence} % Shorthand for \divergence

\DeclareDocumentCommand\curl{ s g }{\IfNoValueTF{#2}{\vnabla \cross}{\IfBooleanTF{#1}{\vnabla \cross \left(#2\right)}{\vnabla \cross #2}}} % Curl [star for parentheses]
\DeclareDocumentCommand\laplacian{ s g }{\IfNoValueTF{#2}{\nabla^2}{\IfBooleanTF{#1}{\nabla^2\!\left(#2\right)}{\nabla^2 #2}}} % Laplacian [star for parentheses]

% Brackets and braces
\DeclareDocumentCommand\quantity{ t\big t\Big t\bigg t\Bigg g o d() d|| }{ % Flexible automatic bracketing of an expression in () or [] or {} or ||
	% Handles manual override of sizing
	\IfBooleanTF{#1}{\let\ltag\bigl \let\rtag\bigr}{
		\IfBooleanTF{#2}{\let\ltag\Bigl \let\rtag\Bigr}{
			\IfBooleanTF{#3}{\let\ltag\biggl \let\rtag\biggr}{
				\IfBooleanTF{#4}
					{\let\ltag\Biggl \let\rtag\Biggr}
					{\let\ltag\left \let\rtag\right}
				}
			}
		}
	% Handles actual bracketing
	\IfNoValueTF{#5}{
		\IfNoValueTF{#6}{
			\IfNoValueTF{#7}{
				\IfNoValueTF{#8}
					{()}
					{\ltag\lvert{#8}\rtag\rvert}
				}
				{\ltag(#7\rtag) \IfNoValueTF{#8}{}{#8}}
			}
			{\ltag[#6\rtag] \IfNoValueTF{#7}{}{[#7]} \IfNoValueTF{#8}{}{#8}}
		}
		{\ltag\lbrace#5\rtag\rbrace  \IfNoValueTF{#6}{}{|#6|} \IfNoValueTF{#7}{}{[#7]} \IfNoValueTF{#8}{}{#8}}
	}
\DeclareDocumentCommand\qty{}{\quantity} % Shorthand for \quantity

\DeclareDocumentCommand\absolutevalue{ t\big t\Big t\bigg t\Bigg m }{ % Absolute value/complex modulus
	\IfBooleanTF{#1}{\quantity\big|#5|}{
		\IfBooleanTF{#2}{\quantity\Big|#5|}{
			\IfBooleanTF{#3}{\quantity\bigg|#5|}{
				\IfBooleanTF{#4}{\let\sizetag\Bigg}{\quantity|#5|}
			}
		}
	}
}
\DeclareDocumentCommand\abs{}{\absolutevalue} % Shorthand for \absolutevalue

\DeclareDocumentCommand\norm{m}{{\left\lVert{#1}\right\rVert}} % Norm
\DeclareDocumentCommand\order{g}{{\mathcal{O}\IfNoValueTF{#1}{}{\!\left(#1\right)}}} % Order symbol -> O(x^2)
\DeclareDocumentCommand\poissonbracket{ m m }{{\left\lbrace{#1},{#2}\right\rbrace}} % Poisson bracket [same as anti-commutator]
\DeclareDocumentCommand\pb{}{\poissonbracket} % Shorthand for \poissonbracket

% Commutators
\DeclareDocumentCommand\commutator{ m m }{\left[{#1},{#2}\right]} % Commutator
\DeclareDocumentCommand\comm{}{\commutator} % Shorthand for \commutator

\DeclareDocumentCommand\anticommutator{ m m }{\left\lbrace{#1},{#2}\right\rbrace} % Anticommutator [same as Poisson bracket]
\DeclareDocumentCommand\acommutator{}{\anticommutator} % Shorthand for \anticommutator
\DeclareDocumentCommand\acomm{}{\anticommutator} % Shorthand for \anticommutator

% Operators
\DeclareMathOperator{\tr}{tr} % Trace and partial trace [w/subscript]
\DeclareMathOperator{\rank}{rank} % Rank of a matrix
\DeclareMathOperator{\erf}{erf} % Gauss error function

% Quick quad text (math-mode text with \quad spacing)
\DeclareDocumentCommand\qqtext{ s m }{\IfBooleanTF{#1}{}{\quad}\text{#2}\quad}
\DeclareDocumentCommand\qq{}{\qqtext}

\DeclareDocumentCommand\qcomma{}{,\quad}
\DeclareDocumentCommand\qc{}{\qcomma}

\DeclareDocumentCommand\qif{s}{\IfBooleanTF{#1}{}{\quad}\text{if}\quad}
\DeclareDocumentCommand\qthen{s}{\IfBooleanTF{#1}{}{\quad}\text{then}\quad}
\DeclareDocumentCommand\qelse{s}{\IfBooleanTF{#1}{}{\quad}\text{else}\quad}
\DeclareDocumentCommand\qotherwise{s}{\IfBooleanTF{#1}{}{\quad}\text{otherwise}\quad}
\DeclareDocumentCommand\qunless{s}{\IfBooleanTF{#1}{}{\quad}\text{unless}\quad}
\DeclareDocumentCommand\qgiven{s}{\IfBooleanTF{#1}{}{\quad}\text{given}\quad}
\DeclareDocumentCommand\qusing{s}{\IfBooleanTF{#1}{}{\quad}\text{using}\quad}
\DeclareDocumentCommand\qfor{s}{\IfBooleanTF{#1}{}{\quad}\text{for}\quad}
\DeclareDocumentCommand\qall{s}{\IfBooleanTF{#1}{}{\quad}\text{all}\quad}
\DeclareDocumentCommand\qeven{s}{\IfBooleanTF{#1}{}{\quad}\text{even}\quad}
\DeclareDocumentCommand\qodd{s}{\IfBooleanTF{#1}{}{\quad}\text{odd}\quad}
\DeclareDocumentCommand\qinteger{s}{\IfBooleanTF{#1}{}{\quad}\text{integer}\quad}
\DeclareDocumentCommand\qand{s}{\IfBooleanTF{#1}{}{\quad}\text{and}\quad}
\DeclareDocumentCommand\qor{s}{\IfBooleanTF{#1}{}{\quad}\text{or}\quad}
\DeclareDocumentCommand\qas{s}{\IfBooleanTF{#1}{}{\quad}\text{as}\quad}
\DeclareDocumentCommand\qin{s}{\IfBooleanTF{#1}{}{\quad}\text{in}\quad}
\DeclareDocumentCommand\qcc{s}{\IfBooleanTF{#1}{}{\quad}\text{c.c.}\quad}

% Derivatives
\DeclareDocumentCommand\differential{ s o g }{{ % Differential 'd'
	% s: star for parentheses as in d(cos x)
	% o: optional n for nth differential
	% g: optional argument for readability and to control spacing
	\IfNoValueTF{#3}
		{\mathrm{d}\IfNoValueTF{#2}{}{^{#2}}}
		{\IfBooleanTF{#1}
			{\mathrm{d}\IfNoValueTF{#2}{}{^{#2}}\!\left(#3\right)}
			{\mathrm{d}\IfNoValueTF{#2}{}{^{#2}} #3 \,}
		}
	}}
\DeclareDocumentCommand\dd{}{\differential} % Shorthand for \differential

\DeclareDocumentCommand\derivative{ o m m o }{{ % Total derivatives
	% o: optional n for nth derivative
	% m: mandatory (f in df/dx)
	% m: mandatory (x in df/dx)
	% o: optional evaluation point
	\IfNoValueTF{#4}{}{\left.}\frac{\dd \IfNoValueTF{#1}{}{^{#1}} #2}{\dd #3\IfNoValueTF{#1}{}{^{#1}}}\IfNoValueTF{#4}{}{\right\rvert_{#4}}
	}}
\DeclareDocumentCommand\dv{}{\derivative} % Shorthand for \derivative
	
\DeclareDocumentCommand\partialderivative{ s o m m g o }{ % Partial derivatives
	% s: star for shorthand curly-d notation [default is Leibniz mode]
	% o: optional n for nth derivative [Leibniz mode]
	% m: mandatory (f in df/dx) [both modes]
	% m: mandatory (x in df/dx) [both modes]
	% g: optional (y in d^2f/dxdy) [both modes]
	% o: optional evaluation point [Leibniz mode]
	\IfNoValueTF{#5}{
		\IfBooleanTF{#1}
			{\partial_{#4} {#3}}
			{\IfNoValueTF{#6}{}{\left.}\frac{\partial\IfNoValueTF{#2}{}{^{#2}} #3}{\partial #4\IfNoValueTF{#2}{}{^{#2}}}\IfNoValueTF{#6}{}{\right\rvert_{#6}}}
		}{
		\IfBooleanTF{#1}
			{\partial_{{#4}{#5}} {#3}}
			{\IfNoValueTF{#6}{}{\left.}\frac{\partial^2 #3}{\partial #4 \partial #5}\IfNoValueTF{#6}{}{\right\rvert_{#6}}}
		}
	}
\DeclareDocumentCommand\pderivative{}{\partialderivative} % Shorthand for \partialderivative
\DeclareDocumentCommand\pdv{}{\partialderivative} % Shorthand for \partialderivative

% Bra-ket notation
\DeclareDocumentCommand\bra{ s m t\ket{g} }{ % Bra
	\IfBooleanTF{#1}
		{\left\lvert{#2}\right\rangle \IfNoValueTF{#4}{}{#4}} % Complex conjugate
		{\IfBooleanTF{#3}{\braket{#2}{\IfNoValueTF{#4}{}{#4}}}{\left\langle{#2}\right\rvert \IfNoValueTF{#4}{}{#4}}} % No star --- allow contraction
	}
	
\DeclareDocumentCommand\ket{ s m }{ % Ket
	\IfBooleanTF{#1}
		{\left\langle{#2}\right\rvert} % Complex conjugate
		{\left\lvert{#2}\right\rangle} % No star
	}
	
\DeclareDocumentCommand\innerproduct{ s m g }{ % Inner product
	\IfNoValueTF{#3}
		{\left\langle{#2}\vert{#2}\right\rangle}
		{ % Two arguments
		\IfBooleanTF{#1}
			{\left\langle{#3}\middle\vert{#2}\right\rangle} % Complex conjugate
			{\left\langle{#2}\middle\vert{#3}\right\rangle} % No star
		}
	}
\DeclareDocumentCommand\braket{}{\innerproduct} % Shorthand for \innerproduct
\DeclareDocumentCommand\ip{}{\innerproduct} % Shorthand for \innerproduct
	
\DeclareDocumentCommand\outerproduct{ s m g }{ % Dyad
	\IfNoValueTF{#3}
		{\left\lvert{#2}\middle\rangle\!\middle\langle{#2}\right\rvert}
		{ % Two arguments
		\IfBooleanTF{#1}
			{\left\lvert{#3}\middle\rangle\!\middle\langle{#2}\right\rvert}
			{\left\lvert{#2}\middle\rangle\!\middle\langle{#3}\right\rvert}
		}
	}
\DeclareDocumentCommand\dyad{}{\outerproduct} % Alternative for \outerproduct
\DeclareDocumentCommand\op{}{\dyad} % Shorthand for \outerproduct
\DeclareDocumentCommand\ketbra{}{\dyad} % Alternative for \outerproduct
	
\DeclareDocumentCommand\expectationvalue{ m g }{ % Expectation value
	\IfNoValueTF{#2}
		{\left\langle{#1}\right\rangle}
		{\left\langle{#2}\middle\vert{#1}\middle\vert{#2}\right\rangle}
	}
\DeclareDocumentCommand\expval{}{\expectationvalue} % Shorthand for \expectationvalue
\DeclareDocumentCommand\ev{}{\expectationvalue} % Shorthand for \expectationvalue
\DeclareDocumentCommand\vev{ m }{\expectationvalue{#1}{0}} % Vacuum expectation value
	
\DeclareDocumentCommand\matrixelement{ m m m }{\left\langle{#1}\middle\vert{#2}\middle\vert{#3}\right\rangle} % Matrix element
\DeclareDocumentCommand\matrixel{}{\matrixelement} % Shorthand for \matrixelement
\DeclareDocumentCommand\mel{}{\matrixelement} % Shorthand for \matrixelement
\DeclareDocumentCommand\homework{}{ % You can try it
	\ensuremath{
	\div{\vb{E}}=\frac{\rho}{\epsilon_0} \qc
	\div{\vb{B}}=0 \qc
	\curl{\vb{E}}=-\pdv{\vb{B}}{t}\qc
	\curl{\vb{B}}=\mu_0\vb{J}+\frac{1}{c^2}\pdv{\vb{E}}{t}\qc
	H\ket{\Psi}=i\hbar\pdv{}{t}\ket{\Psi},
	\qq{all else follows.}
	}
}