\makeatletter

%% AMS
\usepackage{amsmath,amssymb,latexsym}

%% Theorems
\usepackage{xcolor}
\definecolor{darkgreen}{rgb}{0,0.45,0} 
\usepackage[pagebackref,colorlinks,citecolor=darkgreen,linkcolor=darkgreen]{hyperref}
\usepackage{cleveref,aliascnt}
\usepackage{amsthm}
\def\defthm#1#2#3{%
  %% Ensure all theorem types are numbered with the same counter
  \newaliascnt{#1}{thm}
  \newtheorem{#1}[#1]{#2}
  \aliascntresetthe{#1}
  %% Tell cleveref's \cref what to call things
  \crefname{#1}{#2}{#3}}
\newtheorem{thm}{Theorem}[section]
\crefname{thm}{Theorem}{Theorems}
\theoremstyle{definition}
\defthm{defn}{Definition}{Definitions}
%\theoremstyle{remark}
\defthm{rmk}{Remark}{Remarks}
\defthm{advrmk}{Advanced Remark}{Advanced Remarks}
\newenvironment{adv}{\small\begin{advrmk}}{\end{advrmk}}
\defthm{eg}{Example}{Examples}
\defthm{egs}{Examples}{Examples}
\defthm{sage}{Sage}{Sages}

%% Reference format for sections
\crefformat{section}{\S#2#1#3}
\Crefformat{section}{Section~#2#1#3}
\crefrangeformat{section}{\S\S#3#1#4--#5#2#6}
\Crefrangeformat{section}{Sections~#3#1#4--#5#2#6}
\crefmultiformat{section}{\S\S#2#1#3}{ and~#2#1#3}{, #2#1#3}{ and~#2#1#3}
\Crefmultiformat{section}{Sections~#2#1#3}{ and~#2#1#3}{, #2#1#3}{ and~#2#1#3}
\crefrangemultiformat{section}{\S\S#3#1#4--#5#2#6}{ and~#3#1#4--#5#2#6}{, #3#1#4--#5#2#6}{ and~#3#1#4--#5#2#6}
\Crefrangemultiformat{section}{Sections~#3#1#4--#5#2#6}{ and~#3#1#4--#5#2#6}{, #3#1#4--#5#2#6}{ and~#3#1#4--#5#2#6}

%% Use the theorem counter for equations as well
\let\c@equation\c@thm
\numberwithin{equation}{section}

%% Pictures
\usepackage{tikz}
\usepackage{graphicx}

%% Differentials
\newcommand{\dd}{\ensuremath{\mathrm{d}}}
\newcommand{\ed}{\ensuremath{\mathrm{d}\wedge}}

%% Differentials of variables
\newcommand{\dx}{{\ensuremath{\dd x}}}
\newcommand{\dy}{{\ensuremath{\dd y}}}
\newcommand{\dz}{{\ensuremath{\dd z}}}
\newcommand{\dzbar}{{\ensuremath{\dd \bar{z}}}}
\newcommand{\dt}{{\ensuremath{\dd t}}}
\newcommand{\du}{{\ensuremath{\dd u}}}
\newcommand{\dv}{{\ensuremath{\dd v}}}
\newcommand{\df}{{\ensuremath{\dd f}}}
\newcommand{\dg}{{\ensuremath{\dd g}}}
\newcommand{\dr}{{\ensuremath{\dd r}}}
\newcommand{\drho}{{\ensuremath{\dd \rho}}}
\newcommand{\dtheta}{{\ensuremath{\dd \theta}}}
\newcommand{\dphi}{{\ensuremath{\dd \phi}}}
\newcommand{\dpx}{{\ensuremath{\dd \pt{x}}}}

%% Not differentials of anything
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\newcommand{\dl}{\ensuremath{\mathrm{\dj}\ell}}
\newcommand{\dA}{\ensuremath{\mathrm{\dj}A}}
\newcommand{\dV}{\ensuremath{\mathrm{\dj}V}}
\newcommand{\dn}{\ensuremath{\mathrm{\dj}\vc{n}}}

% Apparently one of those packages redefines this
\renewcommand{\dh}{{\ensuremath{\dd h}}}

%% Wedge product
%\newcommand{\wedge}{\wedge}

%% Partial derivatives
% \newcommand{\pder}[2]{\frac{\partial #1}{\partial #2}}
% \newcommand{\pdertwo}[2]{\frac{\partial^2 #1}{\partial #2^2}}
% \newcommand{\pdermixed}[3]{\frac{\partial^2 #1}{\partial #2 \,\partial #3}}
\newcommand{\pder}[2]{{#1}_{#2}}
\newcommand{\pdertwo}[2]{{#1}_{{#2}{#2}}}
\newcommand{\pdermixed}[3]{{#1}_{{#2}{#3}}}

%% Transposing vectors into forms
\newcommand{\tr}[1]{{#1}^\top}
\newcommand{\ptr}[1]{{(#1)}^\top}

%% Hodge star on forms
\newcommand{\str}[1]{{#1}^*}
\newcommand{\pstr}[1]{{(#1)}^*}

%% Points and vectors
\newcommand{\pt}[1]{\mathbf{#1}}
\newcommand{\ptc}[1]{(#1)}
\newcommand{\ptx}{\pt{x}}
% \newcommand{\vc}[1]{\vec{#1}}
\newcommand{\vc}[1]{\mathbf{#1}}
\newcommand{\vcc}[1]{(#1)}
\newcommand{\lrvcc}[1]{\left(#1\right)}

%% Vector operations
\newcommand{\mgn}[1]{\Vert #1 \Vert}
%\newcommand{\dotp}[2]{\langle #1 , #2 \rangle}
\newcommand{\dotp}[2]{#1 \cdot #2}
\newcommand{\crossp}[2]{#1 \times #2}

%% Vector calculus operations
\newcommand{\grad}[1]{\nabla #1}
\renewcommand{\div}[1]{\nabla \cdot #1}
\newcommand{\curl}[1]{\nabla \times #1}

%% Fractions
\newcommand{\half}{\ensuremath{\textstyle\frac{1}{2}}}
\newcommand{\halfi}{\ensuremath{\textstyle\frac{1}{2i}}}
\newcommand{\halfpii}{\ensuremath{\textstyle\frac{1}{2\pi i}}}
\newcommand{\third}{\ensuremath{\textstyle\frac{1}{3}}}

%% Orders
\renewcommand{\th}{^{\mathrm{th}}}
\newcommand{\st}{^{\mathrm{st}}}

%% Approximation to specified order
\newcommand{\apx}[1]{\mathrel{\overset{#1}{\approx}}}

%% Riemann sums
\newcommand{\rstag}[2]{{#1}_{#2}^*}

%% Complex conjugation
\newcommand{\conj}[1]{\bar{#1}}
\newcommand{\zbar}{\conj{z}}
\newcommand{\wbar}{\conj{w}}

%% Parametrized curves
\newcommand{\curve}{C}
\newcommand{\curvept}{\mathbf{x}}
\newcommand{\dcurvept}{\dd\mathbf{x}}
\newcommand{\curvex}{x}
\newcommand{\curvey}{y}
\newcommand{\curvez}{z}

%% Regions
\newcommand{\region}{R}
\newcommand{\bdry}{\partial}

%% Parametrized surfaces
\newcommand{\surf}{S}
\newcommand{\surfx}{x}
\newcommand{\surfy}{y}
\newcommand{\surfz}{z}

%% Integrals of differential forms.  The only argument is the
%% integration manifold; the differential form just comes afterwards.
\newcommand{\lint}[1]{\int_{#1}} % line integral over a curve
\newcommand{\sint}[1]{\iint_{#1}} % surface integral form over a surface
\newcommand{\vint}[1]{\iiint_{#1}} % volume integral over a spatial region

%% "Classical" integrals.  For these the integrand is a *function*
%% (well, technically, an expression with free variables) which is
%% given as a second argument.
\newcommand{\dint}[2]{\iint_{#1} #2 \,\dA} % double integral over a plane region
\newcommand{\tint}[2]{\iiint_{#1} #2 \,\dV} % triple integral over a spatial region
\newcommand{\itint}[4]{\int_{#2}^{#3} #4 \,\dd #1} % ordinary one-variable integral over an interval
\newcommand{\itdint}[7]{\int_{#2}^{#3} \int_{#5}^{#6} #7 \,\dd #4\,\dd #1} % double iterated integral
% Can't have more than 9 parameters
%\newcommand{\ittint}[10]{\int_{#2}^{#3} \int_{#5}^{#6} \int_{#8}^{#9} #10 \,\dd #7\,\dd #4\,\dd #1} % triple iterated integral
\makeatletter
\newcommand{\ittint}[3]{\int_{#2}^{#3} \@ittint #1}
\newcommand{\@ittint}[8]{\int_{#3}^{#4} \int_{#6}^{#7} #8 \,\dd #5\,\dd #2\,\dd #1} % triple iterated integral
\makeatother

%% Textbooks
\usepackage{version}
\includeversion{notextbook}
\excludeversion{stewart}
\excludeversion{hugheshallett}
\excludeversion{rogawski}

\makeatother
