%%
%% qanda - Q and A - Questions and Answers
%%
%% Enda Hargaden, November 2011
%%
%% This class provides a formatting for producing exams with the answers on the last page.
%% This is largely based on the endnotes package
%% Feel free to edit or distribute as you wish.


\LoadClass{scrartcl}
\RequirePackage{enumerate}
\RequirePackage{setspace}
\singlespace
\setlength{\parindent}{0cm}
\let\footnote=\endnote

\newcommand{\qtf}[4]{\vspace{\baselineskip} \endnotemark . #1 %
\begin{enumerate}[(a)]\setlength{\itemsep}{0.5pt}
{\setlength\itemindent{0.75cm} \item #2}
{\setlength\itemindent{0.75cm} \item #3}
\end{enumerate}
\endnotetext{ #4}%
}

\newcommand{\q}[6]{\vspace{\baselineskip} \textbf{\endnotemark . #1} %
\begin{enumerate}[(a)]\setlength{\itemsep}{0.5pt}
{\setlength\itemindent{0.75cm} \item #2}
{\setlength\itemindent{0.75cm} \item #3}
{\setlength\itemindent{0.75cm} \item #4}
{\setlength\itemindent{0.75cm} \item #5}
\end{enumerate}
\endnotetext{ #6}%
}

\@definecounter{endnote}
\def\theendnote{\@arabic\c@endnote}

\def\@makeenmark{ \normalfont\@theenmark }
\def\makeenmark{\@makeenmark}

\def\theenmark{\@theenmark}

\newdimen\endnotesep

\def\endnote{\@ifnextchar[\@xendnote{\stepcounter{endnote}%
     \protected@xdef\@theenmark{\theendnote}%
     \@endnotemark\@endnotetext}}

\def\@xendnote[#1]{%
   \begingroup
     \c@endnote=#1\relax
     \unrestored@protected@xdef\@theenmark{\theendnote}%
   \endgroup
   \@endnotemark\@endnotetext}

%  Here begins a section of endnote code that's really different from
% the footnote code of LaTeX.

\let\@doanenote=0
\let\@endanenote=0

\newwrite\@enotes
\newif\if@enotesopen \global\@enotesopenfalse

\def\@openenotes{\immediate\openout\@enotes=\jobname.ent\relax
      \global\@enotesopentrue}

%  The stuff with \next and \meaning is a trick from the TeXbook, 382,
% there intended for setting verbatim text, but here used to avoid
% macro expansion when the footnote text is written.  \next will have
% the entire text of the footnote as one long line, which might well
% overflow limits on output line length; the business with \newlinechar
% makes every space become a newline in the \@enotes file, so that all
% of the lines wind up being quite short.

\long\def\@endnotetext#1{%
     \if@enotesopen \else \@openenotes \fi
     \immediate\write\@enotes{\@doanenote{\@theenmark}}%
     \begingroup
        \def\next{#1}%
        \newlinechar='40
        \immediate\write\@enotes{\meaning\next}%
     \endgroup
     \immediate\write\@enotes{\@endanenote}}

% \addtoendnotes works the way the other endnote macros probably should
% have, requiring the use of \protect for fragile commands.

\long\def\addtoendnotes#1{%
     \if@enotesopen \else \@openenotes \fi
     \begingroup
        \newlinechar='40
        \let\protect\string
        \immediate\write\@enotes{#1}%
     \endgroup}

%  End of unique endnote code

\def\endnotemark{%
   \@ifnextchar[\@xendnotemark
     {\stepcounter{endnote}%
      \protected@xdef\@theenmark{\theendnote}%
      \@endnotemark}}

\def\@xendnotemark[#1]{%
   \begingroup
      \c@endnote #1\relax
      \unrestored@protected@xdef\@theenmark{\theendnote}%
   \endgroup
   \@endnotemark}

\def\@endnotemark{%
  \leavevmode
  \ifhmode\edef\@x@sf{\the\spacefactor}\nobreak\fi
  \makeenmark
  \ifhmode\spacefactor\@x@sf\fi
  \relax}

\def\endnotetext{%
     \@ifnextchar [\@xendnotenext
       {\protected@xdef\@theenmark{\theendnote}%
    \@endnotetext}}

\def\@xendnotenext[#1]{\begingroup \c@endnote=#1\relax
   \xdef\@theenmark{\theendnote}\endgroup \@endnotetext}

\def\@xendnotenext[#1]{%
  \begingroup
     \c@endnote=#1\relax
     \unrestored@protected@xdef\@theenmark{\theendnote}%
  \endgroup
  \@endnotetext}


%  \theendnotes actually prints out the endnotes.

%  The user may want separate endnotes for each chapter, or a big
% block of them at the end of the whole document.  As it stands,
% either will work; you just say \theendnotes wherever you want the
% endnotes so far to be inserted.  However, you must add
% \setcounter{endnote}{0} after that if you want subsequent endnotes
% to start numbering at 1 again.

%  \enoteformat is provided so user can specify some special formatting
% for the endnotes.  It needs to set up the paragraph parameters, start
% the paragraph, and print the label.  The \mbox stuff in \enoteheading
% is to make and undo a dummy paragraph, to get around the games \section*
% plays with paragraph indenting and instead give us uniform
% indenting for all notes.

\def\notesname{Answers}%
\def\enoteheading{\section*{\notesname
  \@mkboth{\MakeUppercase{\notesname}}{\MakeUppercase{\notesname}}}%
  \mbox{}\par\vskip-\baselineskip}

\def\enoteformat{\rightskip\z@ \leftskip\z@ \parindent=1.8em
  \leavevmode\llap{\makeenmark}}

\def\enotesize{\footnotesize}

\def\theendnotes{\immediate\closeout\@enotes \global\@enotesopenfalse
  \begingroup
    \makeatletter
    %
    % The machinery with \@ResetGT and > here ensures that
    % \@doanenote works properly even if > is an active character
    % at the point where \theendnotes is invoked. > needs to have
    % catcode 12 when the arguments of \@doanenote are scanned, so
    % that the > in the string "macro:->" is matched.  The actual
    % footnote text is not an argument to \@doanenote, but just
    % follows it in the .ent file; so \@ResetGT can reset the
    % category code for > that should be used when processing
    % that text.  That resetting takes place within a
    % \begingroup-\endgroup block set up by \@doanenote and
    % \@endanenote, so the catcode for > is back to 12 for the
    % next note.
    %
    \edef\@tempa{`\string >}%
    \ifnum\catcode\@tempa=12%
      \let\@ResetGT\relax
    \else
      \edef\@ResetGT{\noexpand\catcode\@tempa=\the\catcode\@tempa}%
      \@makeother\>%
    \fi
    \def\@doanenote##1##2>{\def\@theenmark{##1}\par\begingroup
        \@ResetGT
        \edef\@currentlabel{\csname p@endnote\endcsname\@theenmark}%
        \enoteformat}
    \def\@endanenote{\par\endgroup}%
    \enoteheading
    \enotesize
    \input{\jobname.ent}%
  \endgroup}
  