\ProvidesPackage{calculus}
\RequirePackage{amsmath,amssymb,amsopn,amsthm}
\RequirePackage{enumerate,multicol,verbatim,xspace}
% \RequirePackage[dvipdfm]{color}

%
% Test if we're in a display or not
%
\newif\iftextstyle
\textstyletrue
\everydisplay\expandafter{\the\everydisplay\textstylefalse}

% Commonly used symbols
\renewcommand{\epsilon}{\varepsilon}
\newcommand{\suchthat}{\backepsilon}
\newcommand{\st}{\;|\;}
\newcommand{\grad}{\nabla}
\newcommand{\del}{\partial}
\newcommand{\degrees}{^\circ}
\newcommand{\lap}{\triangle}

\newif\iftextstyle
\textstyletrue
\everydisplay\expandafter{\the\everydisplay\textstylefalse}

%
% Norms
%
\newcommand{\abs}[1]{{\iftextstyle\lvert#1\rvert\else\left\lvert#1\right\rvert\fi}}
\newcommand{\norm}[1]{{\iftextstyle\lVert#1\rVert\else\left\lVert#1\right\rVert\fi}}
\newcommand{\lpspace}[1]{\ell^{#1}_{\vphantom{h}}}
%\newcommand{\lpspace}[1]{\ell^{\raise1pt\mbox{$#1$}}}
%\newcommand{\lpspace}[1]{\ell^{#1}}
\newcommand{\lpnorm}[2]{\norm{#1}_{\lpspace{#2}}}
%\newcommand{\lpnorm}[2]{\norm{#1}_{\ell^{\raise.35ex\hbox{$\scriptscriptstyle#2$}}}}
\newcommand{\Lpspace}[1]{L^{#1}}
\newcommand{\Lpnorm}[2]{\norm{#1}_{\Lpspace{#2}}}
\newcommand{\holdernorm}[2]{\norm{#1}_{C^#2}}
\newcommand{\ip}[2]{\iftextstyle\langle\else\left\langle\fi #1, #2 \iftextstyle\rangle\else\right\rangle\fi}

% \providecommand{\implies}{\Rightarrow}
\renewcommand{\leq}{\leqslant}
\renewcommand{\geq}{\geqslant}
\renewcommand{\subset}{\subseteq}
\renewcommand{\supset}{\supseteq}
\newcommand{\transpose}{^\text{t}}
\newcommand{\frowny}{\ensuremath{\ddot{\frown}}}
\newcommand{\smiley}{\ensuremath{\ddot{\smile}}}
\newcommand{\St}{^\text{st}}
\newcommand{\Nd}{^\text{nd}}
\newcommand{\Rd}{^\text{rd}}
\newcommand{\Th}{^\text{th}}

\newcommand{\xaxis}{$x$-axis}
\newcommand{\yaxis}{$y$-axis}
\newcommand{\zaxis}{$z$-axis}

\newcommand{\prop}[1]{\noindent\textbf{#1:}}
\providecommand{\qed}{\raisebox{-1em}{QED}}

% Commonly used operators
\DeclareMathOperator{\cosec}{cosec}
\DeclareMathOperator{\cosech}{cosech}
\DeclareMathOperator{\sech}{sech}
\DeclareMathOperator{\var}{Var}
\DeclareMathOperator{\im}{im}
\DeclareMathOperator{\rank}{rank}
\DeclareMathOperator{\Span}{span}
\DeclareMathOperator{\Ad}{adj}
\DeclareMathOperator{\trace}{tr}
\DeclareMathOperator{\co}{co}
\newcommand{\coc}{\overline{\co}}

% Commonly used sets
\newcommand{\R}{\mathbb{R}}
\newcommand{\C}{\mathbb{C}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\Q}{\mathbb{Q}}

% Commonly used constructs
\newcommand{\ed}{$\epsilon$-$\delta$\xspace}
\newcommand{\inv}{^{-1}}
\newcommand{\dlim}{\lim\limits}%display style limits
\newcommand{\dliminf}{\liminf\limits}
\newcommand{\dlimsup}{\limsup\limits}
\newcommand{\dlimto}[2][x]{\dlim_{#1 \to #2}}
\newcommand{\fracpart}[1]{\boldsymbol{\{}#1\boldsymbol{\}}}
\newcommand{\intpart}[1]{\boldsymbol{[}#1\boldsymbol{]}}
\newcommand{\Perp}{^\perp}
\newcommand{\hatsym}{^{\wedge}}

% Vectors
\newcommand{\bd}{\boldsymbol}
\newcommand{\bdu}{\bd{u}}
\newcommand{\bdv}{\bd{v}}
\newcommand{\proj}[1]{P_{\bd{#1}}}
\newcommand{\vece}{\vec{e}}
\newcommand{\vecf}{\vec{f}}
\newcommand{\vecu}{\vec{u}}
\newcommand{\vecv}{\vec{v}}
\newcommand{\vecx}{\vec{x}}
\newcommand{\vecy}{\vec{y}}
\newcommand{\colvec}[1]{\iftextstyle\bigl(\begin{smallmatrix}#1\end{smallmatrix}\bigr)\else\begin{pmatrix*}#1\end{pmatrix*}\fi}

% Outdated limit constructs.
\newcommand{\limxtoa}{\dlim_{x \to a}}
\newcommand{\limxtoal}{\dlim_{x \to a^-}}
\newcommand{\limxtoar}{\dlim_{x \to a^+}}
\newcommand{\limsupxtoal}{\dlimsup_{x \to a^-}}
\newcommand{\limsupxtoar}{\dlimsup_{x \to a^+}}
\newcommand{\liminfxtoal}{\dliminf_{x \to a^-}}
\newcommand{\liminfxtoar}{\dliminf_{x \to a^+}}
\newcommand{\ds}{\displaystyle}
\newcommand{\limto}{\dlimto}

% Hints and notes
\newcommand{\hint}[2][Hint]{{\footnotesize {\sc #1: }#2}}
\newcommand{\hintb}[2][Hint]{{\footnotesize [{\sc #1: }#2]}}
\newcommand{\note}[1]{{\footnotesize #1}}
\newcommand{\noteb}[1]{{\footnotesize [#1]}}

% Starred items etc. TODO: Make code better! \ifnum \@enumdepth or something
\newcommand{\itemb}{
\stepcounter{enumi}
\item[$\bullet\;$\arabic{enumi}.]}

\newcommand{\sitemb}{
\stepcounter{enumii}
\item[$\bullet\;$(\alph{enumii})]}

\newcommand{\itembb}{
\stepcounter{enumi}
\item[$\bullet\bullet\;$\arabic{enumi}.]}

\newcommand{\itemsb}{
\stepcounter{enumi}
\item[$\star\bullet\;$\arabic{enumi}.]}

\newcommand{\items}{
\stepcounter{enumi}
\item[$\star\;$\arabic{enumi}.]}

\newcommand{\sitems}{
\stepcounter{enumii}
\item[$\star\;$(\alph{enumii})]}

\newcommand{\sitemss}{
\stepcounter{enumii}
\item[$\star\star\;$(\alph{enumii})]}

\newcommand{\itemss}{
\stepcounter{enumi}
\item[$\star\star\;$\arabic{enumi}.]}

\newcommand{\itemsss}{
\stepcounter{enumi}
\item[$\star\star\star\;$\arabic{enumi}.]}

% Environment for Assignments (multiple assignments per page)
\newenvironment{assignment}[2]{%
    \addtocounter{hwno}{1}\vspace{\bigskipamount}%
    \noindent{\large \bf Assignment~\arabic{hwno}:} Assigned {#1}. Due {#2}
    \begin{enumerate}[\hspace{2ex}1.]
}{%
    \end{enumerate}
}
\newcounter{hwno}

% Leave spaces (for multipage exams)
% \newcommand{\LeaveSpaces}{1}
% \newcommand{\ifs}[1]{\ifnum\LeaveSpaces=0{}\else{#1}\fi}
\newif\ifspaces
\spacesfalse

\newcommand{\ColorChanges}{1}
\newcommand{\change}[1]{\ifnum\ColorChanges=1{\textcolor{red}{#1}}\else{#1}\fi}

% Titles and date
\renewcommand{\title}[1]{\def\doctitle{#1}}
\renewcommand{\date}[1]{\def\docdate{#1}}
\let\docdate\@empty
\renewcommand{\maketitle}{%
\begin{center}
{\LARGE\bf\doctitle}%
\ifx\@empty\docdate\else%
\smallskip\\
{\small\docdate}%
\fi%
\bigskip%
\end{center}
}

% Environment for questions and subparts.

% Only define an environment if it's not defined before
\newcommand\provenvironment[3]{
\@ifundefined{#1}
    {\newenvironment{#1}{#2}{#3}}
    {}
}

\newenvironment{mcparts}[1][2]{%
    \vspace{-1em}\begin{multicols}{#1}\begin{parts}
}{%
    \end{parts}\end{multicols}\vspace{-1em}%
}

\provenvironment{parts}{
    \begin{enumerate}
    \setlength{\itemsep}{\smallskipamount}
}{
    \end{enumerate}
}

\provenvironment{questions}{
    \begin{enumerate}[\hspace{-3ex}1.]
    \setlength{\itemsep}{\medskipamount}
}{
    \end{enumerate}
}

% Environment for solutions. (do \solfalse to omit solutions)
\newif\ifsol
\newenvironment{sol}[1][Solution]{%
    \begin{proof}[#1]
}{%
    \end{proof}
}

% \newenvironment{sol}[1][Solution]{
%     \setlength{\parskip}{\smallskipamount}
%     \setlength{\parindent}{3ex}
%     \par
%     \noindent\textbf{#1:}
% }{%
%     \medskip%
% }

% uplevel taken from examclass defn
% \long\def\uplevel#1{%
\providecommand{\uplevel}[1]{
  \par\medskip
  \vbox{%
 {}   \advance\leftskip\@totalleftmargin
    \advance\leftskip-\leftmargin
    #1\par
  }% vbox
  \nobreak
}

\newenvironment{eqngrp}{%
    \left.\begin{aligned}
}{%
    \end{aligned}\right\}
}

%
% Theorems etc (for use in handouts)
%
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{corollary}[theorem]{Corollary}

% Unnumbered theorem / prop
\newtheorem*{theorem*}{Theorem}
\newtheorem*{proposition*}{Proposition}

\theoremstyle{definition}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{example}[theorem]{Example}

\theoremstyle{remark}
\newtheorem{remark}[theorem]{Remark}
\newtheorem*{remark*}{Remark}
