%%
%% University of Waterloo Report Class
%%
%% Copyright (c) 2008 Felix Crux <felixc@felixcrux.com> <www.felixcrux.com>
%%
%% Portions of this file are based on work by Jason Jackson <jason@cvk.ca>,
%% and distributed by express permission of the author.
%%
%% Portions of this file are derived from the default LaTeX3 Project base
%% system files, and are copyright their respective authors. These portions
%% are distributed in compliance with the LaTeX Project Public License, under
%% which they were released by their authors.
%%
%% Best efforts have been made to mark any derived works as such.
%% 
%% This file may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% 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.3c or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%%
%%
%%                 -------------------=-=-------------------
%%
%%   Changelog:
%%     04-11-08: - Added ``nosecbreak'' option, which supresses page breaks
%%                 between sections.
%%     08-10-08: - Repurposed this to be a general UW report class, rather
%%                 just for work term reports.
%%     07-10-08: - Added the ``singlespace'' and ``multiauthor'' options.
%%     05-09-08: - Fixed a bug that caused the table of contents to be
%%                 numbered and listed in the table of contents.
%%     31-08-08: - Modified appendix handling to put ``Appendix N'' and the 
%%                 appendix title on the same line.
%%               - Introduced a workaround to be able to use 
%%                 \pagestyle{headings} without breaking appendices.
%%     07-07-08: - Initial release.
%%     
%%                 -------------------=-=-------------------
%%
%%
%%  User's Guide:
%%
%% A few commands have been introduced or modified to populate the title page
%% with UW-specific information. All of the following fields are required:
%% 
%%    \author{Your Name}{Your Student ID Number}
%%    \program{Your Program}{Your Faculty}
%%    \term{Your Previous Academic Term}
%%    \employer{Company Name}{Location}
%%    \title{The Title of Your Report}
%%
%% If you are using the ``multiauthor'' or ``academic'' options, these 
%% requirements change somewhat; see below.
%%
%% Your report should be divided into three environments: frontmatter,
%% mainmatter, and rearmatter. Please note that these are *environments,*
%% and as such operate differently than the ``book'' class' *commands* of
%% the same name.
%% 
%% Frontmatter should contain your titlepage, table of contents, and lists
%% of figures of tables, and your summary or abstract. If you set your
%% pagestyle to ``headings,'' your frontmatter will be unaffected. Page
%% numbering will always be in plain style and Roman numerals.
%% 
%% Mainmatter should contain your report proper.
%% 
%% Rearmatter is for references, appendices, etc.
%%
%% A new environment named ``summary'' has also been provided, to replace
%% ``abstract.'' It is functionally equivalent except for some tweaks to make
%% it fit the UW style, including being titled ``Summary'' instead of
%% ``Abstract.''
%%
%% Page margins have been set to those recommended by the CECS Style Guide.
%% A side effect of this is to eliminate the space allotted to marginal notes,
%% so if you really need those you will have to tweak this file manually;
%% perhaps by adding an option to enable them?
%%
%% Misc changes: Lists of figures, tables, and the table of contents now all
%% have their own page, as do section breaks. This is another candidate for
%% being modifiable by an option. The font size is by default 12pt, as
%% recommended by CECS. The document will be double-spaced, but not in the
%% frontmatter and rearmatter -- there is an option (singlespace) to disable 
%% doublespacing throughout. Appendices are marked as such. If you pass the 
%% multiauthor option, the ID number will not be printed (or required).
%%
%% There is a bug and workaround for using ``headings'' pagestyle in a 
%% document with appendices. All appendices except the last one will use
%% the plain pagestyle. To work around this, include a dummy appendix that
%% doesn't appear in the TOC by adding \section*{bugfix} (or any title) at
%% the end. This dummy appendix will show up as the last page in the document
%% and can easily be discarded.
%%
%% Lastly, please note that this is a first stab at a UW style, hacked
%% together on a Sunday afternoon. If you have any specific requests, drop me
%% a line and I'll try to incorporate them. If you can write your own changes,
%% that is of course even better, and I will incorporate them.
%% 
%% All the best,
%%
%% Felix C.
%%
%%
%%                 -------------------=-=-------------------
%%
%%
%%  Sample Report, to Illustrate Basic Usage:
%%
%% \documentclass{uw-report}
%%
%% \title{My Work Term Report}
%% \author{Felix Crux}{1234567890}
%% \program{Mechatronics Engineering}{Engineering}
%% \term{2A}
%% \employer{FooMatic Corp, Inc.}{Tranquility, LU}
%%
%% \begin{document}
%% 
%% \begin{frontmatter}
%%   \maketitle
%%   \tableofcontents
%%   \listoffigures
%%   \listoftables
%%   \begin{summary}
%%      The development of the widget at FooMatic can only be summarized
%%      by a summary such as this one. Things were said, plans were made,
%%      times were had. It ended poorly.
%%   \end{summary}
%% \end{frontmatter}
%%
%% \begin{mainmatter}
%%
%% \section{Introduction}
%% Mr. Smith, this is Ms. Doe.
%%
%% \section{The Middle Section}
%% This one goes in between the introduction and the conclusion.
%% \subsection{Why is it in the middle?}
%% Why ask pointless questions?
%%
%% \section{Conclusions}
%% Exciting as they are, they are not reproduced here.
%%
%% \end{mainmatter}
%%
%% \begin{rearmatter}
%%
%% \appendix
%% \section{Schematics of the widget}
%%
%% \section{Source code for the widget}
%%    
%% \end{rearmatter}
%%
%% \end{document}
%%
%%
%%                 -------------------=-=-------------------
%%
%%

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{uw-report}[2008/07/06 University of Waterloo Work Term Report]

\newif\if@singlespace
\newif\if@singleauthor
\newif\if@academic
\newif\if@secbreak
\@singlespacefalse
\@singleauthortrue
\@academicfalse
\@secbreaktrue
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\DeclareOption{singlespace}{\@singlespacetrue}
\DeclareOption{multiauthor}{\@singleauthorfalse}
\DeclareOption{academic}{\@academictrue}
\DeclareOption{nosecbreak}{\@secbreakfalse}
\ProcessOptions\relax
\LoadClass[letterpaper,12pt,titlepage]{article}

\RequirePackage{setspace}
\RequirePackage{ifthen}

\setlength{\oddsidemargin}{0in}
\setlength{\evensidemargin}{0in}
\setlength{\marginparwidth}{0in}
\setlength{\hoffset}{0.5in}
\setlength{\marginparsep}{0in}
\setlength{\footskip}{30pt}

\if@academic
    \newcommand{\class}[2]
        { \gdef\@classcode{#1}
          \gdef\@classname{#2} }
\else
    \newcommand{\employer}[2]
        { \gdef\@employer{#1}
          \gdef\@employerloc{#2} }
\fi
\newcommand{\term}[1]
    { \gdef\@term{#1} }
\newcommand{\program}[2]
    { \gdef\@program{#1}
      \gdef\@faculty{#2} }
\if@singleauthor
    \renewcommand{\author}[2]
        { \gdef\@author{#1}
          \gdef\@idnum{#2} }
\fi

\newenvironment{frontmatter}
    {
     \newpage
     \cleardoublepage
     \singlespacing
     \pagenumbering{roman}
     \pagestyle{plain}
    }

\newenvironment{mainmatter}
    {
     \newpage
     \cleardoublepage
     \if@singlespace
        \singlespacing
     \else
        \doublespacing
     \fi
     \pagenumbering{arabic}
    }

\newenvironment{rearmatter}
    {
     \newpage
     \cleardoublepage
     \singlespacing
     \pagestyle{plain}
    }

%% Summary section based on the ``Abstract'' section of the default article
%% class, distributed by the LaTeX3 Project under the LPPL.
\newcommand\summaryname{Summary}
\newenvironment{summarypage}
    {
     \if@twocolumn
        \@restonecoltrue\onecolumn
     \else
        \@restonecolfalse\newpage
     \fi
     \thispagestyle{plain}
     \singlespacing
    }
    {\if@restonecol\twocolumn \else \newpage \fi
     \if@twoside\else
        \setcounter{page}{1}
     \fi
    }
\newenvironment{summary}
    {
     \summarypage
     \null\vfil
     \@beginparpenalty\@lowpenalty
     \begin{center}%
        \bfseries \summaryname
        \@endparpenalty\@M
     \end{center}
    }
    { \par\vfil\null\endsummarypage }


%% Code for modified title page modified from work by Jason Jackson
\renewcommand{\maketitle}
    {
     \begin{titlepage}
      \begin{center}
        \singlespacing
        \noindent

        \MakeUppercase{\Large University of Waterloo}

        \textsc{\large Faculty of \@faculty}

        \vfill\vfill
    
        \textsc{\Huge \@title}

        \vfill\vfill

        \if@academic \textsf{\@classcode \\ \@classname}
        \else \textsf{\@employer \\ \@employerloc} \fi

        \vfill

        Prepared by \\
        \@author \\
        \if@singleauthor ID\# \@idnum \\ \fi
        \@term \ \@program \\
        \@date

      \end{center}
     \end{titlepage}
    }

%% Code for modified LOT/LOF modified from work by Jason Jackson
\let\@lof\listoffigures
\let\@lot\listoftables
\renewcommand{\listoffigures}{\newpage \@lof}
\renewcommand{\listoftables}{\newpage \@lot}

%% Code to mark appendices as such and list them in TOC
\let\@sec\section
\renewcommand\section{\@ifstar
    \MySectionStar%
    \MySectionNoStar%
}
\newcommand{\MySectionNoStar}[1]{
    \if@secbreak
      \newpage
    \fi
    \ifthenelse{\equal{\@appstart}{1}}{
        \stepcounter{section}
        \addcontentsline{toc}{section}{Appendix \Alph{section}: #1}
        \@sec*{Appendix \Alph{section}: #1}
    }{
        \@sec{#1}
    }
}
\newcommand{\MySectionStar}[1]{
    \if@secbreak
      \newpage
    \fi
    \ifthenelse{\equal{\@appstart}{1}}{
        \stepcounter{section}
        \@sec*{Appendix \Alph{section}: #1}
    }{
        \@sec*{#1}
    }
}

\newcommand{\@appstart}{0}
\let\@app\appendix
\renewcommand\appendix{
    \@app
    \renewcommand{\@appstart}{1}
}

