%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%  This package provides the exam format for MA109 and MA123
%%  See exam1.tex for standard usage.
%%  Actual exam questions are in exam1-sa.tex and exam1-mc.tex (since they are usually generated)
%%
%%  If you need to edit the front page, scroll down a page or so and you
%%  should see the text.

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{UKMathExam}[2019/09/09 Jack Schmidt]
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\ProcessOptions\relax
\LoadClass[12pt]{article}

\RequirePackage{expl3}
\RequirePackage[log-declarations=false]{xparse}
\RequirePackage{etoolbox}
\RequirePackage{tikz} \usetikzlibrary{shadows}
\RequirePackage{geometry} \geometry{left=0.5in,right=0.5in,top=0.75in,bottom=0.75in}
\RequirePackage{amsmath}
\RequirePackage{enumerate}
%\RequirePackage{calc}
\RequirePackage{blkarray,bigstrut}

% Needed for grader page
\RequirePackage{fontspec}
\RequirePackage{multirow}
\def\sillyfont{Comic Sans MS}

% Accessibility
\RequirePackage{unicode-math}
\RequirePackage{microtype}

% Needed for some questions
\RequirePackage{pgfplots}
\pgfplotsset{compat=1.16,compat/show suggested version=false}

\RequirePackage{NumberLine}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%  \FrontPage
%%
%%  Typeset the front page. Name/Section blanks, Answer blanks, Grading blanks,
%%  and basic instructions

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%  The front page header: a thin line, Class information, Exam information,
%%  Name/Section blank
%%

\DeclareDocumentCommand \FrontPageHeader {} {

\DividerLine \medskip

\noindent%
\begin{tabular}{ll}
\textbf{\LARGE\CourseNumber} & \Semester \\
\multicolumn{2}{c}{\CourseName} \\
\end{tabular}
\hfill
\begin{tabular}{l}
\textbf{\ExamName} \\
\ExamDate \\
\end{tabular}
\hfill
\textbf{Name:} \underline{\hspace{4.5cm}} 
\textbf{Sec.:} \underline{\hspace{1.5cm}}\hspace{0.13cm}
\newline

\hfill
\textbf{Student ID \#:} \textbf{9}\underline{\hspace{4.5cm}} 
\vspace{-0.175cm}

\DividerLine \smallskip

}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%  The instructions at the top of the front page. Mostly just plain text.
%%  Notice the clever version marker.
%%

\DeclareDocumentCommand \FrontPageInstructions {} {
\noindent Do not remove this answer page --- you will turn in the entire exam.
You have two hours to do this exam. No books or notes may be used. You may use
an ACT-approved calculator during the exam, but NO calculator with a Computer
Algebra System (CAS), networking, or camera is permitted. Absolutely no cell
phone use during the exam is allowed. \smallskip 

\if@ShortAnswer%
\noindent The exam consists of 2 short answer questions and
\NumberOfMultipleChoice\space multiple choice questions. Answer the short answer
questions on the last two pages, and record your answers to the multiple
choice questions on this page.
\else%
\noindent The exam consists of \NumberOfMultipleChoice\space multiple choice
questions. Record your answers on this page.
\fi%
For each multiple choice question, you will need to fill in the circle
corresponding to the correct answer. For example, if (\ExamVersion)
is correct, you must shade \\
\vspace*{-0.5ex}\centerline{ \answerbox{\ExamVersion} }\vspace{1ex}

\noindent It is your responsibility to make it CLEAR which response has been
chosen. You will not get credit unless the correct answer has been clearly
marked on this page.

\begin{center} \bf GOOD LUCK! \end{center}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%  The grading boxes "out of 20" or whatever.
%%

%\DeclareDocumentCommand \ForGradingUse {} {%
%\begin{center}
%{\bf For grading use:}   \vskip .2in
%
%\if@ShortAnswer
%\if@KeyGrader
%\shadowbox{
%\begin{tabular}{cc|c}
%\multicolumn{2}{c|}{\textbf{Multiple Choice}}  & \textbf{Short Answer} \\
%\multirow[c]{2}{*}{ \fontspec{\sillyfont} \rotatebox{90}{\scriptsize max} \LARGE \NumberOfMultipleChoice } &
%\multirow[c]{2}{*}{ \fontspec{\sillyfont} \rotatebox{90}{\scriptsize max} \LARGE \MultipleChoicePoints } &
%\fontspec{\sillyfont} Leave \\
% & & \fontspec{\sillyfont} Blank \\ \hline
%\textbf{\tiny(number right)} & \textbf{\tiny(\PointsPerMultipleChoice\space points each)} & \textbf{\tiny (out of \ShortAnswerPoints\space points)}
%\end{tabular}
%}
%\else
%\shadowbox{
%\begin{tabular}{cc|c}
%\multicolumn{2}{c|}{\textbf{Multiple Choice}}  & \textbf{Short Answer} \\
% & & \\ 
% & & \\ \hline
%\textbf{\tiny(number right)} & \textbf{\tiny(\PointsPerMultipleChoice\space points each)} & \textbf{\tiny (out of \ShortAnswerPoints\space points)}
%\end{tabular}
%}
%\fi
%\else
%\shadowbox{
%\begin{tabular}{c|c} \\
%\textbf{Number}  & \\
%\textbf{Correct} & \\ \hline
%                 & \textbf{(out of \NumberOfMultipleChoice\space problems)}
%\end{tabular} }
%\fi% Both
%\hspace{0.75in}
%\if@KeyGrader
%\shadowbox{
%\begin{tabular}{c|c} \\
%\textbf{Total}	& \fontspec{\sillyfont} Leave \\
%                & \fontspec{\sillyfont} Blank \\ \hline
%		& \textbf{(out of \TotalNumberOfPoints\space points)}
%\end{tabular}
%}
%\else
%\shadowbox{
%\begin{tabular}{c|c} \\
%\textbf{Total}	&       \\
%		&       \\ \hline
%		& \textbf{(out of \TotalNumberOfPoints\space points)}
%\end{tabular}
%}
%\fi
%\end{center}
%
%\DividerLine
%
%}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%  The front page skeleton. Mostly just uses the previous things to fill up
%%  the front page.

\DeclareDocumentCommand \FrontPage { m } {\bgroup\ExamSetup{#1}%
\FrontPageHeader
\FrontPageInstructions \vfill
\AnswerBoxes \vfill
%\ForGradingUse 
\FancyOverlay
\newpage\egroup
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%  FancyOverlay: On the answer key, add some things that make it easier to use.
%%  Version in big letters, grading key
%%

\def\nbox#1{\edef\theprevdepth{\the\prevdepth}\nointerlineskip\vbox to 0pt{#1\vss}\prevdepth=\theprevdepth}

\def\vtab{%
\def\big{\fontsize{11}{11}\fontspec{\sillyfont}\bfseries}%
\def\TabularData{Right & \big Grade & Wrong \cr \hline }%
\foreach \numWrong in {0,1,...,\NumberOfMultipleChoice} {%
\pgfmathtruncatemacro\numRight{\NumberOfMultipleChoice-\numWrong}%
\pgfmathtruncatemacro\numPoints{\numRight * \PointsPerMultipleChoice}%
\protected@xappto\TabularData{ \numRight & \big \numPoints & \numWrong \vbox to 1.2em{} \cr }%
}%
\begin{tabular}{ccc}
\TabularData
\end{tabular}%
}

\DeclareDocumentCommand \FancyOverlay {} {%
\nbox{%
% Scoot to top of page
\vspace*{-9.5in}%
% Fill in the name blank
\nbox{\vspace{0.2in}\hbox to\textwidth{\hss \fontsize{24}{24}\fontspec{\sillyfont} \if@KeyGrader Grader\hspace{-0.25in}\else\if@KeyStudent Key\fi\fi \hspace{2in}}}%
% Huge exam version on top of the instructions
\nbox{\if@KeyVersionMarker\vspace*{1.1in}\hbox to \textwidth{\fontsize{190}{190}\fontspec{\sillyfont} \MakeUppercase \ExamVersion \hss \MakeUppercase \ExamVersion }\fi}%
% Where to papercut the exam key
\nbox{\if@KeySlice\vspace{7.7in}\begin{tikzpicture}\draw[dashed,ultra thick] (0,0) -- (7in,0);\end{tikzpicture}\fi}%
% Grading table on the side
\nbox{%
%\noindent\textcolor{red}{HI}\noindent%
\if@ShortAnswer\vspace*{3.8in}\else\vspace*{3.25in}\fi%
\hbox to 0.9\textwidth{%
\fontsize{9}{9}\fontspec{\sillyfont}%
\hspace*{-0.25in}%
\if@KeyChartLeft\vtab\fi%
\hss%
\if@KeyChartRight\vtab\fi%
\hspace*{-0.25in}%
}%
}%
}%
}


\def\NumberOfMultipleChoice{20}
\def\PointsPerMultipleChoice{5}
\def\FirstMultipleChoice{1}
\def\TotalNumberOfPoints{100}

\newif\if@KeyChartLeft
\newif\if@KeyChartRight
\newif\if@KeySlice
\newif\if@KeyGrader
\newif\if@KeyStudent
\newif\if@KeyVersionMarker
\newif\if@ShortAnswer
\newif\if@ExamKeysForStudent
\newif\if@ExamKeysForGraders
\newif\if@ShortAnswer
\ExplSyntaxOn
\keys_define:nn {UKMathExam} {
	CourseNumber			.code:n = {\def\CourseNumber{#1}},
	CourseName			.code:n = {\def\CourseName{#1}},
	Semester			.code:n = {\def\Semester{#1}},
	ExamDate			.code:n = {\def\ExamDate{#1}},
	ExamName			.code:n = {\def\ExamName{#1}},
	ExamNumber			.code:n = {\def\ExamNumber{#1}\def\ExamName{Exam~#1}},
	ExamVersion			.code:n = {\def\ExamVersion{#1}},
	ExamVersions			.code:n = {\def\ExamVersions{#1}},
	ExamKeysForStudent		.choice:,
	ExamKeysForStudent		.default:n = {true},
	ExamKeysForStudent/true		.code:n = {\@ExamKeysForStudenttrue},
	ExamKeysForStudent/false	.code:n = {\@ExamKeysForStudentfalse},
	ExamKeysForGraders		.choice:,
	ExamKeysForGraders		.default:n = {true},
	ExamKeysForGraders/true		.code:n = {\@ExamKeysForGraderstrue},
	ExamKeysForGraders/false	.code:n = {\@ExamKeysForGradersfalse},
	KeySlice			.choice:,
	KeySlice			.default:n = {true},
	KeySlice/true 			.code:n = {\@KeySlicetrue},
	KeySlice/false 			.code:n = {\@KeySlicefalse},
	KeyChartLeft			.choice:,
	KeyChartLeft			.default:n = {true},
	KeyChartLeft/true 		.code:n = {\@KeyChartLefttrue},
	KeyChartLeft/false 		.code:n = {\@KeyChartLeftfalse},
	KeyChartRight			.choice:,
	KeyChartRight			.default:n = {true},
	KeyChartRight/true 		.code:n = {\@KeyChartRighttrue},
	KeyChartRight/false 		.code:n = {\@KeyChartRightfalse},
	KeyGrader			.choice:,
	KeyGrader			.default:n = {true},
	KeyGrader/true 			.code:n = {\@KeyGradertrue},
	KeyGrader/false 		.code:n = {\@KeyGraderfalse},
	KeyStudent			.choice:,
	KeyStudent			.default:n = {true},
	KeyStudent/true 		.code:n = {\@KeyStudenttrue},
	KeyStudent/false 		.code:n = {\@KeyStudentfalse},
	KeyVersionMarker		.choice:,
	KeyVersionMarker		.default:n = {true},
	KeyVersionMarker/true 		.code:n = {\@KeyVersionMarkertrue},
	KeyVersionMarker/false 		.code:n = {\@KeyVersionMarkerfalse},
        NumberOfMultipleChoice          .code:n = {\def\NumberOfMultipleChoice{#1}},
        PointsPerMultipleChoice         .code:n = {\def\PointsPerMultipleChoice{#1}},
        FirstMultipleChoice             .code:n = {\def\FirstMultipleChoice{#1}},
        TotalNumberOfPoints             .code:n = {\def\TotalNumberOfPoints{#1}},
}
\DeclareDocumentCommand \ExamSetup { m } {
  \keys_set:nn {UKMathExam} {#1}
  \pgfmathtruncatemacro\MultipleChoicePoints{\NumberOfMultipleChoice*\PointsPerMultipleChoice}
  \pgfmathtruncatemacro\ShortAnswerPoints{\TotalNumberOfPoints - \MultipleChoicePoints}
  \ifnumequal{\ShortAnswerPoints}{0}{\@ShortAnswerfalse}{\@ShortAnswertrue}
}
\ExplSyntaxOff

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%  The answer bubbles. The AnsKey{} part allows the front page to be answer
%%  blanks, and the last page to have answers filled in.
%%

\def\AnswerBoxes{\if@KeyStudent\else\if@KeyGrader\else\ResetVersion\fi\fi%
\pgfmathtruncatemacro{\HalfOfNumberOfMultipleChoice}{(\NumberOfMultipleChoice)/2}%
\def\TabularData{}
\foreach \Counter in {1,2,...,\HalfOfNumberOfMultipleChoice} {
  \pgfmathtruncatemacro{\SmallNum}{\Counter +  \FirstMultipleChoice - 1}
  \pgfmathtruncatemacro{\OtherNum}{\SmallNum +  \HalfOfNumberOfMultipleChoice}
  \protected@xappto\TabularData{
    \textbf{\SmallNum.} & \answerbox{\AnsKey{\SmallNum}} &
    \textbf{\OtherNum.} & \answerbox{\AnsKey{\OtherNum}} \vspace{0.75cm} \cr
  }
}
\centerline{%
\begin{tabular}{r@{~}l@{\hspace{0.75in}}r@{~}l}
\TabularData
\end{tabular}%
}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%  \answerbox{a}
%%
%%  Display a row of 5 answer-bubbles, a-e, and shade in a specific
%%  bubble.  Use "f" for no shade.
%%
\def\alphy#1{\ifcase#1 0\or a\or b\or c\or d\or e\else #1\fi}
\def\boxy#1{\draw[BOX,BOX\alphy{#1}]({((#1)-1)*1.5},0) +(0.5,0.5) circle(0.5) +(0.5,0.5) node[opacity=1,font=\bf] (X) {\alphy{#1}};}
\DeclareDocumentCommand \answerbox { m } {
\bgroup
\begin{tikzpicture}[scale=0.6,baseline=(X.base)]
\tikzset{
BOX/.style={text height=1.5ex, text depth=0ex},
BOXa/.style={},
BOXb/.style={},
BOXc/.style={},
BOXd/.style={},
BOXe/.style={},
BOX#1/.add style={{fill}=gray,{fill opacity}=0.35},
}
\boxy{1} \boxy{2} \boxy{3} \boxy{4} \boxy{5}
\end{tikzpicture}
\egroup
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%  \shadowbox{ stuff to put inside a box }
%%
%%  Typeset some stuff inside a box with a shadow to make it look cool.
%%

\DeclareDocumentCommand \shadowbox { m } {
\tikz { \draw (0,0) node[
	draw=black,
	rectangle,
	drop shadow={shadow xshift=1ex, shadow yshift=-1ex, opacity=1},
	rounded corners,
	ultra thick,
	fill=white] {#1}; }
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%  \FlushRight
%%
%%  Put a graphic off to the right and then return the cursor to its original
%%  position.
%%

\DeclareDocumentCommand \FlushRight { m } {
\noindent\phantom{.}\hfill\hbox{\vtop to 0pt{\hbox{}\hbox{#1}\vss}}\noindent}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%  The multiple choice question section
%%
%%  \begin{questions}
%%
%%  \begin{question} Here is the actual question. Which choice should
%%  you pick?
%%
%%  \begin{choices} % the choices
%%  \choice    Choice a is nice
%%  \choice[!] Choice b is the right choice % the right choice
%%  \choice    Choice c is a popular choice
%%  \end{choices}
%%
%%  \end{question}
%%
%%  \end{questions}
%%

\DeclareDocumentCommand \QuestionSeparator {} {\noindent\unskip\hspace*{-\leftmargin}\rule{\textwidth}{.01in}}

\DeclareDocumentCommand \NewPage {} { \newpage \QuestionSeparator }

\DeclareDocumentEnvironment {questions} {} { \begin{enumerate} \pgfmathsetcounter{enumi}{\FirstMultipleChoice-1} } { \end{enumerate} }

\DeclareDocumentEnvironment {question} {} { \item } { \vfill \QuestionSeparator }

\DeclareDocumentEnvironment {choices} {  } { \medskip \par \textbf{Possibilities:}\vspace{-1ex} \begin{enumerate}[(a)] 
\DeclareDocumentCommand \choice { o } { 
	\item 
	\IfNoValueF{##1}{\SetAns{\arabic{enumi}}{\alph{enumii}}} % \choice[anything] will mark this one as right
}
} {\end{enumerate} } 

\DeclareDocumentCommand \Instructions {m} {

\begin{enumerate}

\item[]

\QuestionSeparator

\begin{center} #1 \end{center}

\vspace{-1em}

\QuestionSeparator

\end{enumerate}

}


\DeclareDocumentCommand \AllQuestions {} {

\IfFileExists{exam\ExamNumber\ExamVersion-before}{\input{exam\ExamNumber\ExamVersion-before}}{%
\IfFileExists{exam\ExamNumber-before}{\input{exam\ExamNumber-before}}{}}%
%
\IfFileExists{exam\ExamNumber\ExamVersion-mc}{
\Instructions{
{\bf Multiple Choice Questions } \\
{\it Show all your work on the page where the question appears}. \\
{\it Clearly mark your answer both on the cover page on this exam} \\
{\it and in the corresponding questions that follow}.
}
\input{exam\ExamNumber\ExamVersion-mc}}{}%
%
\IfFileExists{exam\ExamNumber\ExamVersion-after}{\input{exam\ExamNumber\ExamVersion-after}}{%
\IfFileExists{exam\ExamNumber-after}{\input{exam\ExamNumber-after}}{}}%
\newpage
}

\DeclareDocumentCommand \DividerLine {} {\noindent\rule{7.5in}{0.01in}}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%  Multiple Choice Answer key!
%%

\def\SetAns#1#2{\expandafter\xdef\csname AnsKey#1\ExamVersion\endcsname{#2}}
\def\AnsKey#1{\csname AnsKey#1\ExamVersion\endcsname}

\ExplSyntaxOn
\DeclareDocumentCommand \StoreAnswerKey {m} {
  \clist_set:Nn \l_answerkey_cl {#1}
  \bool_set_true:N \l_tmpa_bool
  \int_zero:N \l_tmpa_int
  \bool_do_while:Nn \l_tmpa_bool {
    \int_incr:N \l_tmpa_int
    \clist_pop:NNTF \l_answerkey_cl \l_tmpa_tl {
	\SetAns{\int_use:N\l_tmpa_int}{\l_tmpa_tl}
    } { \bool_set_false:N \l_tmpa_bool }
  }
}
\ExplSyntaxOff

\DeclareDocumentCommand \ResetVersion {} { \setcounter{page}{1} \StoreAnswerKey{f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f,f} }

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%  Main outline of the PDF:
%%

\DeclareDocumentCommand \MakeExam {m} {
	\ExamSetup{#1}%
	\foreach \ExamVersion in \ExamVersions { 
		\ResetVersion
		\FrontPage{}
		\AllQuestions
                \IfFileExists{FormulaSheet}{\input{FormulaSheet}}{}
	}
	\if@ExamKeysForStudent \foreach \ExamVersion in \ExamVersions {
		\setcounter{page}{1}
		\FrontPage{KeyStudent}
	} \fi
	\if@ExamKeysForGraders \foreach \ExamVersion in \ExamVersions { 
		\setcounter{page}{1}
		\FrontPage{
			KeyGrader,
			KeyVersionMarker,
			%KeySlice,
			%KeyChartLeft,
			%KeyChartRight,
		}
	} \fi
}

\def\log{\operatorname{\mathrm{log}}}
\def\ln{\operatorname{\mathrm{ln}}}
