version 0.0, basic functionality, homebrewed keyval parser.
authorWilliam Trevor King <wking@thor.yang.physics.drexel.edu>
Thu, 6 Aug 2009 13:04:24 +0000 (09:04 -0400)
committerWilliam Trevor King <wking@thor.yang.physics.drexel.edu>
Thu, 6 Aug 2009 13:04:24 +0000 (09:04 -0400)
version 0.1, integrated CTAN keyval package for option parsing.
version 0.2, added the problemskip option for tweaking problem spacing.
version 0.3, added section-level entries to the table of contents.

problempack.sty

index e7479c23a49ff67499aa93af4a9f06017f3a34ce..9efb4c90a50f195f9147bd19096a414aaaab64f7 100644 (file)
@@ -1,6 +1,8 @@
 % problempack.sty, a package for generating problem/solution sets.
+% Version 0.3
 %
 % see clsguide.pdf for a LaTeX package tutorial
+% see xkeyval.pdf for a 72 page spiel on xkeyval
 
 \NeedsTeXFormat{LaTeX2e}
 \ProvidesPackage{problempack}[2008/04/02]
 \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
-\RequirePackage{amsthm} % for \theorem environment handling
+
+% 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
 
-% define some option-processing macros
-\def\Pr@blemOptKey#1=#2 {#1}
-\def\Pr@blemOptArg#1=#2 {#2}
-\newcommand{\Pr@blemOptSetup}[1]{%
-  \expandafter\edef \csname\Pr@blemOptKey#1 \endcsname {\Pr@blemOptArg#1 }%
-}
-% some unit tests ;)
-%\typeout{\Pr@blemOptKey abcd=efg = abcd}
-%\typeout{\Pr@blemOptArg abcd=efg = efg}
-%\Pr@blemOptSetup{abcd=efg}
-%\typeout{\abcd = efg}
-%\def\x{ABCD=EFG}
-%\expandafter\Pr@blemOptSetup\expandafter{\x }
-%\typeout{\ABCD = EFG}
-
-% Our current method is limited by:
-%  option name == control sequence, and 
-%  no error messages on goofy option names
-%  \edef not protected like \newcommand, so could silently clobber something
-%   important.
-
-% non-agrument options
-\DeclareOption{nosolutions}{
+% 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
 }
-\DeclareOption{solutions}{
+\DeclareOptionX{solutions}{
   \renewcommand{\Pr@blemSkip}{0pt}
   %\includecomment{solution} % \includecomment clears the environment definition
   \excludecomment{nosolution}
 }
-% argument and unrecognized options (not accessible through \ExecuteOptions{})
-\DeclareOption*{
-  \typeout{ping \CurrentOption}  
-  \expandafter\Pr@blemOptSetup\expandafter{\CurrentOption}
-  \typeout{Unrecognized option \CurrentOption}
+\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}{\begin{Pr@blem}}{\end{Pr@blem}\vspace\Pr@blemSkip}
+\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}\newtheorem*{Pr@blem\Pr@blemCounter}{Problem #1}\begin{Pr@blem\Pr@blemCounter}}%
-  {\end{Pr@blem\Pr@blemCounter}\vspace\Pr@blemSkip}
+\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}
+%\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
 \newcommand{\Part}[1]{\textcolor{Red}{(#1)}}
 \newcommand{\ans}[1]{\textcolor{Red}{#1}}
 
-% set defaults ('x,y,z' not 'x, y, z')
-%  problem: for '\ExecuteOptions{wierd}',
-%  the option 'wierd' is not handled by '\DeclareOption*{}', but is '\relax'ed
-%  this means that we have to initialize any x=y-type options by hand.
+% 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
-\Pr@blemOptSetup{author={Trevor King}}
-\Pr@blemOptSetup{coursetitle={Phys 101}}
-\Pr@blemOptSetup{classtitle={Recitation 1}}
-\Pr@blemOptSetup{subheading={Chapter 1}}
+\ExecuteOptionsX{tight}
 
 % and process package-user given options
-\ProcessOptions\relax
+\ProcessOptionsX\relax
 
 % finish setting up hyperref
-\hypersetup{pdfauthor={\author}}
-\hypersetup{pdfsubject={\coursetitle}}
-\hypersetup{pdftitle={\classtitle}}
+\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
 
 % You can safely delete/override of the following code without effecting the
 % core problem/solution environment behavior.
 
 \renewcommand{\maketitle}{%
   \begin{center}
-  {\Large Recitation 1} \\
-  \subheading
+  {\Large \Pr@blemClasstitle} \\
+  \Pr@blemSubheading
   \end{center}
 }
 
 %% include some useful goodies
-\usepackage[pdftex]{graphicx}   % to include images
-\DeclareGraphicsRule{*}{mps}{*}{}
-\usepackage{emp}                % embed MetaPost pictures in LaTeX
+\RequirePackage[pdftex]{graphicx}   % to include images
+%\DeclareGraphicsRule{*}{mps}{*}{}
+%\RequirePackage{emp}                % embed MetaPost pictures in LaTeX
 % you need to add \begin{emp*}\end{emp*} by hand, because emp parser
 % doesn't realize \end{EMP} might expand to \end{emp} or \end{empfile}.
 %\newenvironment{EMP}[1]%
 
 
 %% define a few physics shortcuts
-\newcommand{\U}[1]{\text{ #1}}                 % units shortcut
-\newcommand{\E}[1]{\ensuremath{\cdot 10 ^{#1}}}        % exponent shortcut
-\newcommand{\dg}{\ensuremath{^{\circ}}}         % degree symbol ^o
-\newcommand{\vect}[1]{\ensuremath{\mathbf{#1}}} % make vectors bold
-
-\newcommand{\ihat}{\vect{\hat{i}}}
-\newcommand{\jhat}{\vect{\hat{j}}}
-\newcommand{\khat}{\vect{\hat{k}}}
-\newcommand{\rhat}{\vect{\hat{r}}}
+%\newcommand{\U}[1]{\text{ #1}}                        % units shortcut
+%\newcommand{\E}[1]{\ensuremath{\cdot 10 ^{#1}}}       % exponent shortcut
+%\newcommand{\dg}{\ensuremath{^{\circ}}}         % degree symbol ^o
+%\newcommand{\vect}[1]{\ensuremath{\mathbf{#1}}} % make vectors bold
+%\newcommand{\p}[3]{\left#1 #2 \right#3}   % parenthesis, e.g. \p({complicated expr})
+%
+%\newcommand{\ihat}{\vect{\hat{i}}}
+%\newcommand{\jhat}{\vect{\hat{j}}}
+%\newcommand{\khat}{\vect{\hat{k}}}
+%\newcommand{\rhat}{\vect{\hat{r}}}
 
 
 %% finally, set up some simple formatting.
 \textwidth 7.5in
 \pagestyle{empty} % no room for page numbers
 
-% no indentation or parskips
-\setlength{\parindent}{0pt}
-\setlength{\parskip}{0pt}
-
 % struts for table spacing
-\newcommand{\Tstrut}{\rule{0pt}{2.6ex}}
-\newcommand{\Bstrut}{\rule[-1.2ex]{0pt}{0pt}}
+%\newcommand{\Tstrut}{\rule{0pt}{2.6ex}}
+%\newcommand{\Bstrut}{\rule[-1.2ex]{0pt}{0pt}}