\ProvidesPackage{physics}
% physics 1.20
% 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 31, 2012
% Uploaded on November 1, 2012

\RequirePackage{xparse}
\RequirePackage{amsmath}

% 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}{\quantity\Bigg|#5|}{\quantity|#5|}
			}
		}
	}
}
\DeclareDocumentCommand\abs{}{\absolutevalue} % Shorthand for \absolutevalue

\DeclareDocumentCommand\norm{ t\big t\Big t\bigg t\Bigg m }{ % Norm
	\IfBooleanTF{#1}{\bigl\lVert{#5}\bigr\rVert}{
		\IfBooleanTF{#2}{\Bigl\lVert{#5}\Bigr\rVert}{
			\IfBooleanTF{#3}{\biggl\lVert{#5}\biggr\rVert}{
				\IfBooleanTF{#4}{\Biggl\lVert{#5}\Biggr\rVert}{\left\lVert{#5}\right\rVert}
			}
		}
	}
}
\DeclareDocumentCommand\order{ t\big t\Big t\bigg t\Bigg m }{ % Order symbol -> O(x^2)
	\IfBooleanTF{#1}{\mathcal{O}\IfNoValueTF{#5}{}{\!\quantity\big(#5)}}{
		\IfBooleanTF{#2}{\mathcal{O}\IfNoValueTF{#5}{}{\!\quantity\Big(#5)}}{
			\IfBooleanTF{#3}{\mathcal{O}\IfNoValueTF{#5}{}{\!\quantity\bigg(#5)}}{
				\IfBooleanTF{#4}{\mathcal{O}\IfNoValueTF{#5}{}{\!\quantity\Bigg(#5)}}{\mathcal{O}\IfNoValueTF{#5}{}{\!\quantity(#5)}}
			}
		}
	}
}
\DeclareDocumentCommand\evaluated{ g d() }{ % Vertical evaluation bar or parentheses
	\IfNoValueTF{#1}{
		\IfNoValueTF{#2}
			{\rvert}
			{\quantity(#2)}
		}
		{\left.{#1}\right\rvert \IfNoValueTF{#2}{}{(#2)}}
	}
\DeclareDocumentCommand\eval{}{\evaluated} % Shorthand for evaluated
\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

% 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{ g o d() }{ % Gradient
	\IfNoValueTF{#1}{
		\IfNoValueTF{#2}{
			\IfNoValueTF{#3}{\vnabla}{\vnabla\!\quantity(#3)}
			}
			{\vnabla\!\quantity[#2] \IfNoValueTF{#3}{}{(#3)}}
		}
		{\vnabla #1 \IfNoValueTF{#2}{}{[#2]} \IfNoValueTF{#3}{}{(#3)}}
	}
\DeclareDocumentCommand\grad{}{\gradient} % Shorthand for \gradient

\DeclareDocumentCommand\divergence{ g o d() }{ % Divergence
	\IfNoValueTF{#1}{
		\IfNoValueTF{#2}{
			\IfNoValueTF{#3}{\vnabla \vdot}{\vnabla \vdot \quantity(#3)}
			}
			{\vnabla \vdot \quantity[#2] \IfNoValueTF{#3}{}{(#3)}}
		}
		{\vnabla \vdot #1 \IfNoValueTF{#2}{}{[#2]} \IfNoValueTF{#3}{}{(#3)}}
	}
\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{ g o d() }{ % Curl
	\IfNoValueTF{#1}{
		\IfNoValueTF{#2}{
			\IfNoValueTF{#3}{\vnabla \cross}{\vnabla \cross \quantity(#3)}
			}
			{\vnabla \cross \quantity[#2] \IfNoValueTF{#3}{}{(#3)}}
		}
		{\vnabla \cross #1 \IfNoValueTF{#2}{}{[#2]} \IfNoValueTF{#3}{}{(#3)}}
	}

\DeclareDocumentCommand\laplacian{ g o d() }{ % Laplacian
	\IfNoValueTF{#1}{
		\IfNoValueTF{#2}{
			\IfNoValueTF{#3}{\nabla^2}{\nabla^2\!\quantity(#3)}
			}
			{\nabla^2\!\quantity[#2] \IfNoValueTF{#3}{}{(#3)}}
		}
		{\nabla^2 #1 \IfNoValueTF{#2}{}{[#2]} \IfNoValueTF{#3}{}{(#3)}}
	}

% Operators
\let\sine\sin
\let\cosine\cos
\let\tangent\tan
\let\cosecant\csc
\let\secant\sec
\let\cotangent\cot
\let\arcsine\arcsin
\let\arccosine\arccos
\let\arctangent\arctan
\let\hypsine\sinh
\let\hypcosine\cosh
\let\hyptangent\tanh
\let\hypcotangent\coth
\DeclareDocumentCommand\sin{ o d() }{\IfNoValueTF{#2}{\sine \IfNoValueTF{#1}{}{[#1]}}{\sine \IfNoValueTF{#1}{}{^{#1}} \!\quantity(#2)}}
\DeclareDocumentCommand\cos{ o d() }{\IfNoValueTF{#2}{\cosine \IfNoValueTF{#1}{}{[#1]}}{\cosine \IfNoValueTF{#1}{}{^{#1}} \!\quantity(#2)}}
\DeclareDocumentCommand\tan{ o d() }{\IfNoValueTF{#2}{\tangent \IfNoValueTF{#1}{}{[#1]}}{\tangent \IfNoValueTF{#1}{}{^{#1}} \!\quantity(#2)}}
\DeclareDocumentCommand\csc{ o d() }{\IfNoValueTF{#2}{\cosecant \IfNoValueTF{#1}{}{[#1]}}{\cosecant \IfNoValueTF{#1}{}{^{#1}} \!\quantity(#2)}}
\DeclareDocumentCommand\sec{ o d() }{\IfNoValueTF{#2}{\secant \IfNoValueTF{#1}{}{[#1]}}{\secant \IfNoValueTF{#1}{}{^{#1}} \!\quantity(#2)}}
\DeclareDocumentCommand\cot{ o d() }{\IfNoValueTF{#2}{\cotangent \IfNoValueTF{#1}{}{[#1]}}{\cotangent \IfNoValueTF{#1}{}{^{#1}} \!\quantity(#2)}}
\DeclareDocumentCommand\arcsin{ o d() }{\IfNoValueTF{#2}{\arcsine \IfNoValueTF{#1}{}{[#1]}}{\arcsine \IfNoValueTF{#1}{}{^{#1}} \!\quantity(#2)}}
\DeclareDocumentCommand\arccos{ o d() }{\IfNoValueTF{#2}{\arccosine \IfNoValueTF{#1}{}{[#1]}}{\arccosine \IfNoValueTF{#1}{}{^{#1}} \!\quantity(#2)}}
\DeclareDocumentCommand\arctan{ o d() }{\IfNoValueTF{#2}{\arctangent \IfNoValueTF{#1}{}{[#1]}}{\arctangent \IfNoValueTF{#1}{}{^{#1}} \!\quantity(#2)}}
\DeclareDocumentCommand\sinh{ o d() }{\IfNoValueTF{#2}{\hypsine \IfNoValueTF{#1}{}{[#1]}}{\hypsine \IfNoValueTF{#1}{}{^{#1}} \!\quantity(#2)}}
\DeclareDocumentCommand\cosh{ o d() }{\IfNoValueTF{#2}{\hypcosine \IfNoValueTF{#1}{}{[#1]}}{\hypcosine \IfNoValueTF{#1}{}{^{#1}} \!\quantity(#2)}}
\DeclareDocumentCommand\tanh{ o d() }{\IfNoValueTF{#2}{\hyptangent \IfNoValueTF{#1}{}{[#1]}}{\hyptangent \IfNoValueTF{#1}{}{^{#1}} \!\quantity(#2)}}
\DeclareDocumentCommand\coth{ o d() }{\IfNoValueTF{#2}{\hypcotangent \IfNoValueTF{#1}{}{[#1]}}{\hypcotangent \IfNoValueTF{#1}{}{^{#1}} \!\quantity(#2)}}

\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\qassume{s}{\IfBooleanTF{#1}{}{\quad}\text{assume}\quad}
\DeclareDocumentCommand\qsince{s}{\IfBooleanTF{#1}{}{\quad}\text{since}\quad}
\DeclareDocumentCommand\qlet{s}{\IfBooleanTF{#1}{}{\quad}\text{let}\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{ o g d() }{ % Differential 'd'
	% o: optional n for nth differential
	% g: optional argument for readability and to control spacing
	% d: long-form as in d(cos x)
	\IfNoValueTF{#2}{
		\IfNoValueTF{#3}
			{\mathrm{d}\IfNoValueTF{#1}{}{^{#1}}}
			{\mathrm{d}\IfNoValueTF{#1}{}{^{#1}}\!\quantity(#3)}
		}
		{\mathrm{d}\IfNoValueTF{#1}{}{^{#1}} #2 \,}
	}
\DeclareDocumentCommand\dd{}{\differential} % Shorthand for \differential

\DeclareDocumentCommand\derivative{ o m g d() }{ % Total derivative
	% o: optional n for nth derivative
	% m: mandatory (x in df/dx)
	% g: optional (f in df/dx)
	% d: long-form d/dx(...)
	\IfNoValueTF{#3}{
		\IfNoValueTF{#4}
			{\frac{\dd \IfNoValueTF{#1}{}{^{#1}}}{\dd #2\IfNoValueTF{#1}{}{^{#1}}}}
			{\frac{\dd \IfNoValueTF{#1}{}{^{#1}}}{\dd #2\IfNoValueTF{#1}{}{^{#1}}} \!\quantity(#4)}
		}
		{\frac{\dd \IfNoValueTF{#1}{}{^{#1}} #2}{\dd #3\IfNoValueTF{#1}{}{^{#1}}}}
	}
\DeclareDocumentCommand\dv{}{\derivative} % Shorthand for \derivative
	
\DeclareDocumentCommand\partialderivative{ o m g g d() }{ % Partial derivative
	% o: optional n for nth derivative
	% m: mandatory (x in df/dx)
	% g: optional (f in df/dx)
	% g: optional (y in d^2f/dxdy)
	% d: long-form d/dx(...)
	\IfNoValueTF{#3}{
		\IfNoValueTF{#5}
			{\frac{\partial \IfNoValueTF{#1}{}{^{#1}}}{\partial #2\IfNoValueTF{#1}{}{^{#1}}}}
			{\frac{\partial \IfNoValueTF{#1}{}{^{#1}}}{\partial #2\IfNoValueTF{#1}{}{^{#1}}} \!\quantity(#5)}
		}{
		\IfNoValueTF{#4}
			{\frac{\partial \IfNoValueTF{#1}{}{^{#1}} #2}{\partial #3\IfNoValueTF{#1}{}{^{#1}}}}
			{\frac{\partial^2 #2}{\partial #3 \partial #4}}
		}
	}
\DeclareDocumentCommand\pderivative{}{\partialderivative} % Shorthand for \partialderivative
\DeclareDocumentCommand\pdv{}{\partialderivative} % Shorthand for \partialderivative

\DeclareDocumentCommand\variation{ o g d() }{ % Functional variation
	% o: optional n for nth differential
	% g: optional argument for readability and to control spacing
	% d: long-form as in d(F(g))
	\IfNoValueTF{#2}{
		\IfNoValueTF{#3}
			{\delta \IfNoValueTF{#1}{}{^{#1}}}
			{\delta \IfNoValueTF{#1}{}{^{#1}}\!\quantity(#3)}
		}
		{\delta \IfNoValueTF{#1}{}{^{#1}} #2 \,}
	}
\DeclareDocumentCommand\var{}{\variation} % Shorthand for \variation

\DeclareDocumentCommand\functionalderivative{ o m g d() }{ % Functional derivative
	% o: optional n for nth derivative
	% m: mandatory (g in dF/dg)
	% g: optional (F in dF/dg)
	% d: long-form d/dx(...)
	\IfNoValueTF{#3}{
		\IfNoValueTF{#4}
			{\frac{\variation \IfNoValueTF{#1}{}{^{#1}}}{\variation #2\IfNoValueTF{#1}{}{^{#1}}}}
			{\frac{\variation \IfNoValueTF{#1}{}{^{#1}}}{\variation #2\IfNoValueTF{#1}{}{^{#1}}} \!\quantity(#4)}
		}
		{\frac{\variation \IfNoValueTF{#1}{}{^{#1}} #2}{\variation #3\IfNoValueTF{#1}{}{^{#1}}}}
	}
\DeclareDocumentCommand\fderivative{}{\functionalderivative} % Shorthand for \functionalderivative
\DeclareDocumentCommand\fdv{}{\functionalderivative} % Shorthand for \functionalderivative

% 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

% Misc
\DeclareDocumentCommand\varE{}{\mathcal{E}} % Curly 'E'
\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.}
	}
}