% problempack.sty, a package for generating problem/solution sets.
%
% Copyright (C) 2008-2010 W. Trevor King <wking@drexel.edu>
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License along
% with this program; if not, write to the Free Software Foundation, Inc.,
% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

% see clsguide.pdf for a LaTeX package tutorial
% see xkeyval.pdf for a 72 page spiel on xkeyval

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{problempack}[2010/10/14 0.4 problempack]

\RequirePackage{comment}% show/hide the solution sections
\RequirePackage[pdftex]{hyperref} % pdf niceties & active links
\RequirePackage{amsmath}% environments for multiline displayed equations, \text{} and other enhancements

% get some \theorem environment handling (pick one of ntheorem or amsthm)
%\RequirePackage{ntheorem}
\RequirePackage{amsthm}

% color text (usenames -> with 68 dvips named colors)
\RequirePackage[dvipsnames, usenames]{color}
\RequirePackage{xkeyval} % handling x=y-type options

% non-argument options
\DeclareOptionX{nosolutions}{
  \renewcommand{\Pr@blemSkip}{2\baselineskip}
  % \bigskip to get a bit of space between problems
  \excludecomment{solution}
  %\includecomment{nosolution} % \includecomment clears the environment definition
}
\DeclareOptionX{solutions}{
  \renewcommand{\Pr@blemSkip}{0pt}
  %\includecomment{solution} % \includecomment clears the environment definition
  \excludecomment{nosolution}
}
\DeclareOptionX{tight}{
  % no indentation or parskips
  \setlength{\parindent}{0pt}
  \setlength{\parskip}{0pt}
}
\DeclareOptionX{loose}{
  % no indentation, reasonable parskips
  \setlength{\parindent}{0pt}
  \setlength{\parskip}{0.5\baselineskip}
}

% argument options
\DeclareOptionX{author}{\renewcommand{\Pr@blemAuthor}{#1}}
\DeclareOptionX{coursetitle}{\renewcommand{\Pr@blemCoursetitle}{#1}}
\DeclareOptionX{classtitle}{\renewcommand{\Pr@blemClasstitle}{#1}}
\DeclareOptionX{subheading}{\renewcommand{\Pr@blemSubheading}{#1}}
\DeclareOptionX{problemskip}{\renewcommand{\Pr@blemSkip}{#1}}

% argument and unrecognized options (not accessible through \ExecuteOptionsX{})
\DeclareOptionX*{
  \PackageWarning{problempack}{option `\CurrentOption' ignored}
}

% define theorem environments
\newtheorem{Pr@blem}{Problem}
\newcommand{\Pr@blemSkip}{0pt} % set to 2\baselineskip when nosolutions, to give breathing room between problems
\newenvironment{problem}{%
  \phantomsection%
  \addtocounter{Pr@blem}{1}% <- temporarily increment Pr@blem
  \addcontentsline{toc}{section}{Problem \arabic{Pr@blem}}%
  \addtocounter{Pr@blem}{-1}% <- restore to its initial value
  \begin{Pr@blem}% <- the Pr@blem counter actually gets implemented here
}{%
  \end{Pr@blem}%
  \vspace\Pr@blemSkip%
}

% HACK: you can't nest problem* environments, as there's only one PCC command.
% not that you should ever need to as far as I can imagine.
% Note: using a constant theorem name like ProblemSTAR fails on redefine attempt
% Note: you also get matching errors with \begin{Pr@blem#1}\end{Pr@blem\Pr@blemCounter}
\newcommand{\Pr@blemCounter}{}
\newenvironment{problem*}[1]{%
  \renewcommand{\Pr@blemCounter}{#1}%
  \phantomsection%
  \addcontentsline{toc}{section}{Problem #1}%
  \newtheorem*{Pr@blem\Pr@blemCounter}{Problem #1}%
  \begin{Pr@blem\Pr@blemCounter}%
}{%
  \end{Pr@blem\Pr@blemCounter}%
  \vspace\Pr@blemSkip%
}

%\theoremstyle{definition} % for amsthm.sty
\theoremstyle{plain} % for ntheorem.sty
%\newtheorem*{solution}{} % problem: starts out with a dot.
% solution must be defined here before we process the options,
% because TODO
% Also, \excludecomment{}ed environments choke on \next when
% called from another environment, for example
%  \newenvironment{invisableSolution}
%  \excludecomment{invisibleSolution}
%  \newenvironment{solution}{\begin{invisibleSolution}}{\end{invisibleSolution}}
%  \begin{solution}  % <--- chokes on \next
\newenvironment{solution}{}{} % null environment wrapper

% absorb problemskip from problem definition and spit it out after the nosolution region
\newenvironment{nosolution}{\vspace{-\Pr@blemSkip}}{\vspace\Pr@blemSkip}

\newcommand{\Part}[1]{\textcolor{Red}{(#1)}}
\newcommand{\ans}[1]{\textcolor{Red}{#1}}

% set defaults
\newcommand{\Pr@blemAuthor}{Trevor King}
\newcommand{\Pr@blemCoursetitle}{Phys 101}
\newcommand{\Pr@blemClasstitle}{Recitation 1}
\newcommand{\Pr@blemSubheading}{Chapter 1}
%\ExecuteOptions{solutions} % set up for solutions, but don't initialize, b/c \includecomment is touchy
\ExecuteOptionsX{tight}

% and process package-user given options
\ProcessOptionsX\relax

% finish setting up hyperref
\hypersetup{pdfauthor={\Pr@blemAuthor}}
\hypersetup{pdfsubject={\Pr@blemCoursetitle}}
\hypersetup{pdftitle={\Pr@blemClasstitle}}
\hypersetup{pdfstartview=FitH} % zooming to fit the page width.
\hypersetup{colorlinks} % don't do the ugly boxes around links

% setup a simple title
\renewcommand{\maketitle}{%
  \begin{center}
  {\Large \Pr@blemClasstitle} \\
  \Pr@blemSubheading
  \end{center}
}
