d9d374b30226f677c6c43e37919397d23d3f35a8
[problempack.git] / problempack.sty
1 % problempack.sty, a package for generating problem/solution sets.
2 %
3 % Copyright (C) 2008-2010 W. Trevor King <wking@drexel.edu>
4 %
5 % This program is free software; you can redistribute it and/or modify
6 % it under the terms of the GNU General Public License as published by
7 % the Free Software Foundation; either version 2 of the License, or
8 % (at your option) any later version.
9 %
10 % This program is distributed in the hope that it will be useful,
11 % but WITHOUT ANY WARRANTY; without even the implied warranty of
12 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 % GNU General Public License for more details.
14 %
15 % You should have received a copy of the GNU General Public License along
16 % with this program; if not, write to the Free Software Foundation, Inc.,
17 % 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 % see clsguide.pdf for a LaTeX package tutorial
20 % see xkeyval.pdf for a 72 page spiel on xkeyval
21
22 \NeedsTeXFormat{LaTeX2e}
23 \ProvidesPackage{problempack}[2010/10/14 0.4 problempack]
24
25 \RequirePackage{comment}% show/hide the solution sections
26 \RequirePackage[pdftex]{hyperref} % pdf niceties & active links
27 \RequirePackage{amsmath}% environments for multiline displayed equations, \text{} and other enhancements
28
29 % get some \theorem environment handling (pick one of ntheorem or amsthm)
30 %\RequirePackage{ntheorem}
31 \RequirePackage{amsthm}
32
33 % color text (usenames -> with 68 dvips named colors)
34 \RequirePackage[dvipsnames, usenames]{color}
35 \RequirePackage{xkeyval} % handling x=y-type options
36
37 % non-argument options
38 \DeclareOptionX{nosolutions}{
39   \renewcommand{\Pr@blemSkip}{2\baselineskip}
40   % \bigskip to get a bit of space between problems
41   \excludecomment{solution}
42   %\includecomment{nosolution} % \includecomment clears the environment definition
43 }
44 \DeclareOptionX{solutions}{
45   \renewcommand{\Pr@blemSkip}{0pt}
46   %\includecomment{solution} % \includecomment clears the environment definition
47   \excludecomment{nosolution}
48 }
49 \DeclareOptionX{tight}{
50   % no indentation or parskips
51   \setlength{\parindent}{0pt}
52   \setlength{\parskip}{0pt}
53 }
54 \DeclareOptionX{loose}{
55   % no indentation, reasonable parskips
56   \setlength{\parindent}{0pt}
57   \setlength{\parskip}{0.5\baselineskip}
58 }
59
60 % argument options
61 \DeclareOptionX{author}{\renewcommand{\Pr@blemAuthor}{#1}}
62 \DeclareOptionX{coursetitle}{\renewcommand{\Pr@blemCoursetitle}{#1}}
63 \DeclareOptionX{classtitle}{\renewcommand{\Pr@blemClasstitle}{#1}}
64 \DeclareOptionX{subheading}{\renewcommand{\Pr@blemSubheading}{#1}}
65 \DeclareOptionX{problemskip}{\renewcommand{\Pr@blemSkip}{#1}}
66
67 % argument and unrecognized options (not accessible through \ExecuteOptionsX{})
68 \DeclareOptionX*{
69   \PackageWarning{problempack}{option `\CurrentOption' ignored}
70 }
71
72 % define theorem environments
73 \newtheorem{Pr@blem}{Problem}
74 \newcommand{\Pr@blemSkip}{0pt} % set to 2\baselineskip when nosolutions, to give breathing room between problems
75 \newenvironment{problem}{%
76   \phantomsection%
77   \addtocounter{Pr@blem}{1}% <- temporarily increment Pr@blem
78   \addcontentsline{toc}{section}{Problem \arabic{Pr@blem}}%
79   \addtocounter{Pr@blem}{-1}% <- restore to its initial value
80   \begin{Pr@blem}% <- the Pr@blem counter actually gets implemented here
81 }{%
82   \end{Pr@blem}%
83   \vspace\Pr@blemSkip%
84 }
85
86 % HACK: you can't nest problem* environments, as there's only one PCC command.
87 % not that you should ever need to as far as I can imagine.
88 % Note: using a constant theorem name like ProblemSTAR fails on redefine attempt
89 % Note: you also get matching errors with \begin{Pr@blem#1}\end{Pr@blem\Pr@blemCounter}
90 \newcommand{\Pr@blemCounter}{}
91 \newenvironment{problem*}[1]{%
92   \renewcommand{\Pr@blemCounter}{#1}%
93   \phantomsection%
94   \addcontentsline{toc}{section}{Problem #1}%
95   \newtheorem*{Pr@blem\Pr@blemCounter}{Problem #1}%
96   \begin{Pr@blem\Pr@blemCounter}%
97 }{%
98   \end{Pr@blem\Pr@blemCounter}%
99   \vspace\Pr@blemSkip%
100 }
101
102 %\theoremstyle{definition} % for amsthm.sty
103 \theoremstyle{plain} % for ntheorem.sty
104 %\newtheorem*{solution}{} % problem: starts out with a dot.
105 % solution must be defined here before we process the options,
106 % because TODO
107 % Also, \excludecomment{}ed environments choke on \next when
108 % called from another environment, for example
109 %  \newenvironment{invisableSolution}
110 %  \excludecomment{invisibleSolution}
111 %  \newenvironment{solution}{\begin{invisibleSolution}}{\end{invisibleSolution}}
112 %  \begin{solution}  % <--- chokes on \next
113 \newenvironment{solution}{}{} % null environment wrapper
114
115 % absorb problemskip from problem definition and spit it out after the nosolution region
116 \newenvironment{nosolution}{\vspace{-\Pr@blemSkip}}{\vspace\Pr@blemSkip}
117
118 \newcommand{\Part}[1]{\textcolor{Red}{(#1)}}
119 \newcommand{\ans}[1]{\textcolor{Red}{#1}}
120
121 % set defaults
122 \newcommand{\Pr@blemAuthor}{Trevor King}
123 \newcommand{\Pr@blemCoursetitle}{Phys 101}
124 \newcommand{\Pr@blemClasstitle}{Recitation 1}
125 \newcommand{\Pr@blemSubheading}{Chapter 1}
126 %\ExecuteOptions{solutions} % set up for solutions, but don't initialize, b/c \includecomment is touchy
127 \ExecuteOptionsX{tight}
128
129 % and process package-user given options
130 \ProcessOptionsX\relax
131
132 % finish setting up hyperref
133 \hypersetup{pdfauthor={\Pr@blemAuthor}}
134 \hypersetup{pdfsubject={\Pr@blemCoursetitle}}
135 \hypersetup{pdftitle={\Pr@blemClasstitle}}
136 \hypersetup{pdfstartview=FitH} % zooming to fit the page width.
137 \hypersetup{colorlinks} % don't do the ugly boxes around links
138
139 % setup a simple title
140 \renewcommand{\maketitle}{%
141   \begin{center}
142   {\Large \Pr@blemClasstitle} \\
143   \Pr@blemSubheading
144   \end{center}
145 }