% --------------------------------------%--------------------------------------
% ---------------------------- Identification part ----------------------------
% --------------------------------------%--------------------------------------

% I use RCS for version control, and the rcs package from Joachim
% Schrod to fetch the useful information.  The ifthen package is used
% in the interpretation of the information.
\RequirePackage{rcs}

\RCS$Date: 2000/12/21 15:15:49 $
\RCS$RCSfile: aathesis.cls,v $
\RCS$Revision: 1.23 $
\RCS$State: Exp $
\RCS$Author: aa $

% Interpret, print and provide the RCS information.  This is a verry
% general approach used without any changes in all my LaTeX packages
% and classes.
\input{aausercs}


% --------------------------------------%--------------------------------------
% ----------------------------- The revision log ------------------------------
% --------------------------------------%--------------------------------------

% $Log: aathesis.cls,v $
% Revision 1.23  2000/12/21 15:15:49  aa
% Changed footnote
%
% Revision 1.22  2000/10/10 13:27:51  aa
% Minor typo
%
% Revision 1.21  2000/10/10 13:25:59  aa
% Increased space in sectionmark (thanks Tage)
%
% Revision 1.20  1999/03/22 19:58:13  anders
% Less skip in paragraphs (and subsection, subsubsection and subparagraph).
%
% Revision 1.19  1998/12/11 13:38:49  anders
% Changed length on lines in part and title to match textwidth (titlewidth).
%
% Revision 1.18  1998/11/20 10:24:27  anders
% Changed endnotes a little bit + other minor changes.
%
% Revision 1.17  1998/06/04 09:39:51  anders
% Changed layout and corrected header information for endnotes.
%
% Revision 1.16  1998/06/03 13:30:54  anders
% Added commands for isdn numer and report type and number (not used in the
% class).  Found a way to eat leading zero in the day of the date.
%
% Revision 1.15  1998/05/29 13:38:10  anders
% Removed dublication of bibliography in toc.
%
% Revision 1.14  1998/05/29 13:29:55  anders
% Footnotes at the end of each chapter (optional but default).
%
% Revision 1.13  1998/05/29 11:44:13  anders
% Changed backmatter and thebibliography.
%
% Revision 1.12  1998/05/28 19:15:25  anders
% Changed default option marginpar to nomarginpar.  Added the savercsinfo
% command, and use this (AADate and so on) in the maketitle command.
%
% Revision 1.11  1997/06/30 11:47:07  anders
% Changed listof (from float.sty) to add contentsline to toc.
%
% Revision 1.10  1997/06/29 21:01:30  anders
% Minor corrections to titlepage (added subtitle).
%
% Revision 1.9  1997/03/20 11:55:30  anders
% Some small layot changes in part.
%
% Revision 1.8  1997/03/19 16:59:05  anders
% Moved \reset@font from font command to where the font command is used.
%
% Revision 1.7  1997/03/10 13:46:29  anders
% Added bibliography listings to table of contents.
%
% Revision 1.6  1997/03/10 12:10:37  anders
% Small changes in numered chapter head and titlepage.
%
% Revision 1.5  1997/03/07 12:59:59  anders
% Changed the name of options bigmarg/equalmarg to marginpar/nomarginpar.
%
% Revision 1.4  1997/03/07 12:53:10  anders
% Fixed bug in @makeschapterhead which made problems for table of contents
% and list of figures/tables.
%
% Revision 1.3  1997/03/07 12:25:18  anders
% Fixed bug in list of figures and list of tables.
%
% Revision 1.2  1997/03/07 11:49:24  anders
% Made som small changes to titlepage an made the thickness (width) of rules
% configurable.
%
% Revision 1.1  1997/03/06 17:24:38  anders
% Position text on the titlepage with stretch instead of static values.
%
% Revision 1.0  1997/03/06 16:57:31  anders
% First revision of my new thesis class.  It is completly rewritten from
% the old revision 0.13.


% --------------------------------------%--------------------------------------
% ----------------------------------- Usage -----------------------------------
% --------------------------------------%--------------------------------------

% This class is made for my Dr. thesis, but I think it is general and
% therefore also usable for others.  It extends the LaTeX book class
% with a new pagelayout and other addons that I prefer.
% 
% Options (options from the LaTeX book class are also available):
% 
%   final/draft
%   marginpar/nomarginpar
% 
% New commands for the title:
%
%   \subtitle{A sub title}
%   \thesistype{Type of thesis}
%   \institution{Your institution}
%   \logo{Logo for your institution}
% 
% Font customization (use \renewcommand):
%   
%   \titlefont
%   \revfont
%   \authorfont (also used for date and institution)
%   \thesistypefont
%   \partfont
%   \partnumfont
%   \chapterfont
%   \chapternumfont
%   \sectionfont
%   \subsectionfont
%   \subsubsectionfont
%   \paragraphfont
%   \subparagraphfont
%   \headingsfont
%   \figurenamefont
%   \figuretextfont
% 
% Page layout customization (use \setlength):
% 
%   \marginparwidth  % default 25mm
%   \marginparsep    % default  3mm
%   \allmargins      % default 30mm
%   \toprulewidth    % default  1mm
%   \bottomrulewidth % default .5mm
% 
% If you want equal right and left margins, set marginparwidth and
% marginparsep to 0mm with the equalmarg option.
% 
% This is an example of a document using this class:
% 
% \documentclass[final,a4paper]{aathesis}
% 
% Revision control (optional)
% \usepackage{rcs}
% \RCS$Date: 2000/12/21 15:15:49 $
% \RCS$RCSfile: aathesis.cls,v $
% \RCS$Revision: 1.23 $
% \RCS$State: Exp $
% \RCS$Author: aa $
% \savercsinfo{Date,RCSfile,Revision,State,Author}
% 
% \usepackage{graphics}
% 
% \begin{document}
% 
% \frontmatter
% 
% \maketitle
% \title{Thesis title}
% %\subtitle{The subtitle}              % optional
% \author{Authors Name}
% %\date{The date}                      % optional
% \thesistype{Type of thesis}
% \institution{Your Institution}
% \logo{\includegraphics{instlogo.ps}}  % optional
% 
% \chapter{Acknowledge}
% 
% Whatever
% 
% \tableofcontents
% 
% \listoffigures
% 
% \listoftables
% 
% \mainmatter
% 
% \part{The first part of the thesis}   % optional
% 
% \chapter{A chapter}
% 
% Whatever
% 
% \chapter{Another chapter}
% 
% Whatever
% 
% \part{Second part of the thesis}
% 
% \chapter{Another chapter}
% 
% Whatever
% 
% \chapter{Another chapter}
% 
% Whatever
% 
% \endmatter
% 
% \appendix
% 
% \part{Appendix}
% 
% \chapter{An appendix}
% 
% Whatever
% 
% \end{document}


% --------------------------------------%--------------------------------------
% -------------------------------- Start code ---------------------------------
% --------------------------------------%--------------------------------------

% Some new booleans needed in the options.
\newboolean{draft} \newboolean{final}

% The size of all margins.
\newlength{\allmargins}\setlength{\allmargins}{-10mm}
                                % Just a negative value to
                                % say it hasn't any value.
\newlength{\mymarginparwidth}
\newlength{\mymarginparsep}

% Save original footnote
\let\orgfootnote\footnote

% Smart footnote (punctation aware)
\newcommand{\aafootnote}[1]{\@ifnextchar.{\@afootnote{#1}}{\@@footnote{#1}}}
\newcommand{\@@footnote}[1]{\@ifnextchar,{\@afootnote{#1}}{%
    \footnotemark\footnotetext{#1} }}
\def\@afootnote#1#2{\makebox[0pt][l]{\expandafter\@#2}%
  \footnotemark\footnotetext{#1}}
\let\footnote\aafootnote


% --------------------------------------%--------------------------------------
% -------------------------- Declaration of options ---------------------------
% --------------------------------------%--------------------------------------

% Paper layout (latex class options).  We must translate to the names
% used in the Vmargin package.
\DeclareOption{a4paper}{\def\@@paper{A4}}
\DeclareOption{a5paper}{\def\@@paper{A5}}
\DeclareOption{b5paper}{\def\@@paper{B5}}
\DeclareOption{letterpaper}{\def\@@paper{USletter}}
\DeclareOption{legalpaper}{\def\@@paper{USlegal}}
\DeclareOption{executivepaper}{\def\@@paper{USexecutive}}

% The draft option indicates that the paper is still a draft.  This
% option is also passed to the book class.  Be aware that this option
% also may influence the behavior of other packages, like graphics.
% The final option removes the revision information on the title page
% and is also passed to the book class.
\DeclareOption{draft}{\setboolean{draft}{true}\setboolean{final}{false}}
\DeclareOption{final}{\setboolean{draft}{false}\setboolean{final}{true}}

% Footnotes as endnote (in each chapter).
\DeclareOption{chapnote}{\renewcommand{\footnote}{\endnote}}
\DeclareOption{nochapnote}{\renewcommand{\footnote}{\orgfootnote}}

% Equal size on the left and right margins.
\DeclareOption{nomarginpar}{%
  \setlength{\mymarginparwidth}{0mm}%
  \setlength{\mymarginparsep}{0mm}}
\DeclareOption{marginpar}{%
  \setlength{\mymarginparwidth}{25mm}%
  \setlength{\mymarginparsep}{3mm}}


% The rest of the options are handled by the book class.
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{book}}


% --------------------------------------%--------------------------------------
% --------------------------- Execution of options ----------------------------
% --------------------------------------%--------------------------------------

% A4 paper is our default (I'm an european), and I prefer no marginpar.
\ExecuteOptions{a4paper,nomarginpar,chapnote}

% Process user selected options.
\ProcessOptions


% --------------------------------------%--------------------------------------
% ------------------------------ Package loading ------------------------------
% --------------------------------------%--------------------------------------

% This class is build on top on the LaTeX book class.
\ifthenelse{\boolean{draft}}{\PassOptionsToClass{draft}{book}}{%
  \ifthenelse{\boolean{final}}{\PassOptionsToClass{final}{book}}{}}
\LoadClass{book}

% We use ifthen to test some conditions.
\RequirePackage{ifthen}

% We use Vmargin to handle papertypes and margins,
\RequirePackage{Vmargin}

% and fancyheadings to handle headings and foots.
\RequirePackage{fancyheadings}

% Endnotes for footnotes.
\RequirePackage{endnotes}


% --------------------------------------%--------------------------------------
% --------------------------------- Main code ---------------------------------
% --------------------------------------%--------------------------------------

% Footnotes as endnotes
\def\enoteheading{\section*{\notesname\markright{\notesname}}\leavevmode\par}
\def\enoteformat{\par{\@theenmark}.\hspace*{.8em}}
\def\enotesize{\normalsize}
\@addtoreset{endnote}{chapter}
\def\theendnotes{%
  \ifnum\c@endnote>0
  \begingroup
    \makeatletter
    \def\@doanenote##1##2>{\def\@theenmark{##1}\par\begingroup
        \edef\@currentlabel{\csname p@endnote\endcsname\@theenmark} %DW
        \enoteformat}
    \def\@endanenote{\par\endgroup}%
    \def\ETC.{\errmessage{Some long endnotes will be truncated; %
                            use BIGLATEX to avoid this}%
          \def\ETC.{\relax}}
    \enoteheading
    \enotesize
    \immediate\closeout\@enotes
    \@input{\jobname.ent}%
    \immediate\openout\@enotes=\jobname.ent\relax
  \endgroup\fi}

% Macros for the title missing in the book class.
\newcommand{\subtitle}[1]{\def\@subtitle{#1}}
\newcommand{\thesistype}[1]{\def\@thesistype{#1}}
\newcommand{\institution}[1]{\def\@institution{#1}}
\newcommand{\logo}[2][1]{\def\@logo{#2}\def\@logoscale{#1}}

% Macros not used in this class, but some need them anyway.
\newcommand{\isdnnumber}[1]{\def\@isdnnumber{#1}}
\newcommand{\reporttype}[1]{\def\@reporttype{#1}}
\newcommand{\reportnumber}[1]{\def\@reportnumber{#1}}


% Fonts used in the thesis.
\newcommand{\titlefont}{\huge\bfseries}
\newcommand{\subtitlefont}{\Large}
\newcommand{\revfont}{\small\ttfamily}
\newcommand{\authorfont}{\Large}
\newcommand{\thesistypefont}{\normalsize}
\newcommand{\partfont}{\Huge\bfseries}
\newcommand{\partnumfont}{\huge}
\newcommand{\chapterfont}{\Huge\bfseries}
\newcommand{\chapternumfont}{\huge}
\newcommand{\sectionfont}{\Large\bfseries}
\newcommand{\subsectionfont}{\large\bfseries}
\newcommand{\subsubsectionfont}{\normalsize\bfseries}
\newcommand{\paragraphfont}{\normalsize\bfseries}
\newcommand{\subparagraphfont}{\normalsize\bfseries}
\newcommand{\headingsfont}{\normalsize\bfseries}
\newcommand{\figurenamefont}{\normalsize}
\newcommand{\figuretextfont}{\normalsize}

% The page layout.
\ifthenelse{\lengthtest{\allmargins < 0mm}}{%
  \setlength{\allmargins}{30mm}}{}
\setlength{\marginparwidth}{\mymarginparwidth}
\setlength{\marginparsep}{\mymarginparsep}
\newlength{\mparmargin}\setlength{\mparmargin}{\allmargins}
\newlength{\botommargin}\setlength{\botommargin}{0.5\allmargins}
\addtolength{\mparmargin}{\marginparwidth}
\addtolength{\mparmargin}{\marginparsep}
\setpapersize{\@@paper}
\setmarginsrb{\allmargins}{\allmargins}{\mparmargin}{\botommargin}{%
  1.5\baselineskip}{2\baselineskip}{1.5\baselineskip}{\botommargin}

% Thicknes of rules.
\newlength{\toprulewidth}\setlength{\toprulewidth}{1mm}
\newlength{\bottomrulewidth}\setlength{\bottomrulewidth}{0.5mm}
\setlength{\headrulewidth}{0.4pt}  % fancyheadings

% Headings and foots with fancyheadings.
\pagestyle{fancyplain}
\addtolength{\headwidth}{\marginparsep}
\addtolength{\headwidth}{\marginparwidth}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\hspace{1em}#1}{}}
\lhead[\fancyplain{}{\reset@font\headingsfont\thepage}]{%
  \fancyplain{}{\reset@font\headingsfont\rightmark}}
\rhead[\fancyplain{}{\reset@font\headingsfont\leftmark}]{%
  \fancyplain{}{\reset@font\headingsfont\thepage}}
\cfoot{\fancyplain{\reset@font\headingsfont\thepage}{}}
\chead{}\lfoot{}\rfoot{}

% We split the document in \frontmatter, \mainmatter og \backmatter
% (from the LaTeX book class).
\renewcommand{\frontmatter}{%
  \cleardoublepage
  \@mainmatterfalse\pagenumbering{roman}\pagestyle{plain}}
\renewcommand{\mainmatter}{%
  \cleardoublepage
  \@mainmattertrue\pagenumbering{arabic}\pagestyle{fancyplain}}
\renewcommand{\backmatter}{}

% We have to save the RCS information
\newcommand{\savercsinfo}[1]{%
  \@savercsinfo#1,:}
\def\@savercsinfo#1,{%
  \ifthenelse{\equal{Date}{#1}}{%
    \edef\AADate{\expandafter\@fetchrcsdate \RCSRawDate X}%
    %\edef\AADate{\RCSDate}%
    \edef\AARawDate{\RCSRawDate}%
    }{%
    \expandafter\edef\csname AA#1\endcsname{\csname RCS#1\endcsname}}%
  \@moresavercsinfo}
\def\@moresavercsinfo{\@ifnextchar:{\@donesavercsinfo}{\@savercsinfo}}
\def\@donesavercsinfo:{}
% This is a hack:
\def\@fetchrcsdate#1/#2/#3 #4X{%
  \ifcase#2\or
  January\or February\or March\or April\or May\or June\or
  July\or August\or September\or October\or November\or December\fi
  \space\aa@eatzero#3, #1}
\def\aa@eatzero#1#2,{\if0#1#2,\else#1#2,\fi}

% Generating the title (based on \maketitle from the LaTeX book class)
\newlength{\titlewidth}
\setlength{\titlewidth}{\textwidth}
\addtolength{\titlewidth}{\marginparsep}
\addtolength{\titlewidth}{\marginparwidth}
\renewcommand{\maketitle}{%
  \begin{titlepage}
    \let\footnotesize\small
    \let\footnoterule\relax
    \def\thanks##1{\typeout{Warning: thanks doesn't work in aathesis.cls!}}%
    \ifthenelse{\boolean{final}}{}{%
      \@ifundefined{AARawDate}{}{%
        \centerline{{\reset@font\revfont%
            \@ifundefined{AAState}{}{\AAState: }%
            \@ifundefined{AARCSfile}{}{\AARCSfile\ }%
            \@ifundefined{AARevision}{}{\AARevision\ }%
            \AARawDate\ %
            \@ifundefined{AAAuthor}{}{\AAAuthor}}}}}
    \vspace*{\fill}
    \noindent\makebox[0pt][l]{\parbox{\titlewidth}{%
        \begin{center}
          \begin{minipage}[b][0.9\textheight][s]{0.9\titlewidth}
            \begin{center}
              \rule{\linewidth}{\toprulewidth} \par
              \vspace*{\stretch{0.5}}%
              {\reset@font\titlefont \@title \par}%
              \@ifundefined{@subtitle}{}{%
                \vskip 3ex              
                {\reset@font\subtitlefont \@subtitle \par}}%
              \vspace*{\stretch{0.5}}
              \rule{\linewidth}{\bottomrulewidth} \par
              \vspace*{\stretch{1}}
              {\reset@font\authorfont
                \lineskip .75em%
                \begin{tabular}[t]{c}
                  \@author
                \end{tabular}\par}%
              \vskip 1.5em%
              {\reset@font\authorfont \@ifundefined{AADate}{\@date}{%
                  \AADate}\par}%
              \vspace*{\stretch{1}}
              {\reset@font\thesistypefont \@thesistype \par}
              \vspace*{\stretch{0.5}}
              \@ifundefined{@logo}{}{\scalebox{\@logoscale}%
                {\@logo} \par
                \vspace*{\stretch{0.5}}}
              {\reset@font\authorfont \@institution \par}
            \end{center}
          \end{minipage}
        \end{center}\par}}
    \vspace*{\fill}
  \end{titlepage}%
  \setcounter{footnote}{0}%
  \let\thanks\relax\let\maketitle\relax
  %\gdef\@thanks{}\gdef\@author{}\gdef\@title{}
  }

% Table of contents, list of figures, list of tables and bibiography
% listing in the table of contents listing.
\let\oldtoc\tableofcontents
\def\tableofcontents{\oldtoc
  \addcontentsline{toc}{chapter}{\contentsname}}
\let\oldlof\listoffigures
\def\listoffigures{\oldlof
  \addcontentsline{toc}{chapter}{\listfigurename}}
\let\oldlot\listoftables
\def\listoftables{\oldlot
  \addcontentsline{toc}{chapter}{\listtablename}}
%\let\oldbib\bibliography
%\renewcommand{\bibliography}[1]{\oldbib{#1}
%  \addcontentsline{toc}{chapter}{\bibname}}
\AtBeginDocument{\@ifundefined{listof}{}{%
    \let\oldlo\listof
    \renewcommand\listof[2]{\oldlo{#1}{#2}%
      \addcontentsline{toc}{chapter}{#2}}}}

% We change the layout of both numbered 
\def\@part[#1]#2{%
  \def\@partname{#2}%
  \ifnum \c@secnumdepth >-2\relax
    \refstepcounter{part}%
    \addcontentsline{toc}{part}{\thepart\hspace{1em}#1}%
  \else
    \addcontentsline{toc}{part}{#1}%
  \fi
  \markboth{}{}
  \noindent\makebox[0pt][l]{\parbox{\titlewidth}{\centering
      \parbox{0.9\titlewidth}{\centering
        \rule{\linewidth}{\toprulewidth} \par
        \vspace*{50\p@}%
        \interlinepenalty \@M
        \ifnum \c@secnumdepth >-2\relax
        {\reset@font\partnumfont \partname~\thepart}
        \par
        \vskip 20\p@
        \fi
        \reset@font\partfont #2\par\nobreak
        \vskip 40\p@
        \rule{\linewidth}{\bottomrulewidth}\par\nobreak}}}%
  \@endpart}

% and unnumered parts.
\def\@spart#1{%
  \def\@partname{#1}%
  \noindent\makebox[0pt][l]{\parbox{\titlewidth}{\centering
      \parbox{0.9\titlewidth}{\centering
        \rule{\linewidth}{\toprulewidth} \par
        \vspace*{50\p@}%
        \interlinepenalty \@M
        \reset@font\partfont #1\par\nobreak
        \vskip 40\p@
        \rule{\linewidth}{\bottomrulewidth}\par\nobreak}}}%
  \@endpart}

% We change the layout of numered
\def\@makechapterhead#1{%
  \def\@chapname{#1}%
  {\parindent \z@ \raggedright
    \makebox[0pt][l]{%
      \parbox{\titlewidth}{%
        \rule{\titlewidth}{\toprulewidth} \par
        \vspace*{50\p@}%
        \ifnum \c@secnumdepth >\m@ne
          \if@mainmatter
            {\reset@font\chapternumfont \@chapapp{} \thechapter}
            \par\nobreak
            \vskip 20\p@
          \fi
        \fi
        \interlinepenalty\@M
        \reset@font\chapterfont #1\par\nobreak
        \vskip 40\p@
        \rule{\titlewidth}{\bottomrulewidth}}}
    \par\nobreak
    \vskip 30\p@
    }}

% and unumered chapters too.
\def\@makeschapterhead#1{%
  \def\@chapname{#1}%
  {\parindent \z@ \raggedright
    \makebox[0pt][l]{%
      \parbox{\titlewidth}{%
        \rule{\titlewidth}{\toprulewidth} \par
        \vspace*{50\p@}%
        \interlinepenalty\@M
        \reset@font\chapterfont #1\par\nobreak
        \vskip 40\p@
        \rule{\titlewidth}{\bottomrulewidth}}}
    \par\nobreak
    \vskip 30\p@
    }}

% Here can we change the layout of sections.
\renewcommand{\section}{\@startsection {section}{1}{\z@}%
  {-3.5ex \@plus -1ex \@minus -.2ex}%
  {2.3ex \@plus.2ex}%
  {\reset@font\sectionfont}}
\renewcommand{\subsection}{\@startsection{subsection}{2}{\z@}%
  {-2.75ex\@plus -.75ex \@minus -.2ex}%
  {1.5ex \@plus .2ex}%
  {\reset@font\subsectionfont}}
\renewcommand{\subsubsection}{\@startsection{subsubsection}{3}{\z@}%
  {-2.75ex\@plus -.75ex \@minus -.2ex}%
  {1.5ex \@plus .2ex}%
  {\reset@font\subsubsectionfont}}
\renewcommand{\paragraph}{\@startsection{paragraph}{4}{\z@}%
  {1.75ex \@plus.5ex \@minus.2ex}%
  {-1em}%
  {\reset@font\paragraphfont}}
\renewcommand{\subparagraph}{\@startsection{subparagraph}{5}{\parindent}%
  {1.75ex \@plus.5ex \@minus.2ex}%
  {-1em}%
  {\reset@font\subparagraphfont}}

% The bibliography layout (from book: just changed chapter* to chapter).
\renewenvironment{thebibliography}[1]
     {\chapter{\bibname}%
      \list{\@biblabel{\@arabic\c@enumiv}}%
           {\settowidth\labelwidth{\@biblabel{#1}}%
            \leftmargin\labelwidth
            \advance\leftmargin\labelsep
            \@openbib@code
            \usecounter{enumiv}%
            \let\p@enumiv\@empty
            \renewcommand\theenumiv{\@arabic\c@enumiv}}%
      \sloppy
      \clubpenalty4000
      \@clubpenalty \clubpenalty
      \widowpenalty4000%
      \sfcode`\.\@m}
     {\def\@noitemerr
       {\@latex@warning{Empty `thebibliography' environment}}%
      \endlist}
