
%%
%%	class.sty: Homework & Exam Style:
%%
%%	
%%	This style is used for homeworks and exams. You should
%%	define \ClassName to be the class name and
%%	\ClassNumber to be the class number. If this is an
%%	exam, you should define \ClassExam to be the kind of
%%	exam (final, midterm, whatever). If you're making an
%%	exam, you should also say \pagestyle{examheaders}
%%	and \thispagestyle{firstexamheader} in your document preamble.
%%	
%%	\listofproblems will print a table listing the problem
%%	numbers, the page they appear on and the number of
%%	points they're worth.  You're also left with space for
%%	to enter the final problem score. Points information
%%	is recorded in the file \currentjob.lop, much like
%%	table of contents and list of figures data.
%%	
%%	The ``problems'' environment is currently an alias for
%%	``enumerate''; however, you should you \problem rather
%%	than \item for the top level problems.  This will add
%%	formatting info to the list-of-problems.
%%	
%%	Before each problem, \Points{<value>} will print
%%	``[Points <value>]'' and record the points in the .lop
%%	file. An optional argument, \Points{<value>}[<label>]
%%	labels the current problem so you can refer to it
%%	later using \ref.
%%	
%%	Answers are boxed, and only printed if \answerstrue
%%	has been declared, or if you've say \Solutions. You
%%	can use \ifanswers if you want to do conditional
%%	answers outside of an answer box.
%%	
%%	Otherwise, \begin{answer} ... \end{answer} passes the
%%	answer through to TeX only if \ifanswers is true.  You
%%	can specify an optional dimension; that much space is
%%	left when printing without answers. You can also
%%	specify 'p' to say you want a page eject when answers
%%	aren't being printed. I.e.
%%	\begin{answer}[3in] foo \end{answer} will pass 'foo'
%%	to TeX if printing with answers, otherwise, it'll leave
%%	3 inches of room for the student to write down an answer.
%%	
%%	You can also say \TrueFalse{true}{your text} or
%%	\TrueFalse{false}{your text} to make a true false
%%	question. The 'true' or 'false' (must be one of the
%%	two) is the correct answer
%%
%%
%%	\begin{hint} ... \end{hint} provides a boxed region like answer,
%%	with a title (\hinttitle).
%%
%%	example:
%%
%%	\documentstyle[class]{article}
%%	\def\ClassName{Csci}
%%	\def\ClassNumber{2555}
%%	\def\ClassExam{Final~Exam}
%%	\handedout{whenver}	%% included in headers/footers
%%	\Solutions		%% optionally include solutions
%%				%% can also say \answerstrue
%%	\listofproblems		%% a table of problems & point values
%%	\begin{problems}
%%	\problem \Points{20}
%%		This is the question
%%	\begin{answer}
%%	This is the answer  
%%	\end{answer}
%%	
%%	\problem
%%	\begin{enumerate}
%%		\item\Points{10}[labelname]
%%	
%%		This ia a sub-probem with an optional label name for reference
%%	
%%		\begin{answer}[3in]
%%		 This is an answer that will leave 3in of space when answers
%%		 aren't being printed.
%%		\end{answer}
%%	  
%%		\item\Points{5}
%%		\ifanswers include some non-boxed in answer
%%			\else \vskip 2in \fi
%%	
%%	\end{enumerate}
%%	\end{problems}

\def\ClassName{CSci}
\def\ClassNumber{}
\def\ClassExam{}

\newif\ifanswers
\answersfalse
\def\Solutions{\answerstrue}

\def\Homework#1{%
  \ifanswers Solutions to \ClassName\ \ClassNumber~Homework~#1%
  \else \ClassName\ \ClassNumber~Homework~#1 \fi}

\def\MP#1{%
  \ifanswers Solutions to \ClassName\ \ClassNumber~Machine Problem~#1%
  \else \ClassName\ \ClassNumber~Machine Problem~#1 \fi}

\def\handedout#1{\gdef\thehandedout{#1}}
\handedout{\today}
\def\Assigned#1{\centerline{Assigned #1}\handedout{#1}}
\def\Due#1{\centerline{Due~#1}}
\def\date#1{\gdef\thedate{#1}}
%
\def\ps@classheaders{
	\def\@oddhead{\hbox to \textwidth{
		\ClassName\ \ClassNumber \hfill -\thepage- \hfill \thehandedout}}
	\def\@oddfoot{\hbox to \textwidth{
		\ClassName\ \ClassNumber \hfill -\thepage- \hfill \thehandedout}}
	\def\@evenhead{}
	\def\@evenfoot{}
}
\pagestyle{classheaders}

\def\ps@firstclassheaders{
	\def\@oddhead{\hbox to \textwidth{
		\ClassName\ \ClassNumber \hfill \thehandedout}}
	\def\@oddfoot{\hbox to \textwidth{
		\ClassName\ \ClassNumber \hfill -\thepage- \hfill \thehandedout}}
	\def\@evenhead{}
	\def\@evenfoot{}
}

\def\ps@examheaders{
	\def\@oddhead{
		\ClassName\ \ClassNumber \hfill -\thepage-
		\hfill Id\#
		\ifanswers
			\underline{\Large Solutions}
		\else
			\underline{\hbox to 2in{}}\fi}
	\def\@oddfoot{
		\ClassName\ \ClassNumber \hfill -\thepage- \hfill \thehandedout}
%		\ClassName\ \ClassNumber \hfill -\thepage- \hfill \@PrintPagePoints{\thepage} }
	\def\@evenhead{}
	\def\@evenfoot{}
}

\def\ps@firstexamheaders{
	\def\@oddhead{%
		\hfill
		{\Large\bf \ClassName\ \ClassNumber~\ClassExam~\Answersp}
		\hfill}
	\def\@oddfoot{}
	\def\@evenhead{}
	\def\@evenfoot{}
}

\def\ID{ID\#:\underline{\ifanswers SOLUTIONS \else \hskip 4in\fi}}

\pagestyle{classheaders}
\thispagestyle{firstclassheaders}

\def\Answersp{\ifanswers {\bf Solutions}\fi}

\newdimen\BAwidth
\def\answerwidth{\textwidth}

\begingroup
\catcode`\{= 12 \catcode`\}= 12
 \catcode`\[=1 \catcode`]=2
 \catcode`\|=0 \catcode`\\=12
 |global|long|def|@gobbleanswer #1\end{answer}[|end[answer]]%
|endgroup

\global\def\answer{\@ifnextchar [{\@answer}{\@answer[0pt]}}

\global\def\@answer[#1]{%
%%
%%	Did they specify 'p' for a new page?
%%
\if p#1%
	\@tempswatrue
\else
	\@tempswafalse
	\@tempdima=#1 \relax
\fi
\ifanswers
  \vspace{1em}
  \def\next{%
	\par
	\begin{tabular}{|p{\linewidth}|}%
	\hline
	{\bf Answer:}\\%
  }
\else
   \vspace{1em}
   \def\next{%
	\if@tempswa
		\clearpage
	\else
	    \ifdim\@tempdima=0pt%
		\vfil
	    \else
		\vskip\@tempdima
	    \fi
	\fi
	\let\do\@makeother\dospecials\@gobbleanswer}%
        \vspace{1em}
\fi \next}
\def\endanswer{\ifanswers \\ \hline \end{tabular}\vspace{1em}\fi}


\newif\ifhints
\hintstrue

\begingroup
\catcode`\{= 12 \catcode`\}= 12
 \catcode`\[=1 \catcode`]=2
 \catcode`\|=0 \catcode`\\=12
 |global|long|def|@gobblehint #1\end{hint}[|end[hint]]%
|endgroup

\def\hinttitle{Hints from Heloise}

\global\def\hint{\@ifnextchar [{\@hint}{\@hint[0pt]}}

\global\def\@hint[#1]{%
%%
%%	Did they specify 'p' for a new page?
%%
\if p#1%
	\@tempswatrue
\else
	\@tempswafalse
	\@tempdima=#1 \relax
\fi
\ifhints
  \def\next{%
	\par
	\begin{tabular}{|p{\linewidth}|}%
	\hline
	{\bf \hinttitle:}\\%
  }
\else
   \def\next{%
	\if@tempswa
		\clearpage
	\else
	    \ifdim\@tempdima=0pt%
		\vfil
	    \else
		\vskip\@tempdima
	    \fi
	\fi
	\let\do\@makeother\dospecials\@gobblehint}%
\fi \next}
\def\endhint{\ifhints \\ \hline \end{tabular}\fi}

\parindent=0pt
\parskip=6pt
\textheight=9.25in
\textwidth=6.75in
\voffset=-1in
\hoffset=-0.25in
\marginparwidth 0pt
\oddsidemargin  0pt
\evensidemargin  0pt

\def\answerpage{\ifanswers\clearpage\fi}

%%
%%	For true false exams
%%
%% 	usage:
%%	\TrueFalse{false}{text}
%%	\TrueFalse{true}{text}
%%
%%	first arg is correct answer, only printed \ifanswers is true
%%

\long\def\TrueFalse#1#2{
\begingroup
  \dimen0=\linewidth
  \advance\dimen0 by -10em
  \advance\dimen0 by -2\tabcolsep
  \def\tfstruct{{\vrule width 0pt height 2.5ex depth 1ex}}
  \begin{tabular}{ccp{\dimen0}}
  {\bf TRUE} & {\bf FALSE} & \\
%
  \framebox[5em]{\tfstruct
	\expandafter\ifanswers\csname if#1\endcsname {$\surd$}\else{}\fi\fi}
  & 
  \framebox[5em]{\tfstruct
	\expandafter\ifanswers\csname if#1\endcsname {}\else{$\surd$}\fi\fi}
  & {#2}
  \end{tabular}
\endgroup
}

%%
%%	points tracking
%%
\newcount\@totalpoints

\def\listofproblems{%
%%\tracingmacros=1
\makeatletter
\openin1 \jobname.lop
\ifeof1
    \typeout{No list of problems file \jobname.lop, omitting table}%
    {\em List of Problems goes here}
  \else
    \@totalpoints=0
    \begin{tabular}{|l|r|r|p{0.75in}|}
    \hline
    Problem	& Page & Possible & Score\\%
    \hline\hline
    %%%%
    %%%% the body of \@starttoc w/o the outer begingroup...endgroup
    %%%%
    \@input{\jobname.lop}%
    \multicolumn{2}{|c|}{{\bf  Total}} & \the\@totalpoints & \\
    \hline
    \end{tabular}
    \makeatother
\fi
    \if@filesw \expandafter\newwrite\csname tf@lop\endcsname
             \expandafter\openout \csname tf@lop\endcsname \jobname.lop\relax
    \fi \global\@nobreakfalse
%%\tracingmacros=0
}


\def\problems{\begin{enumerate}}
\def\problem{\addtocontents{lop}{\protect{\@PointSpacer}}\item}
\def\endproblems{\end{enumerate}}

\newcount\@pointCount
\@pointCount=0

\def\@pointLabel#1[#2]{%
  \label{#2}
  {\bf [#1~Points]~}
  \addtocontents{lop}{\protect{\@PointItem}{#2}{#1}}
}
\def\Points#1{%
  \@ifnextchar [%
	{\@pointLabel#1}%
	{\global\advance\@pointCount by 1%
	 \@pointLabel#1[p@l@\the\@pointCount]%
	}
}

%%
\def\@CreatePagePoints#1{
  \message{Attempt to create points for #1}%
  \@ifundefined{@pp:#1}{%
	\@namedef{@pp:#1}{\relax}%
	\expandafter\newcount\csname @ppc:#1\endcsname%
	\expandafter\global\csname @ppc:#1\endcsname=0\relax%
  }{}}

\def\@AddPagePoints#1#2{%
 %% #1 is page number
 %% #2 is points to add
 %%
\message{Attempt to add points to #1}%
 \@CreatePagePoints{#1}%% just in case it's not there
 \global\expandafter\advance\csname @ppc:#1 \endcsname by #2 \relax
}

\def\@PrintPagePoints#1{%
  \message{Attempt to get points for #1}%
  \@CreatePagePoints{#1}%
  \message{\csname @ppc:#1\endcsname is \the\csname @ppc:#1\endcsname**}
  **\number\the\csname @ppc:#1\endcsname**%
  }

\def\@PointItem #1#2 {%
\global\advance\@totalpoints by #2\relax%
%%\@ifundefined{r@#1}{}{%  else ...
%%   \message{Attempt to add points}%
%%   \edef\@tempa{\@nameuse{r@#1}}
%%   \edef\@tempa{\expandafter\@cdr\@tempa\@nil}
%%   \message{cdr tempa is \@tempa}
%%   \@AddPagePoints{\@tempa}{#2}\relax%
%%}
%%
\ref{#1} & \pageref{#1} & #2 &\\%
\hline}

\def\@PointSpacer {\gdef\@PointSpacer{\hline\hline}}

\def\PointsItem#1#2{
  \global\@namedef{@p:#1}{#2}
}

\def\PointsRef#1{%
	\@ifundefined{@p:#1}{\hbox{??}}{\csname @p:#1\endcsname}
}





