%% exams.sty
  %% Copyright 2016 Laurence R. Taylor
    %
  % This work may be distributed and/or modified under the
  % conditions of the LaTeX Project Public License, either version 1.3
  % of this license or (at your option) any later version.
  % The latest version of this license is in
  %   http://www.latex-project.org/lppl.txt
  % and version 1.3 or later is part of all distributions of LaTeX
  % version 2005/12/01 or later.
  %
  % This work has the LPPL maintenance status `maintained'.
  % 
  % The Current Maintainer of this work is Laurence R. Taylor [taylor.2@nd.edu].
  %
  % This work consists of the file exams.sty.

\makeatletter
\filename@parse{\@currname}
\edef\@currname{\filename@base}
\makeatother

\NeedsTeXFormat{LaTeX2e}% LaTeX 2.09 can not be used (nor non-LaTeX)
[1994/12/01]% LaTeX date must December 1994 or later
\ProvidesClass{exams}[2016/05/01 LRT test macros]

\RequirePackage[total={6.5in,9.75in}, top=1.2in, left=0.9in, includefoot]{geometry} 
\RequirePackage{etoolbox}  %% This package is essential. 
\usepackage{amssymb}


%: Some defaults
\gdef\defaultMarks{abcdefghj}%% expand as needed or change labels all together.

\gdef\defaultMultipleChoicePts{(5pts)}
\gdef\defaultPartialCreditPts{(12pts)}

\gdef\partialCreditItem#1{(#1)}
\gdef\markedPartialCreditItem#1{($\bullet$)}
\gdef\lineMark#1{(#1)}

\def\versionFilePath#1{versions/#1.ver}
\def\versionCoverFilePath#1{front_page/cover.#1.page}
\def\defaultCoverFilePath{front_page/cover.page}

\makeatletter
% \namedArgs  arguments of form {'name'}->{'value'}
\def\gobbleat#1@{}
\def\LRTargs#1#2->#3#4@{%
\expandafter\gdef\csname #1.#2\endcsname{#3}%
\gdef\LRTargsXX{{#1}#4@}%
\def\LRTydyd{{#1}@}%
\ifx\LRTydyd\LRTargsXX\relax%
\let\LRTYYnext=\gobbleat\else\let\LRTYYnext =\LRTargs\fi%
\expandafter\LRTYYnext\LRTargsXX}

\def\namedArgs#1#2{%
\ifempty{#2}{}{\expandafter\LRTargs{#1}#2@}%
}
\long\def\boxit#1#2#3{\vbox{\hrule\hbox{\vrule\kern#2
\vbox{\hsize=#3\kern#2#1\kern#2}\kern#2\vrule}\hrule}}

%: \forloop keeps its index in the #4 counter
% The loop exits with the smallest #2 + k(#1) which is greater than #3 in the counter #4
% If #3<\value{#4} at start, then the counter #4 remains unchanged 
\providecommand{\forloop}[5][1]{%
\setcounter{#4}{#2}%
\ifnumcomp{\value{#4}}{<}{#3 }%
{#5\addtocounter{#4}{#1}\forloop[#1]{\value{#4}}{#3}{#4}{#5}}%
{\ifnum\value{#4}=#3\relax#5\addtocounter{#4}{#1}\fi}}

%: The \new's
\newdimen\betweenProblemAndAnswers\betweenProblemAndAnswers=10pt
\newdimen\aboveSolution\aboveSolution=10pt
\newdimen\betweenAnswerLines\betweenAnswerLines=10pt
\newdimen\basic@LRTSkip

\newif\ifNoVersionFile
\newif\ifmarkedAnswerSheet
\newif\ifPartialCredit

\newcounter{VERSION}
\newcounter{COVERSHEETS}
\newcounter{SHOWSOLUTIONS}
\newcounter{tmpLRTC}
\newcounter{tmpLRTY}
\newcounter{tmpLRTCounter}
\newcounter{answerCounter}
\newcounter{breakCounter}
\newcounter{LRTArrayCount}
\newcounter{write problem number}
\newcounter{writeCounterA}
\newcounter{tmpLRTCounterB}
\newcounter{tmpLRTMCL}
\newcounter{tmpLRTCA}
\newcounter{answerCounterInv}



% checks to see if #1 is empty. If it is, do #2, if not do #3
\def\ifempty#1#2#3{\def\LRTxx{#1}\def\LRTempty{}\ifx\LRTxx\LRTempty#2\else#3\fi}

% If there is no version file, set up to do defaults as we read the problems
% This means \WriteNewVersion{\d} needs to be sure there is a version file.
\def\XX@LRTgetVersionEtc#1.#2.#3.#4@{%
\gdef\versionFile{}%
\global\NoVersionFiletrue%
\ifempty{#1}{\setcounter{VERSION}{0}}{\setcounter{VERSION}{#1}}%
\ifnum\value{VERSION}>0 %
\edef\versionFile{\versionFilePath{\number\value{VERSION}}}%
\IfFileExists{\versionFile}{\global\NoVersionFilefalse}{\global\NoVersionFiletrue}%
\fi%
%
\ifempty{#3}{\ifNoVersionFile%
\ifempty{#1}{\setcounter{SHOWSOLUTIONS}{1}}{\setcounter{SHOWSOLUTIONS}{0}}\fi}%
{\setcounter{SHOWSOLUTIONS}{#3}}%
\ifNoVersionFile \else\input\versionFile\relax\fi%
%
\ifempty{#2}{\setcounter{COVERSHEETS}{0}}{%
\ifnum\value{VERSION}=0 \setcounter{COVERSHEETS}{0}\else%
\setcounter{COVERSHEETS}{#2}\fi}%
}

\def\@LRTgetVersionEtc#1.#2.#3.#4@{%
\ifempty{#1}{\setcounter{VERSION}{0}}{\setcounter{VERSION}{#1}}%
\ifempty{#2}{\setcounter{COVERSHEETS}{0}}{\setcounter{COVERSHEETS}{#2}}%
\ifempty{#3}{%
\ifempty{#1}{\setcounter{SHOWSOLUTIONS}{1}}{\setcounter{SHOWSOLUTIONS}{0}}}%
{\setcounter{SHOWSOLUTIONS}{#3}}%
}


\newcommand\getVersionEtc[1][0.0.1]{%
\@LRTgetVersionEtc#1......@}


\def\makeLRTArray#1#2#3@{%
\ifnum\value{LRTArrayCount}<\value{#1} %
\else%
\expandafter\gdef\csname#1.\number\value{#1}\endcsname{#2}\addtocounter{#1}{1}%
\ifempty{#3}{}{\makeLRTArray{#1}#3@}\fi%
}
\gdef\defaultArrayStart{1}
%\makeArray  #2 is name of array, #3 is a list of vaules - typically {{x_1}{x_2}{x_3}{x_4}...}
%                    creates a counter named #1 which has the number of elements in the array - 1
%                    and entries \csname #2.1\endcsname, \csname #1.2\endcsname, etc. 
%                    The optional argument #1 is an upper bound to the number of elements alloted. 
%                    The routine allocates elements until it finds an empty one or until there are #1 elements. 
\newcommand\makeArray[3][200]{%
\setcounter{LRTArrayCount}{#1}%
\@ifundefined{c@#2}{\newcounter{#2}}{}%
\setcounter{#2}{\defaultArrayStart}%
\xdef\xLRTyx{{#2}#3}%
\expandafter\makeLRTArray\xLRTyx{}@%
\addtocounter{#2}{-1}%
}


\def\LRTcover{\IfFileExists{\versionCoverFilePath{\number\value{VERSION}}}%
{\xdef\coverFile{\versionCoverFilePath{\number\value{VERSION}}}}%
{\IfFileExists{\defaultCoverFilePath}{\xdef\coverFile{\defaultCoverFilePath}}{}}%
\setcounter{page}{0}\input\coverFile%
}

\def\coverSheet{\global\markedAnswerSheetfalse\LRTcover}
\def\markedCoverSheet{\global\markedAnswerSheettrue\LRTcover}


\def\defaultFileProblemList{%
\forloop{1}{\value{file problem number}}{tmpLRTCounterB}{%
\expandafter\xdef\csname \number\value{tmpLRTCounterB}.PermutedProblemData.fileProblem\endcsname{\number\value{tmpLRTCounterB}}%
}}

\gdef\fileProblemList{}
\def\nextFileProblem#1.#2!{\gdef\fileProblemList{#2}\gdef\dd{#1}}



%: exam environment
\newenvironment{exam}[1][]{%
\getVersionEtc[#1]%
\newcounter{file problem number}%
\setcounter{file problem number}{0}%
\xdef\versionFile{\versionFilePath{\number\value{VERSION}}}
\expandafter\IfFileExists{\versionFile}{\input \versionFile}{}%
}%
{\xdef\numberOfProblemsInFile{\number\value{file problem number}}%
\ifnum\value{VERSION}=0\relax\writeVersionZero\else\writePositiveVersions\fi}

%: 'before problem' environment
\newenvironment{before problem}{%
\edef\LRTbeforeBox{\csname before problem.\number\value{file problem number}\endcsname}%
\global\expandafter\newbox\LRTbeforeBox%
\global\expandafter\setbox\LRTbeforeBox=\vbox\bgroup}%
{\egroup}

%: 'before pdf problem' environment
\newenvironment{before pdf problem}[1]{%
\edef\LRTbeforeBox{\csname pdf.before problem.#1\endcsname}%
\global\expandafter\newbox\LRTbeforeBox%
\global\expandafter\setbox\LRTbeforeBox=\vbox\bgroup}%
{\egroup}

%: 'after problem' environment
\newenvironment{after problem}{%
\edef\LRTafterBox{\csname after problem.\number\value{file problem number}\endcsname}%
\global\expandafter\newbox\LRTafterBox%
\global\expandafter\setbox\LRTafterBox=\vbox\bgroup}%
{\egroup}

%: 'part' environment - when you begin a problem zero out part counter
\global\newbox\tmpPartBox
\global\newbox\tmpSolBox
\newcounter{part counter}
\newenvironment{part of problem}[1][]{\addtocounter{part counter}{1}%
\ifempty{#1}%
{\expandafter\gdef\csname cPS.\number\value{file problem number}.\number\value{part counter}\endcsname{}}%
{\expandafter\gdef\csname cPS.\number\value{file problem number}.\number\value{part counter}\endcsname{\vskip#1}}%
\global\setbox\tmpPartBox=\vtop\bgroup%
\global\advance\hsize by -40pt\noindent\ignorespaces}
{\egroup%
\global\expandafter\newbox\csname part.\number\value{file problem number}.\number\value{part counter}\endcsname%
\global\expandafter\setbox\csname part.\number\value{file problem number}.\number\value{part counter}\endcsname=\box\tmpPartBox%
\global\advance\hsize by 40pt
}
\newenvironment{part of solution}{\setbox\tmpSolBox=\vtop\bgroup\noindent\ignorespaces}
{\egroup%
\global\expandafter\newbox\csname solution.\number\value{file problem number}.\number\value{part counter}\endcsname%
\global\expandafter\setbox\csname solution.\number\value{file problem number}.\number\value{part counter}\endcsname=\box\tmpSolBox%
}

\newenvironment{remark after part}[1][]{\ifempty{#1}%
{\expandafter\gdef\csname cPR.\number\value{file problem number}.\number\value{part counter}\endcsname{}}%
{\expandafter\gdef\csname cPR.\number\value{file problem number}.\number\value{part counter}\endcsname{\vskip#1}}%
\setbox\tmpSolBox=\vbox\bgroup\noindent}
{\egroup%
\global\expandafter\newbox\csname remark.\number\value{file problem number}.\number\value{part counter}\endcsname%
\global\expandafter\setbox\csname remark.\number\value{file problem number}.\number\value{part counter}\endcsname=\box\tmpSolBox%
}

%: 'after pdf problem' environment
\newenvironment{after pdf problem}[1]{%
\edef\LRTbeforeBox{\csname pdf.after problem.#1\endcsname}%
\global\expandafter\newbox\LRTbeforeBox%
\global\expandafter\setbox\LRTbeforeBox=\vbox\bgroup}%
{\egroup}

%: problem environment
% Boxes problem into box problem.problem_number
% Sets counter answer.problem_number.count to 0
% prepends problem_number.ProblemData.pts to the box before it collects the problem text

\newbox\LRTtmpBox
\newdimen\LRTtmpBoxDimen
\newenvironment{problem}{%
\addtocounter{file problem number}{1}%
\setcounter{part counter}{0}%
\newcounter{answer.\number\value{file problem number}.count}%
\setcounter{answer.\number\value{file problem number}.count}{0}%
\edef\LRTproblemTag{\csname \number\value{file problem number}.ProblemData.pts\endcsname}%
\setbox\LRTtmpBox=\hbox{\LRTproblemTag}%
\LRTtmpBoxDimen=\wd\LRTtmpBox%
\expandafter\newbox\csname problem.\number\value{file problem number}\endcsname%
\global\expandafter\setbox\csname problem.\number\value{file problem number}\endcsname=\vtop%
\bgroup\noindent\hskip\LRTtmpBoxDimen\ }%
{\egroup}

\def\IFblank#1#2#3{%
\def\IFLRTblankyy{ }\def\IFLRTblankxx{#1}%
\ifx\IFLRTblankxx\IFLRTblankyy{}\else{#3}\fi%
}

\gdef\@@LRTgetAnswers#1\\#2!{%
\addtocounter{answer.\number\value{file problem number}.count}{1}%
\xdef\xLRTx{\number\csname c@answer.\number\value{file problem number}.count\endcsname}%
\expandafter\newbox\csname answer.\number\value{file problem number}.\xLRTx\endcsname%
\global\expandafter\setbox\csname answer.\number\value{file problem number}.\xLRTx\endcsname=\hbox{#1}%
\IFblank{#2}{}{\@@LRTgetAnswers#2!}%
}


\long\def\LRTGetAnswers#1\end{%
\@@LRTgetAnswers#1!%
\end}


%: answers environment
% Boxes each answer in box answer.problem_number.answer_number
% Increments answer.problem_number.count 
\newenvironment{answers}{%
\LRTGetAnswers}{}

%: solution environment
% Boxes each solution in box solution.problem_number with space prepended for Solution. 
\newenvironment{solution}{%
\expandafter\newbox\csname solution.\number\value{file problem number}\endcsname%
\global\expandafter\setbox\csname solution.\number\value{file problem number}\endcsname=\vtop\bgroup\noindent \textbf{Solution.}}%
{\mbox{\hfil}\egroup}



%%% WRITES
%: write exam with and without version file.
% If no version file make and write a default one and then use it. 


%: \writeVersionZero
\gdef\writeVersionZero{%
\makeArray{defaultMarksArray}{\defaultMarks}%% converts the \defaultMarks into an array
\gdef\finalNumberOfProblems{{\tiny need a version file to be set correctly}}%
\gdef\finalNumberOfPages{{\tiny need a version file to be set correctly}}%
\gdef\finalNumberOfProblemsInFile{100}%
\gdef \numberOfProblemsInPDFfile{\numberOfProblemsInFile}%
\writeCoverSheets%
\forloop{1}{\numberOfProblemsInPDFfile}{write problem number}{%
\writeVersionZeroProblem{\number\value{write problem number}}}%
\ifnum\value{SHOWSOLUTIONS}=2\relax\newpage\writeSolutionsAtBackVersionZero\fi%
}
%: \writeVersionZeroProblem
\def\writeVersionZeroProblem#1{%#1 is number of problem in file to be printed.
\expandafter\@ifundefined{before problem.#1}{}{%
\expandafter\copy\csname before problem.#1\endcsname}%
\par\noindent%
\llap{\textbf{\number\value{write problem number}.}}\noindent\expandafter\copy\csname problem.#1\endcsname%
\writeVersionZeroAnswers{#1}%
\expandafter\@ifundefined{after problem.#1}{}{%
\expandafter\copy\csname after problem.#1\endcsname}%
\ifnum\value{SHOWSOLUTIONS}=1\writeAtProblemSolutionVersionZero{#1}\fi%
\par\vskip 10pt\noindent\vrule width \textwidth depth .1pt height 0pt\par\vskip 10pt%
}
%: \writeVersionZeroAnswers
\def\writeVersionZeroAnswers#1{%
\ifnum\value{answer.#1.count}=0 %No answers for this problem
\else{%
\vskip\betweenProblemAndAnswers%
\par\noindent%
\forloop{1}{\value{answer.#1.count}}{answerCounter}{%
\@ifundefined{answer.#1.\number\value{answerCounter}}{}{%
\penalty-1\hbox{\lineMark{\expandafter\csname defaultMarksArray.\number\value{answerCounter}%
\endcsname}\ %
\edef\LRTlocalAnswerBox{\csname answer.#1.\number\value{answerCounter}\endcsname}%
\expandafter\copy\LRTlocalAnswerBox\hskip 4pt}}\hfill\null}}%
\fi}

%: \writePositiveVersions
% Checks for versionFile - if none, writes a default one
% Loads versionFile and does all the setup
\gdef\writePositiveVersions{%
\xdef\versionFile{\versionFilePath{\number\value{VERSION}}}%
\IfFileExists{\versionFile}{}{\makeVersionFile}%
\loadVersionFile%
\writePDF%
}

%: \makeVersionFile
\gdef\makeVersionFile{%
\newwrite\outFile%
\immediate\openout\outFile=\versionFile%
%
\makeArray{defaultMarksArray}{\defaultMarks}
\forloop{1}{\numberOfProblemsInFile}{tmpLRTC}{%
\ifnum\value{answer.\number\value{tmpLRTC}.count}>0{%
\edef\LRTNoA{\number\value{answer.\number\value{tmpLRTC}.count}}%
\edef\LRTlocal{}%
\forloop{1}{\LRTNoA}{tmpLRTCA}{%
\edef\LRTlocal{\LRTlocal{\number\value{tmpLRTCA}}}%
}
\edef\dLRTd{\string\namedArgs{\number\value{tmpLRTC}.AnswerData}{{Perm}->{\LRTlocal}}}%
\immediate\write\outFile{\dLRTd}}\fi}%
%
%
\forloop{1}{\numberOfProblemsInFile}{tmpLRTC}{%
\ifnum\value{answer.\number\value{tmpLRTC}.count}>0{%
\edef\dLRTd{\string\namedArgs{\number\value{tmpLRTC}.AnswerData}{{perLine}->{3}}}%
\immediate\write\outFile{\dLRTd}}\fi}%
%
%
\forloop{1}{\numberOfProblemsInFile}{tmpLRTC}{%
\ifnum\value{answer.\number\value{tmpLRTC}.count}>0{%
\edef\LRTlocal{}\edef\LRTNoA{\number\value{answer.\number\value{tmpLRTC}.count}}%
\forloop{1}{\LRTNoA}{tmpLRTCA}{%
\edef\LRTlocal{\LRTlocal{\csname defaultMarksArray.\number\value{tmpLRTCA}\endcsname}}}%
\edef\dLRTd{\string\namedArgs{\number\value{tmpLRTC}.AnswerData}{{Marks}->{\LRTlocal}{skip}->{\vskip 10pt}}}%
\immediate\write\outFile{\dLRTd}}\fi}%
%
%
\forloop{1}{\numberOfProblemsInFile}{tmpLRTC}{%
\ifnum\value{answer.\number\value{tmpLRTC}.count}>0%
\edef\LRTlocal{\defaultMultipleChoicePts}%
\else\edef\LRTlocal{\defaultPartialCreditPts}%
\fi%
\edef\dLRTd{\string\namedArgs{\number\value{tmpLRTC}.ProblemData}{{pts}->{\LRTlocal}}}%
\immediate\write\outFile{\dLRTd}}%
%
%
\forloop{1}{\numberOfProblemsInFile}{tmpLRTC}{%
\edef\LRTlocal{\number\value{tmpLRTC}}%
\let\eject=\relax
\ifodd\number\LRTlocal{\gdef\LRTlocalA{\vfill}}\else%
{\gdef\LRTlocalA{\vfill\eject}}\fi%
\edef\dLRTd{\string\namedArgs{\number\value{tmpLRTC}.PermutedProblemData}%
{{fileProblem}->{\LRTlocal}{spaceAfter}->{\LRTlocalA}}}%
\immediate\write\outFile{\dLRTd}}%
%
\immediate\closeout\outFile%
}

\def\cpr#1#2!{\xdef\cprA{#1}\xdef\cprB{#2}}

\newcounter{inverter}\setcounter{inverter}{1}
\def\JP#1!#2!{%
\expandafter\cpr#1!\expandafter\xdef\csname #2.\cprA\endcsname{\number\value{inverter}}%
\addtocounter{inverter}{1}%
\expandafter\@ifempty\expandafter{\cprB}{\relax}{\expandafter\JP\cprB!#2!}%
%
}

\def\IP#1!#2!{%
\expandafter\cpr#1!%
\expandafter\xdef\csname #2.\cprA\endcsname{\number\value{inverter}}%
\expandafter\xdef\csname #2.correct\endcsname{\cprA}%
\addtocounter{inverter}{1}%
\expandafter\@ifempty\expandafter{\cprB}{\relax}{\expandafter\JP\cprB!#2!}%
}
\newcounter{pdf problem number}%
%: \loadVersionFile------------------------------------------
\gdef\loadVersionFile{%
\input \versionFile\relax%
\setcounter{pdf problem number}{0}%
\forloop{1}{\numberOfProblemsInFile}{tmpLRTC}{%
\@ifundefined{\number\value{tmpLRTC}.PermutedProblemData.fileProblem}{\relax}%
{\addtocounter{pdf problem number}{1}}%
\@ifundefined{\number\value{tmpLRTC}.AnswerData.Perm}{\relax}{%
\setcounter{inverter}{1}%
\expandafter\IP\csname\number\value{tmpLRTC}.AnswerData.Perm\endcsname!\number\value{tmpLRTC}.answer!%
\setcounter{answer.\number\value{tmpLRTC}.count}{\number\value{inverter}}%
\addtocounter{answer.\number\value{tmpLRTC}.count}{-1}%
\makeArray[\number\value{answer.\number\value{tmpLRTC}.count}]{\number\value{tmpLRTC}.answer.mark}%
{\csname\number\value{tmpLRTC}.AnswerData.Marks\endcsname}
}%
\xdef\numberOfProblemsInPDFfile{\number\value{pdf problem number}}%
\xdef\finalNumberOfProblems{\numberOfProblemsInPDFfile}%
\xdef\finalNumberOfPages{\csname finalNumberOfPages.\number\value{VERSION}\endcsname}%
}%
}

\def\writeProblemNumber{\llap{\textbf{\number\value{write problem number}.}}}
%: \writePDF
\def\writePDF{%
\@ifundefined{finalNumberOfPages}{\gdef\finalNumberOfPages{0}}{}%
\@ifundefined{finalNumberOfProblems}{\xdef\finalNumberOfProblems{\number\value{file problem number}}}{}%
\expandafter\@ifundefined{finalNumberOfPages.\number\value{VERSION}}%
{\gdef\oldNumberOfPagesInThisVersion{0}}%
{\xdef\oldNumberOfPagesInThisVersion{\csname finalNumberOfPages.\number\value{VERSION}\endcsname}%
}%
\def\LRTstartPageNumber{1}%
\gdef\LRTcorrectMark{\correctMark}%
\ifnum\value{COVERSHEETS}=1 %
\coverSheet%
\def\LRTstartPageNumber{2}%
\else%
\ifnum\value{COVERSHEETS}=2 %
\gdef\LRTcorrectMark{\correctMarkA}%
\markedCoverSheet%
\def\LRTstartPageNumber{2}%
\else%
\ifnum\value{COVERSHEETS}=21 %
\markedCoverSheet%
\coverSheet%
\def\LRTstartPageNumber{2}%
\fi\fi\fi%
%
\setcounter{page}{\LRTstartPageNumber}%
\setcounter{write problem number}{1}%
\forloop{1}{\numberOfProblemsInPDFfile}{write problem number}{%
\writePositiveVersionProblem{\number\value{write problem number}}
}%
\newpage%
\addtocounter{page}{-1}%
\xdef\numberOfPages{\number\value{page}}%
\addtocounter{page}{1}%
\addtocounter{write problem number}{-1}%
\xdef\numberOfProblems{\number\value{write problem number}}%
\ifnum\value{SHOWSOLUTIONS}=2\relax\newpage\writeSolutionsAtBackVersionPositive\fi%
\write@aux%
\xdef\finalNumberOfPages{\csname finalNumberOfPages.\number\value{VERSION}\endcsname}%
\ifx\oldNumberOfPagesInThisVersion\finalNumberOfPages\relax\else%
\immediate\write16{}\immediate\write16{}\immediate\write16{}%
\errmessage{Final number of pages is wrong - please finish and TeX again!}\fi%
}

\def\writeProblemNumber{\llap{\textbf{\number\value{write problem number}.}}}
%: \writePositiveVersionProblem--------------------------
\def\writePositiveVersionProblem#1{%
\edef\PN{\csname #1.PermutedProblemData.fileProblem\endcsname}%
\expandafter\@ifundefined{pdf.before problem.#1}{\relax}{%
\expandafter\copy\csname pdf.before problem.#1\endcsname}%
\par\noindent%
\expandafter\@ifundefined{before problem.\PN}{}{%
\expandafter\copy\csname before problem.\PN\endcsname}%
\par\noindent%
\writeProblemNumber%
\noindent \hbox to 0pt{\csname \PN.ProblemData.pts\endcsname\hss}%
\expandafter\copy\csname problem.\PN\endcsname%
\expandafter\@ifundefined{part.\PN.1}{}{%
\forloop{1}{10}{tmpLRTCounter}{%
\expandafter\@ifundefined{part.\PN.\number\value{tmpLRTCounter}}{}{%
\par%
(\alph{tmpLRTCounter}) \expandafter\copy\csname part.\PN.\number\value{tmpLRTCounter}\endcsname%
\ifnum\value{SHOWSOLUTIONS}=1 \else%
\csname cPS.\PN.\number\value{tmpLRTCounter}\endcsname\fi%
}%
\ifnum\value{SHOWSOLUTIONS}=1 %
\expandafter\@ifundefined{solution.\PN.\number\value{tmpLRTCounter}}{}{%
\par\vskip 10pt%
{\noindent\textbf{Solution:\ }\vskip 4pt\expandafter\unvcopy\csname solution.\PN.\number\value{tmpLRTCounter}\endcsname}\par%
\par\noindent\vrule width \hsize depth 0pt height .3pt%
\vskip 20pt}\fi%
\expandafter\@ifundefined{remark.\PN.\number\value{tmpLRTCounter}}{}{%
\par%
\expandafter\copy\csname remark.\PN.\number\value{tmpLRTCounter}\endcsname%
\csname cPR.\PN.\number\value{tmpLRTCounter}\endcsname%
}%
}%
}%
\ifnum\value{answer.\PN.count}>0\relax\expandafter\writePositiveVersionAnswers{\PN}\fi%
\expandafter\@ifundefined{after problem.\PN}{}{%
\expandafter\copy\csname after problem.\PN\endcsname}%
\par\noindent%
\ifnum\value{SHOWSOLUTIONS}=1\writeAtProblemSolutionVersionPositive{\PN}\fi%
\expandafter\@ifundefined{pdf.after problem.#1}{\relax}{%
\expandafter\unvcopy\csname pdf.after problem.#1\endcsname}%
\csname #1.PermutedProblemData.spaceAfter\endcsname%
}


\newcount\@NOLLRT\newcount\@LaLLRT
\def\makeAutoLineData#1{%
\xdef\autoLineData{}%
\@NOLLRT=\number\value{answer.#1.count}%
\divide\@NOLLRT by \number\NumberEntriesPerLine\relax%
\xdef\NumberRows{\the\@NOLLRT}%
\@LaLLRT=\number\value{answer.#1.count}%
\multiply\@NOLLRT by \number\NumberEntriesPerLine\relax%
\advance\@LaLLRT by -\@NOLLRT%
\forloop{1}{\number\NumberRows}{answerCounter}{%
\forloop{1}{\NumberEntriesPerLine}{answer line counter}{%
\xdef\autoLineData{\autoLineData(\number\value{answerCounter},\number\value{answer line counter})}%
}}%
\@NOLLRT=\number\value{answer.#1.count}%
\divide\@NOLLRT by \number\NumberEntriesPerLine\relax%
\advance\@NOLLRT by 1%
\forloop{1}{\@LaLLRT}{answer line counter}{%
\xdef\autoLineData{\autoLineData(\number\@NOLLRT,%
\number\value{answer line counter})}%
}%
}

\newcount\@LRTpreviousRow
\newcount\@LRTnextCol
\newcount\@LRTcurrentRow
\newcount\@LRTcurrentCol
\newcount\@LRTcurrentCount
\newbool{@LRTAnswerLoop}
\newbool{@LRTRowLess}
\newbool{@LRTColLess}


\newcommand{\whilenumless}[4][@LRTAnswerLoop]{%
\booltrue{#1}%
\whileboolexpr{bool{#1}}{%
\ifnumless{#2}{#3}{\booltrue{#1}#4}%
{\boolfalse{#1}}%
}}

\newcommand{\whiledefnotempty}[3][@LRTAnswerLoop]{%
\booltrue{#1}%
\whileboolexpr{bool{#1}}{%
\ifdefempty{#2}{\boolfalse{#1}}{\booltrue{#1}#3}
}}

\def\@LRTPopOne(#1,#2)#3!{\xdef\autoLineData{#3}%
\global\@LRTcurrentRow=#1\relax\global\@LRTcurrentCol=#2%
}

\def\numberEntriesPerLine#1#2!{\xdef\NumberEntriesPerLine{#1}%
\xdef\autoLineData{#2}}%
\def\commarl#1,#2!{\xdef\ccprA{#1}\xdef\ccprB{#2}}
\newcounter{answer line counter}

\def\@@lexError@LRT#1#2#3{%
\errmessage{^^J^^J Bad lexicographical #1 ordering in TeX-problem  #2.^^J
 Bad lexicographical #1 ordering in pdf-problem  #3.^^J^^J}%
}
%: \writePositiveVersionAnswers---------------------
\def\writePositiveVersionAnswers#1{%
\edef\@PDF@LRT@PN{\number\value{write problem number}}
\vskip\betweenProblemAndAnswers%
\edef\adpl{\csname#1.AnswerData.perLine\endcsname}%
\expandafter\numberEntriesPerLine\adpl!%
\basic@LRTSkip=\textwidth\relax\divide\basic@LRTSkip%
by \number\NumberEntriesPerLine\relax%
\def\LRTEPT{}%
{\ifx\autoLineData\LRTEPT \makeAutoLineData{#1}\fi}%
\global\@LRTpreviousRow=1%
\global\@LRTnextCol=1%
\global\@LRTcurrentCount=1%
\par\noindent%
\whiledefnotempty{\autoLineData}{%
\expandafter\@LRTPopOne\autoLineData!%
%
\whilenumless[@LRTRowLess]{\@LRTpreviousRow}{\@LRTcurrentRow}%
{\par\csname #1.AnswerData.skip\endcsname\noindent%
\global\@LRTnextCol=1%
\advance\@LRTpreviousRow by 1%
}%
%
\ifnumgreater{\@LRTpreviousRow}{\@LRTcurrentRow}{\def\autoLineData{}%
\@@lexError@LRT{row}{#1}{\number\value{write problem number}}}{}%
\ifnumgreater{\@LRTnextCol}{\@LRTcurrentCol}{\def\autoLineData{}%
\@@lexError@LRT{column}{#1}{\number\value{write problem number}}}{}%
\whilenumless[@LRTColLess]{\@LRTnextCol}{\@LRTcurrentCol}%
%BLANK
{\leavevmode{\hbox to\basic@LRTSkip{\hfill}}%
\global\advance\@LRTnextCol by 1}%
%ANSWER
\leavevmode{\hbox to\basic@LRTSkip{%
\@LRTmakeAnswerBox{#1}{\the\@LRTcurrentCount}\hss}}%
\global\advance\@LRTcurrentCount by 1%
\global\advance\@LRTnextCol by 1}%
}

\def\@LRTmakeAnswerBox#1#2{%
\@ifundefined{answer.#1.#2}{XXX}{%
\edef\LRTpermutedAC{\csname#1.answer.#2\endcsname}
{\lineMark{\csname #1.answer.mark.#2\endcsname}\ %
\edef\LRTlocalAnswerBox{\csname answer.#1.\LRTpermutedAC\endcsname}%
\expandafter\copy\LRTlocalAnswerBox\hfil\null}}
}

%: \writeAtProblemSolutionVersionZero
\def\writeAtProblemSolutionVersionZero#1{%
\@ifundefined{solution.#1}{}{
\vskip\aboveSolution%
\par\noindent%
\hbox to 0pt{\textbf{Solution. }\hss}%
\expandafter\copy\csname solution.#1\endcsname}%
}

%: \writeSolutionsAtBackVersionZero 
\gdef\writeSolutionsAtBackVersionZero{%
\setcounter{write problem number}{1}%
\forloop{1}{\numberOfProblemsInFile}{write problem number}{%
\@ifundefined{solution.\number\value{write problem number}}{}{%
\vskip\aboveSolution%
\par\noindent%
\llap{\textbf{\number\value{write problem number}.}\ }%
\expandafter\copy\csname solution.\number\value{write problem number}\endcsname%
\par\vskip 10pt\noindent\vrule width \textwidth depth .1pt height 0pt\par\vskip 10pt}}%
}


%: \writeAtProblemSolutionVersionPositive
\def\writeAtProblemSolutionVersionPositive#1{%
\@ifundefined{solution.#1}{}{%
\vskip\aboveSolution%
\par\noindent%
\hbox to 0pt{\textbf{Solution. }\hss}%
\expandafter\copy\csname solution.#1\endcsname}%
}

%: \writeSolutionsAtBackVersionPositive
\gdef\writeSolutionsAtBackVersionPositive{%
\setcounter{write problem number}{1}%
\forloop{1}{\numberOfProblemsInFile}{write problem number}{%
\vskip\aboveSolution%
\par\noindent%
\llap{\textbf{\number\value{write problem number}.}\ }%
\@ifundefined{solution.\number\value{write problem number}}{\noindent\textbf{Solution.}\ }{%
\expandafter\copy\csname solution.\number\value{write problem number}\endcsname%
\par\vskip 10pt\noindent\vrule width \textwidth depth .1pt height 0pt\par\vskip 10pt}%
\forloop{1}{10}{tmpLRTCounter}{%
\expandafter\@ifundefined{solution.\number\value{write problem number}.\number\value{tmpLRTCounter}}{}{%
\vskip 4pt\noindent(\alph{tmpLRTCounter})\ \expandafter\copy\csname solution.\number\value{write problem number}.\number\value{tmpLRTCounter}\endcsname}\par%
\vskip 10pt}%
\par\noindent\vrule width \textwidth depth .1pt height 0pt\par\vskip10pt%
}%
}


%: \writeCoverSheets
\gdef\writeCoverSheets{%
\ifnum\value{COVERSHEETS}=1 %
\coverSheet%
\else%
\ifnum\value{COVERSHEETS}=2 %
\gdef\LRTcorrectMark{\correctMarkA}%
\markedCoverSheet%
\else%
\ifnum\value{COVERSHEETS}=21 %
\markedCoverSheet%
\coverSheet%
\fi\fi\fi%
\def\LRTstartPageNumber{2}%
}

\gdef\write@aux{
\edef\dLRTd{\string\expandafter\string\gdef\string\csname\space finalNumberOfPages.\number\value{VERSION}\string\endcsname{\numberOfPages}}%
\immediate\write\@auxout{\dLRTd}%
}

\def\defaultMultipleChoiceNumberStyle#1{%
\hbox to 30pt{\ \hbox to 12pt{\hfill#1}\lineLabelPunct\hfill}%
}
\global\let\multipleChoiceNumberStyle=\defaultMultipleChoiceNumberStyle

\gdef\lineLabelPunct{.}
%: multipleChoiceLine------------
\gdef\multipleChoiceLine#1#2{%#1 - pdf file problem number:     #2 - width of line.  
\ifcsname \number#1.PermutedProblemData.fileProblem\endcsname{\relax}%
{\edef\@LRTactualProblemNumberInFile{\csname \number#1.PermutedProblemData.fileProblem\endcsname}%
\ifcsname \number\@LRTactualProblemNumberInFile.AnswerData.Perm\endcsname\relax%
{%
\setcounter{tmpLRTMCL}{\number\value{answer.\@LRTactualProblemNumberInFile.count}}%
\addtocounter{tmpLRTMCL}{-1}%
\ifnum\value{tmpLRTMCL}=0 \setcounter{tmpLRTMCL}{1}\fi%
\basic@LRTSkip#2 %
\advance\basic@LRTSkip by -30pt%
\divide\basic@LRTSkip by \value{tmpLRTMCL}\relax%
\edef\@LRTnumberOfAnswers{\number\value{answer.#1.count}}%
\edef\LRTcorrect{\csname \@LRTactualProblemNumberInFile.answer.correct\endcsname}%
\mbox{}\multipleChoiceNumberStyle{#1}%
\forloop{1}{\@LRTnumberOfAnswers}{tmpLRTMCL}{%
\ifmarkedAnswerSheet%
{\ifnum\value{tmpLRTMCL}=\LRTcorrect %
\mbox{}\hbox to \basic@LRTSkip{\markedPartialCreditItem{%
\csname\@LRTactualProblemNumberInFile.answer.mark.\number\value{tmpLRTMCL}\endcsname}\hfill}%
\else%
\mbox{}\hbox to \basic@LRTSkip{\partialCreditItem{%
\csname\@LRTactualProblemNumberInFile.answer.mark.\number\value{tmpLRTMCL}\endcsname}\hfill}%
\fi}%
\else{%
\mbox{}\hbox to \basic@LRTSkip{\partialCreditItem{%
\csname\@LRTactualProblemNumberInFile.answer.mark.\number\value{tmpLRTMCL}%
\endcsname}\hfill}}%
\fi}%-- end of loop
}\fi%
}\fi}


%: isPartialCredit
% Usage: \isPartialCredit{aparent problem number} \fi
%
\gdef\isPartialCredit#1{%#1 - apparent problem number
\global\PartialCreditfalse%
\ifcsname \number#1.PermutedProblemData.fileProblem\endcsname\relax%
\edef\@LRTactualProblemNumberInFile{\csname \number#1.PermutedProblemData.fileProblem\endcsname}%
\edef\@LRTnumberOfAnswers{\number\value{answer.\@LRTactualProblemNumberInFile.count}}%
\ifnum\number\@LRTnumberOfAnswers > 0 \global\PartialCreditfalse\else\global\PartialCredittrue\fi%
\fi%
\ifPartialCredit}

%: \WriteNewVersion
\def\WriteNewVersion#1{%
\getVersionEtc[#1]%
\xdef\versionFile{\versionFilePath{\number\value{VERSION}}}%
\IfFileExists{\loadVersionFile\writePDF}{}%
}


\makeatother


\pagestyle{empty}
\thispagestyle{empty}
\headheight=14pt


