
%----------------- Identification ----------------

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{jdr-hwexam}[2015/10/21
  Joe Rabinoff's homework and exam macros]

%----------------- Package loading ---------------

\RequirePackage{geometry}
\RequirePackage{framed}
\RequirePackage{tikz}
\RequirePackage{fancyhdr}

\RequirePackage{jdr-macros}

\usetikzlibrary{positioning}

%----------------- Package options ---------------

\newif\if@homework
\@homeworkfalse
\DeclareOption{homework}{\@homeworktrue}
\newif\if@exam
\@examfalse
\DeclareOption{exam}{\@examtrue}

\ProcessOptions

\newif\ifanswers
\answersfalse

\if@homework
\geometry{left=1.4in, right=1.4in, top=1.0in}
\pagestyle{empty}
\fi

\if@exam
\geometry{left=1.2in, right=1.2in, top=0.8in}
\pagestyle{empty}
\fi

%----------------- Numbering ---------------------

% For definition, theorem, etc.
\renewcommand\thesubsection{\arabic{subsection}}

%----------------- Preamble stuff ----------------

% For homeworks
\def\thename{Use \texttt{\textbackslash name\{\}} to set your name}
\def\theclass{Math ????}
\def\thehwno{??}
\def\theduedate{Due when?}
\def\thecollaborators{}
\def\thetitle{}

\newcommand\name[1]{\global\def\thename{#1}}
\newcommand\class[1]{\global\def\theclass{#1}}
\newcommand\homeworknum[1]{\global\def\thehwno{#1}}
\newcommand\duedate[1]{\global\def\theduedate{#1}}
\newcommand\collaborators[1]{\global\def\thecollaborators{#1}}
\renewcommand\title[1]{\global\def\thetitle{#1}}

\AtBeginDocument{%
  \ifthenelse{\equal{\thetitle}{}}%
    {\title{{\theclass} Homework \thehwno}}{}%
  \ifanswers\if@homework%
  \pagestyle{fancy}%
  \fancyfoot{}
  \fancyhead[LE,RO]{\small\thepage}
  \fancyhead[RE]{\small\textsc{\thename}}
  \fancyhead[LO]{\small\textsc{\thetitle}}
  \fi\fi%
}


%----------------- Homework ----------------------

\newcommand\hwtitle{%
  \ifanswers\thispagestyle{plain}\else\thispagestyle{empty}\fi%
  \noindent%
  \begin{center}%
    \textbf{\thetitle}\\%
    \vspace{.25\baselineskip}
    \ifanswers\thename\else\theduedate\fi%
  \end{center}%
  \ifanswers\begingroup%
  \renewcommand\thefootnote{}%
  \ifthenelse{\equal{\thecollaborators}{}}{}%
  {\footnote{\textit{Collaborators:} \thecollaborators}%
    \addtocounter{footnote}{-1}}%
  \endgroup\fi%
  \vskip\baselineskip}

\newenvironment{textproblems}[1][Exercises from the text:]{%
  \par\noindent #1
  \begin{list}{}{%
      \setlength{\leftmargin}{0pt}%
      \setlength{\rightmargin}{0pt}%
      \setlength{\itemsep}{0pt}%
      \setlength{\topsep}{.5\baselineskip}%
      \setlength{\listparindent}{\parindent}%
    }}{%
  \end{list}}

\newcounter{problem}
\newcounter{subproblem}[problem]

% Optional argument: for a comment about the problems
\newenvironment{problems}[1][]{%
  \begin{list}{\textbf{\large\refstepcounter{problem}\arabic{problem}. }}{%
      \setlength{\leftmargin}{0pt}%
      \setlength{\rightmargin}{0pt}%
      \setlength{\itemsep}{\baselineskip}%
      \setlength{\topsep}{\baselineskip}%
      \setlength{\itemindent}{0pt}%
      \setlength{\listparindent}{\parindent}%
      \let\problem\item%
    }%
    \ifthenelse{\equal{#1}{}}{}{\item[] #1}}{%
  \end{list}}

\newenvironment{subproblems}{%
  \begin{list}{\textbf{\refstepcounter{subproblem}\alph{subproblem})}}{%
      \setlength{\leftmargin}{2em}%
      \setlength{\rightmargin}{0pt}%
      \setlength{\itemsep}{.5\baselineskip}%
      \setlength{\topsep}{.25\baselineskip}%
      \setlength{\itemindent}{0pt}%
      \setlength{\listparindent}{\parindent}%
      \let\subproblem\item%
    }}{%
  \end{list}}

\newenvironment{answer}[1][]{%
  \setcounter{subproblem}{0}%
  \ifanswers\else\setbox0=\vbox\bgroup\fi%
  \begin{list}{}{%
      \setlength{\leftmargin}{0pt}%
      \setlength{\rightmargin}{0pt}%
      \setlength{\itemsep}{\baselineskip}%
      \setlength{\topsep}{\baselineskip}%
      \setlength{\itemindent}{0pt}%
      \setlength{\listparindent}{\parindent}%
    }%
    \item}{%
  \end{list}%
  \ifanswers\else\egroup\fi}

%----------------- Exams -------------------------

% Don't use this; use \examheader{}
\newcommand\examtitle[1]{\noindent%
  \thispagestyle{empty}%
  \begin{center}%
    \textsc{\large\bfseries\uppercase{#1}}%
  \end{center}}

% Don't use this; use \examheader{}
\newcommand\namebox{
  \begin{center}
    \begin{tikzpicture}[
      every node/.style={draw,
        minimum height=2\baselineskip,
        inner xsep=.5em,
        outer sep=0mm}]
      \node (namenode) at (0,0) [node font=\bfseries] {Name};
      \node [right=0mm of namenode, minimum width=.6\linewidth, outer sep=0mm] {};
    \end{tikzpicture}
  \end{center}}

% Automatic score table generator.
% Don't use this; use \examheader{}
\newcommand\scoretable[1]{
  \begin{center}
    \begin{tikzpicture}[
      numbernode/.style={draw,
        minimum width=\widthof{Total}+.6666em, 
        minimum height=1.5\baselineskip,
        outer sep=0mm},
      boxnode/.style={numbernode, minimum height=2\baselineskip}]
      \coordinate (box1) at (0,0);
      \pgfmathtruncatemacro{\total}{#1+1}
      \pgfmathtruncatemacro{\rows}{ceil(\total/11)}
      \pgfmathtruncatemacro{\cols}{ceil(\total/\rows)}
      \foreach \i in {1,...,\total}
      {
        \pgfmathtruncatemacro{\j}{Mod(\i-1, \cols)+1}
        \pgfmathtruncatemacro{\jminus}{equal(\j, 1) ? 1 : \j-1}
        \pgfmathsetmacro{\pos}{equal(\j, 1) ? "below" : "right"}
        \pgfmathsetmacro{\key}{equal(\j, 1) ? "box" : "number"}
        \pgfmathsetmacro{\head}{equal(\i, \total) ? "Total" : \i}
        \node (number\j) [numbernode, \pos=0mm of \key\jminus] {\head};
        \node (box\j) [boxnode, below=0mm of number\j] {};
      }
    \end{tikzpicture}
  \end{center}}

% Automatically keep track of the total number of problems for \scoretable
\xdef\lastproblem{0}
\AtEndDocument{%
  \if@filesw
    \immediate\write\@mainaux{%
      \string\xdef\string\lastproblem{\theproblem}\relax
    }%
  \fi}

% Arguments:
%  [#1]: number of problems (default: automatic)
%   #2 : class name
%   #3 : midterm title
\newcommand\examheader[3][\lastproblem]{%
  \examtitle{#2\\\smallskip#3}
  \vskip .2in
  \namebox
  \vskip .2in
  \scoretable{#1}
  \vskip .2in}

% Instructions list
\newenvironment{instructions}{
  \noindent 
  Please \textbf{read all instructions} carefully before beginning.
  \begin{list}{$\bullet$}{%
      \setlength{\itemsep}{.5\baselineskip}%
      \setlength{\topsep}{.5\baselineskip}%
      \setlength{\leftmargin}{1.5em}%
      \setlength{\rightmargin}{0pt}%
      \setlength{\itemindent}{0pt}%
      \setlength{\listparindent}{\parindent}%
    }}{%
  \end{list}}

% Optional argument: number of points, e.g.
\newenvironment{problem}[1][]{%
  \noindent\textbf{\Large Problem \refstepcounter{problem}\arabic{problem}.}%
  \ifthenelse{\equal{#1}{}}{}{\hfill[#1]}%
  \begin{framed}\noindent\ignorespaces}{%
  \end{framed}}

\newenvironment{solution}[1][Solution]{%
  \setcounter{subproblem}{0}%
  \ifanswers\else\setbox0=\vbox\bgroup\fi%
  \bigskip%
  \noindent\textbf{\large #1.}%
  \par\vskip .2\baselineskip plus 5pt minus 5pt\noindent\ignorespaces}{%
  \ifanswers\else\egroup\fi}

% True-false questions
\newenvironment{truefalse}{%
  \begin{list}{%
      \makebox[\widthof{w)}]{%
        \refstepcounter{subproblem}\alph{subproblem}) }%
      \makebox[3em]{\large\textbf T}%
      \makebox[3em]{\large\textbf F}}{%
      \setlength{\leftmargin}{8em}%
      \setlength{\labelwidth}{8em}%
      \setlength{\rightmargin}{0pt}%
      \setlength{\itemsep}{1.5\baselineskip}%
      \setlength{\topsep}{\baselineskip}%
      \setlength{\itemindent}{0pt}%
      \setlength{\listparindent}{\parindent}%
      \let\tfproblem\item}%
  }{\end{list}}

% Optional argument: number of scratch pages
\newcommand\scratchpage[1][1]{
  \foreach \i in {1,...,#1} {
    \newpage
    \begin{center}
      [Scratch work]
    \end{center}
  }}


\endinput
