%
% $Id: kasper.cls,v 1.52 2004/02/29 16:32:36 kp229 Exp $
%
% LaTeX class file for typesetting of papers and notes. Can combine
% multiple .tex files into one. Plus several macros to build a title-page, to
% typeset numbered examples, intermezzos and exercises.
%
% Kasper Peeters <k.peeters@damtp.cam.ac.uk>
%

\NeedsTeXFormat{LaTeX2e} \ProvidesClass{kasper}[2001/08/05]
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}

\def\@clock{{\count0=\time
           \divide\count0 60
           \ifnum\count0<10 0\fi\the\count0
           \multiply\count0 -60 \advance\count0 \time
           :\ifnum\count0<10 0\fi \the\count0
         }}

\newif\if@preprint  % whether or not the preprint number should be displayed
\newif\if@comments  % whether or not to print comments
\def\@kptsize{12pt}
\DeclareOption{12pt}
   {\renewcommand{\@kptsize}{12pt}}
\DeclareOption{11pt}
   {\renewcommand{\@kptsize}{11pt}}
\DeclareOption{10pt}
   {\renewcommand{\@kptsize}{10pt}}
\DeclareOption{final}
   {\def\@preprint{~}%
    \@preprinttrue
    \@commentsfalse
    \def\@timestamp{\@date}}
\DeclareOption{draft}
   {\def\@preprint{~}%
    \@preprinttrue
    \@commentstrue
    \def\@timestamp{{\tt \jobname.tex}\\\@date, \@clock}}
\DeclareOption{notes}
   {\def\@preprint{}%
    \@preprintfalse
    \@commentstrue
    \def\@timestamp{{\tt \jobname.tex}\\\@date, \@clock}}
\ExecuteOptions{final}
\ProcessOptions\relax

\author{}
\LoadClass[\@kptsize]{article}

% Include several standard LaTeX packages. 

\usepackage[verbose,textwidth=478.00812pt,textheight=693.53699pt]{geometry}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{latexsym}
\usepackage{amssymb}
\usepackage[draft]{hyperref}
\usepackage[final]{graphicx}
\usepackage{tabularx}
\usepackage{bbold}
\usepackage{verbatim}
% \usepackage{fancyvrb}
\usepackage[numbers]{natbib}
\usepackage[bf,small]{caption}
\setlength{\captionmargin}{20pt}

% The alignment spacing changed in AMS-LaTeX 2.0; we set
% it back to its old value.
\renewcommand{\minalignsep}{0pt}

\setlength{\skip\footins}{15pt plus 4pt minus 2 pt}

% Title page definitions for papers and preprints.

\def\@email{}
\newcommand{\email}[1]{\def\@email{#1}}
\newcommand{\preprintnumber}[1]{\def\@preprint{#1}}
\newcommand{\address}[2]{\expandafter\def\csname @address#1\endcsname{#2}}
\newcommand{\onleave}[2]{\expandafter\def\csname @onleave#1\endcsname{#2}}
\newcommand{\nofootnotemark}{\let\@fnbackup\@makefnmark\let\@makefnmark\relax}
\newcommand{\yesfootnotemark}{\let\@makefnmark\@fnbackup}
\def\maketitle{\@ifundefined{@address1}{}{\setcounter{page}{0}\pagestyle{empty}}\@maketitle}
\def\@maketitle{%
  \newpage
  \@ifundefined{@onleave1}{}{\nofootnotemark\footnote[1]{${}^1$ \csname @onleave1\endcsname}\yesfootnotemark}
  \@ifundefined{@onleave2}{}{\nofootnotemark\footnote[2]{${}^2$ \csname @onleave2\endcsname}\yesfootnotemark}
  \@ifundefined{@onleave3}{}{\nofootnotemark\footnote[3]{${}^3$ \csname @onleave3\endcsname}\yesfootnotemark}
  \@ifundefined{@onleave4}{}{\nofootnotemark\footnote[4]{${}^4$ \csname @onleave4\endcsname}\yesfootnotemark}
  \null
  \vskip 2em%
   \begin{flushright}
   \if@preprint
   {\tt \@preprint}\\[2ex]
   \fi
   \@timestamp
   \end{flushright}
  \vskip 2em%
  \begin{center}%
    {\LARGE\bf \@title \par}%
    \ifx\@author\@empty{}\else{
    \vskip 4ex%
    {\large
      \lineskip .5em%
      \normalsize
      \begin{tabular}[t]{c}%
         \large\bf\@author
         \@ifundefined{@address1}{\\[1ex]}{\\[4ex]
         \@ifundefined{@address2}{}{$^1$\!}
         \begin{tabular}[t]{@{}l@{}}
         \csname @address1\endcsname
         \end{tabular}}
         \@ifundefined{@address2}{}{
         \;\;$^2$\!
         \begin{tabular}[t]{@{}l@{}}
         \csname @address2\endcsname
         \end{tabular}}
         \@ifundefined{@address3}{}{
         \;\;$^3$\!
         \begin{tabular}[t]{@{}l@{}}
         \csname @address3\endcsname
         \end{tabular}}
         \@ifundefined{@address4}{}{
         \;\;$^4$\!
         \begin{tabular}[t]{@{}l@{}}
         \csname @address4\endcsname
         \end{tabular}}\@ifundefined{@address1}{}{\\
         \\}
         {\tt \@email}
      \end{tabular}\par
  }}\fi
  \end{center}
  \par
  \vskip 1.5em}


\newcommand{\maketoc}{\@ifundefined{@address1}{}{\vfill\eject\setcounter{page}{2}\pagestyle{plain}}
\hrule
\tableofcontents
\bigskip\bigskip
\hrule
}

% \def\dollarcode{\futurelet\next\checkdsp}
% \begingroup
% \lccode`\~=$
% \lowercase{\endgroup\let~}\dollarcode
% \def\checkdsp{\ifx\next\dollarcode\expandafter\dspcomplain
%               \else\expandafter$\fi}
% \def\dspcomplain#1{%
%   \PackageWarning{mypackage}{I don't want $$,  I'll take \string\[.}
%   \ifmmode \expandafter \]\else \expandafter \[\fi}
% \AtBeginDocument{\catcode`\$=13 }

\newcommand{\eqn}[1]{(\ref{#1})}  
\newcommand{\eqnf}[2]{\@ifundefined{r@#1}{(see #2)}{(\ref{#1})}}
\newcommand{\dcite}[1]{{\citeauthor{#1}~\cite{#1}}}
\newcommand{\ddcite}[2]{{\citeauthor{#1}~\cite{#1,#2}}}
\newcommand{\rep}[1]{{\bf #1}}
\newcommand{\crep}[1]{\overline{\bf #1}}
\newcommand{\bfcomment}[2]{\if@comments{{\bf [#1: #2]}}\fi}
% the verbatim package clashes with comment, so here's a backup
\newcommand{\kcomment}[2]{\if@comments{{\bf [#1: #2]}}\fi}

\newcommand{\rmd}{{\rm d}}
\newcommand{\tighter}{\addtolength{\itemsep}{-.5\baselineskip}}

\renewcommand{\contentsname}{Table of Contents}

\newenvironment*{talk}{\makeatletter
 \renewcommand\@seccntformat[1]{\relax}
 \let\old@enumerate\enumerate
 \renewcommand{\enumerate}{\old@enumerate\tighter}
 \makeatother
\allsectionsfont{\sffamily}}%
{\allsectionsfont{\rmfamily}
 \let\enumerate\old@enumerate}

\newenvironment*{related}{\begin{enumerate}}{\end{enumerate}}


% Environment masterfile should be used in files that include
% several other LaTeX documents. It takes care of stripping off
% the \documentclass, \usepackage and \begin{document} lines.
% You have to make sure that the master file contains all the
% \usepackage statements of the sub-files.

\newenvironment*{masterfile}{
  \renewcommand\documentclass[2][]{\relax}
  \renewcommand\usepackage[2][]{\relax}
  \makeatletter
  \let\old@maketoc\maketoc
  \let\old@tableofcontents\tableofcontents
  \let\old@document\document
  \let\old@enddocument\enddocument
  \let\old@bibliography\bibliography
  \renewcommand{\maketoc}{\relax}
  \renewcommand{\tableofcontents}{\relax}
  \renewcommand{\document}{\sectionunit}
  \renewcommand{\enddocument}[2]{\endsectionunit}
  \renewcommand{\bibliography}[1]{}}%
{\makeatletter
  \let\maketoc\old@maketoc
  \let\tableofcontents\old@tableofcontents
  \let\enddocument\old@enddocument
  \let\bibliography\old@bibliography
  \makeatother}

% Screen environment, for display of verbatim material.

\newenvironment{screen}{\def\verbatim@font{\smaller\smaller\ttfamily}%
  \def\verbatim@processline{\hskip 5ex\the\verbatim@line\par}%
  \verbatim}{\endverbatim}

% Environment sectionunit automatically takes care of nested sections 
% and their numbering.

\let\LT@title\maketitle
\newenvironment*{sectionunit}[1][1]
  {\let\@author\@empty
   \let\@title\@empty
   \def\@headtitle{\@title}%
   \def\@toctitle{\@title}%
   \let\maketitle\SU@maketitle
   \let\title\SU@title
   \let\toctitle\SU@toctitle
   \let\headtitle\SU@headtitle
   \advance\@secdepth#1\relax
%
  \edef\sectioncommand{%
  \ifnum\@secdepth=\m@ne
    \noexpand\part
  \else
    \expandafter\noexpand\ifcase\@secdepth
      \chapter\or
      \section\or
      \subsection\or
      \subsubsection\or
      \paragraph\or
      \subparagraph\else
      \deepsection
     \fi
   \fi}}%
  {}

\newenvironment*{sectionunit*}[1][1]
  {\let\@author\@empty
   \let\@title\@empty
   \def\@headtitle{\@title}%
   \def\@toctitle{\@title}%
   \let\maketitle\SU@maketitle
   \let\title\SU@title
   \let\toctitle\SU@toctitle
   \let\headtitle\SU@headtitle
   \advance\@secdepth#1\relax
%
  \edef\sectioncommand{%
  \ifnum\@secdepth=\m@ne
    \noexpand\part
  \else
    \expandafter\noexpand\ifcase\@secdepth
      \chapter*\or
      \section*\or
      \subsection*\or
      \subsubsection*\or
      \paragraph*\or
      \subparagraph*\else
      \deepsection
     \fi
   \fi}}%
  {}

\newcount\@secdepth

\def\SU@maketitle{%
  \@ifstar
   {\sectioncommand*[\@toctitle]{\@title}\@gobble}%
   {\sectioncommand[\@toctitle]{\@title}}%
}

\def\SU@title#1{\def\@title{#1}}
\def\SU@toctitle#1{\def\@toctitle{#1}}
\def\SU@headtitle#1{\def\@headtitle{#1}}
\def\SU@author#1{\def\@author{#1}}
\def\deepsection#1]#2{\par#2 }


% Sections as appendices.
%

%\newcounter {secapp}[section]  =
%\setcounter{secapp}{0}
%\newcommand{\secappmark}[1]{}
%\renewcommand\thesecapp       {\Alph{secapp}\thechapter}
%\newcommand\secapp{\@startsection {secapp}{1}{\z@}%
%                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
%                                   {2.3ex \@plus.2ex}%
%                                   {\reset@font\Large\bfseries}}
%\newcommand\l@secapp      {\@dottedtocline{1}{1.5em}{2.3em}}
%\@addtoreset{equation}{secapp}
%\renewcommand\theequation{\thesecapp.\arabic{equation}}


% Environments example, intermezzo and exercise for handling of
% numbered blocks of text typeset in smaller font.

\@ifundefined{chapter}{\def\k@lablformat{\thesection.}
                       \@addtoreset{examplcount}{section}}
                      {\def\k@lablformat{\thechapter.}
                       \@addtoreset{examplcount}{chapter}
                      }
\def\k@stepcounter#1{\stepcounter
    {#1}\edef\@currentlabel{\csname p@#1\endcsname\k@lablformat
    \csname the#1\endcsname}}

\@definecounter{examplcount}
\@definecounter{intermcount}
\@definecounter{exerccount}

\newcommand\txttolabel{0.2em}
\newcommand\labeltoexm{3em}
%
\@ifundefined{chapter}{\def\lablformat{\thesection.}
                       \@addtoreset{examplcount}{section}
                       \@addtoreset{intermcount}{section}
                       \@addtoreset{exerccount}{section}}
                      {\def\lablformat{\thechapter.}
                       \@addtoreset{examplcount}{chapter}
                       \@addtoreset{intermcount}{chapter}
                       \@addtoreset{exerccount}{chapter}
                      }
\def\myrefstepcounter#1{\stepcounter                                           
    {#1}\edef\@currentlabel{\csname p@#1\endcsname\lablformat
    \csname the#1\endcsname}}        
\def\smallsec#1#2{

   \parskip=-20pt%
   \myrefstepcounter{#2}%
        \vtop to 0pt{\hfill\vtop to 0pt{\vskip \txttolabel\hbox{\vrule height 1pt width 2.4cm}%
            \vskip -0.4em\hbox{\small #1~
                           {\lablformat}\arabic{#2}}%
                           \vss}%
                        }%
        \vskip \labeltoexm\nobreak%
        \list{}{\listparindent=0cm%
                \itemindent\listparindent%
               }%
   \item[] \small}
\newcommand\example{\smallsec{Example}{examplcount}}
\newcommand\intermezzo{\smallsec{Intermezzo}{intermcount}}
\newcommand\exercise{\smallsec{Exercise}{exerccount}}
\let\endexample\endlist
\let\endintermezzo\endlist
\let\endexercise\endlist

% Environment `todo'.

\@definecounter{todocount}

\newcommand\todo{\list{}{\listparindent=0cm
                         \itemindent\listparindent
                        }
                 \item[] \small\sf
}
\let\endtodo\endlist



% Finally, turn on the bibliography style.

\bibliographystyle{kasper}

