\ProvidesPackage{calculus}
\RequirePackage{amsmath,amssymb,amsopn,amsthm}
\RequirePackage{IEEEtrantools}
%\RequirePackage{fancybox}
\RequirePackage{suffix}%To define starred commands
\RequirePackage{enumerate,multicol,verbatim,xspace,centernot}
\RequirePackage{mathtools,forloop,paralist}
\RequirePackage[pdfborder={0 0 .1}]{hyperref}% put this last...
\RequirePackage[initials,nobysame]{amsrefs}% Load after hyperref

\newif\iflecturesinline
\DeclareOption{inlineLectures}{\lecturesinlinetrue}
\ProcessOptions

% \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{\eps}{\varepsilon}
\newcommand{\suchthat}{\backepsilon}
\newcommand{\st}{\iftextstyle\;|\;\else\;\big|\;\fi}
\newcommand{\grad}{\nabla}
\newcommand{\divergence}{\nabla\cdot}
\newcommand{\curl}{\nabla\times}
\newcommand{\del}{\partial}
\newcommand{\delx}{\del_x}
\newcommand{\dely}{\del_y}
\newcommand{\delz}{\del_z}
\newcommand{\delt}{\del_t}
\newcommand{\degrees}{^\circ}
\newcommand{\lap}{\triangle}
\newcommand{\cnot}{\centernot}
\newcommand{\Chi}[1]{\Chi*{\{#1\}}}
\WithSuffix\newcommand\Chi*[1]{\chi_{\raise-.5ex\hbox{$\scriptstyle#1$}}}
\newcommand{\defeq}{\stackrel{\text{def}}{=}}
\newcommand{\as}{\ifmmode \text{a.s.} \else a.s.\xspace\fi}
\newcommand{\varmin}{\wedge}
\newcommand{\varmax}{\vee}
\newcommand{\given}{\,\iftextstyle|\else\big|\fi\,}
\newcommand{\decreasesto}{\searrow}
\newcommand{\increasesto}{\nearrow}
\newcommand{\weaklyto}{\xrightarrow{\text{w}}}
\newcommand{\cross}{\times}
\newcommand{\closure}[1]{\overline{#1}}
\newcommand{\interior}[1]{\mathring{#1}}
\newcommand{\union}{\bigcup}
\newcommand{\intersection}{\bigcap}
\newcommand{\intersect}{\intersection}

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

%
% Norms
%
\DeclarePairedDelimiter\abs{\lvert}{\rvert}
\DeclarePairedDelimiter\norm{\lVert}{\rVert}
%\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}

\DeclarePairedDelimiter\qv{\langle}{\rangle}
\newcommand\jqv[2]{\qv{#1,#2}}
\newcommand{\F}{\mathcal F}
\newcommand{\mc}{\mathcal}

% \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{\comp}{^\text{c}}

\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{\re}{Re}
\DeclareMathOperator{\rank}{rank}
\DeclareMathOperator{\Span}{span}
\DeclareMathOperator{\Ad}{adj}
\DeclareMathOperator{\mat}{Mat}
\DeclareMathOperator{\ann}{ann}
\DeclareMathOperator{\trace}{tr}
\DeclareMathOperator{\co}{co}
\newcommand{\coc}{\overline{\co}}
\DeclareMathOperator{\erf}{erf}
\DeclareMathOperator{\sign}{sign}
\DeclareMathOperator{\supp}{supp}
\DeclareMathOperator{\esssup}{ess\,sup}

% 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}}
\newcommand{\dmax}{\max\limits}
\newcommand{\dmin}{\min\limits}

\newcommand{\pts}[2][\@empty]{\textit{\tiny (#2 pts\ifx\@empty#1\else, #1\fi)}}

% 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}
\newcommand{\scolvec}[1]{\left(\begin{smallmatrix}#1\end{smallmatrix}\right)}

% Matrices
\renewcommand*\env@matrix[1][*\c@MaxMatrixCols c]{%
    \hskip -\arraycolsep
    \let\@ifnextchar\new@ifnextchar
    \array{#1}%
}

% Misc
\newcommand{\per}{_\text{per}}


% 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\textstylefalse}
\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{\itemopt}{\item[\textasteriskcentered]}

\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
\newif\ifmultipleassignmentsperpage
\multipleassignmentsperpagetrue
\newenvironment{assignment}[2]{%
    \setcounter{hwpart}{0}\addtocounter{hwno}{1}\vspace{\bigskipamount}%
    \noindent{\large \bf Assignment~\arabic{hwno}:} Assigned {#1}. Due {#2}
    %\begin{enumerate}[\hspace{2ex}1.]\setlength{\itemsep}{0pt}
    \begin{questions}\setlength{\itemsep}{0pt}
}{%
    \end{questions}
    %\end{enumerate}
    \ifmultipleassignmentsperpage\else\newpage\fi
}
\newcounter{hwno}
\newcounter{hwpart}
\newcommand{\hwpart}[1][Part]{
    \addtocounter{hwpart}{1}\uplevel{\noindent\textsc{#1 \Roman{hwpart}:}}}

% Leave spaces (for multipage exams)
% \newcommand{\LeaveSpaces}{1}
% \newcommand{\ifs}[1]{\ifnum\LeaveSpaces=0{}\else{#1}\fi}
\newif\ifspaces
\spacesfalse
\newcommand{\solbreak}{\ifspaces\newpage\fi}
\newcommand{\solspace}[1]{\ifspaces\vspace{\stretch{#1}}\fi}
\newcommand{\blankpage}{\ifspaces\fullwidth{\noindent\centering This page has been left blank for your work.}\newpage\fi}

\newcommand{\mygradetable}{%
    \small
    \newlength{\extrarowheight}
    \setlength{\extrarowheight}{1ex}
    \newlength{\extrarowdepth}
    \setlength{\extrarowdepth}{1.5ex}%

    \addtolength{\extrarowheight}{\baselineskip}
    \newcommand{\Startrow}{\rule{0pt}{\extrarowheight}}
    \newcommand{\Endrow}{\\[\extrarowdepth]}
    \begin{tabular}[c]{|c|c|}
	\hline
	\Startrow Question & Points\Endrow
	\hline
	\newcounter{i}\forloop{i}{1}{\value{i}<\numquestions \OR \value{i}=\numquestions}{\Startrow \arabic{i} & \Endrow\hline}
	\Startrow Total & \\
	\relax[\numpoints] & \Endrow
	\hline
    \end{tabular}
}



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

% Titles and date
\renewcommand{\title}[1]{\def\doctitle{#1}}
\newcommand{\subtitle}[1]{\def\docsubtitle{#1}}
\renewcommand{\date}[1]{\def\docdate{#1}}
\let\docdate\@empty
\let\docsubtitle\@empty
\renewcommand{\maketitle}{%
\begin{center}
{\Large\bf\doctitle}%
\markboth{\hspace{4ex}\doctitle\hfill}{\hfill\doctitle\hspace{4ex}}%
\ifx\@empty\docsubtitle\else%
\medskip\\
{\large\docsubtitle}%
\fi
\ifx\@empty\docdate\else%
\smallskip\\
{\small\docdate}%
\fi%
\bigskip%
\end{center}
}

% Environment for questions and subparts.
\newcounter{exampartno}\setcounter{exampartno}{1}
\newcommand{\exampart}[1]{\fullwidth{\Large\bf Part \arabic{exampartno}: #1}\addtocounter{exampartno}{1}}

% 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}
}

\@ifundefined{questionshook}{\newcommand{\questionshook}{}}{}
\@ifundefined{partshook}{\newcommand{\partshook}{}}{}
\@ifundefined{subpartshook}{\newcommand{\subpartshook}{}}{}
\@ifundefined{subsubpartshook}{\newcommand{\subsubpartshook}{}}{}
\renewcommand{\questionshook}{\setlength{\parindent}{2ex}}
\renewcommand{\partshook}{\setlength{\parindent}{2ex}}

% Environment for solutions. (do \solfalse to omit solutions)
\newif\ifsol
\newenvironment{qn}{\ifsol\itshape\fi}{\ifsol\normalfont\fi}
\newenvironment{sol}[1][Solution]{%
    \begin{proof}[#1]%
    \setlength{\parindent}{2ex}
}{%
    \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}
\newtheorem{claim}[theorem]{Claim}

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

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

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

%
% Macros for writing lecture schedule.
%
\newcounter{lecture}
\setcounter{lecture}{0}
%\newcommand{\TheLecture}[1]{\textit{L\rlap{\arabic{lecture},}\phantom{88,} #1.}\hspace{2ex}}
\newcommand{\topicbullet}{\textbullet\hspace{1ex}}
\newcommand{\subtopicbullet}{--\hspace{1ex}}
\newcommand{\subsubtopicbullet}{\textasteriskcentered\hspace{1ex}}


\newlength{\topicindent}
\settowidth{\topicindent}{\hbox{\topicbullet}}
\newlength{\topicsmargin}
\iflecturesinline
    \setlength{\topicsmargin}{\topicindent}
\else
    \settowidth{\topicsmargin}{\textit{L88, Mon.\ 88/88.}}
\fi
\addtolength{\topicsmargin}{.25cm}

\newlength{\subtopicsmargin}%For indenting subtopic lists.
\setlength{\subtopicsmargin}{\topicsmargin}
\addtolength{\subtopicsmargin}{\topicindent}

\newlength{\subtopicindent}%For indenting subsubtopic lists.
\settowidth{\subtopicindent}{\hbox{\subtopicbullet}}
\newlength{\subsubtopicsmargin}
\setlength{\subsubtopicsmargin}{\subtopicsmargin}
\addtolength{\subsubtopicsmargin}{\subtopicindent}

\newlength{\marginlength}

\newif\ifshowlecture
\newif\ifsubtopics
\newif\ifsubsubtopics
\showlecturefalse\subtopicsfalse\subsubtopicsfalse

\makeatletter
\newcommand{\TheLecture}{\ignorespaces
    \iflecturesinline
	\textbf{Lecture \arabic{lecture}, \lecturetext\ifx\lecturetitle\@empty.\else: \lecturetitle.\fi}
    \else
	\ifsubsubtopics
	    \setlength{\marginlength}\subsubtopicsmargin
	\else\ifsubtopics
	    \setlength{\marginlength}\subtopicsmargin
	\else
	    \setlength{\marginlength}\topicsmargin
	\fi\fi
	\makebox[\marginlength][l]{\hspace{.25cm}\textit{L\arabic{lecture}, \lecturetext.}}
    \fi
}
\newcommand{\lecture}[2][\@empty]{\global\showlecturetrue\gdef\lecturetext{#2}\global\edef\lecturetitle{#1}}
\makeatother

\newcommand{\bulletsymbol}{\ifsubsubtopics\subsubtopicbullet\else\ifsubtopics\subtopicbullet\else\topicbullet\fi\fi}
\newcommand{\topic}{%
    \ifsubsubtopics\end{subsubtopics}\fi
    \ifsubtopics\end{subtopics}\fi
    \showtopic}
\newcommand{\subtopic}{%
    \ifsubsubtopics\end{subsubtopics}\fi
    \ifsubtopics\else\begin{subtopics}\fi
    \showtopic
}
\newcommand{\subsubtopic}{%
    \ifsubtopics\else
	\typeout{subsubtopics used with subtopics}
	%XXX Raise an error and stop
    \fi
    \ifsubsubtopics\else\begin{subsubtopics}\fi
    \showtopic
}
\newcommand{\showtopic}{
    \ifshowlecture
	\global\showlecturefalse\addtocounter{lecture}{1}
	\iflecturesinline
	    \fullwidth{\TheLecture}
	    \item
	\else
	    \item[\llap{\TheLecture}\bulletsymbol]
	\fi
    \else
	\item[\bulletsymbol]
    \fi}

\newenvironment{topics}{%
    \begin{list}{\topicbullet}{\leftmargin=\topicsmargin\labelwidth=\topicindent\labelsep=0ex\itemsep=0pt\topsep=0pt}
}{%
    \ifsubsubtopics\end{subsubtopics}\fi
    \ifsubtopics\end{subtopics}\fi
    \end{list}
}
\newenvironment{subtopics}{
    \begin{list}{\subtopicbullet}{\leftmargin=\topicindent\labelsep=0ex\topsep=0pt}\subtopicstrue%
}{%
    \end{list}\subtopicsfalse
}
\newenvironment{subsubtopics}{%
    \begin{list}{\subsubtopicbullet}{\leftmargin=\subtopicindent\labelsep=0ex\topsep=0pt}\subsubtopicstrue
}{%
    \end{list}\subsubtopicsfalse
}
