%%% -*-latex-*-
%%%
%%% Definitions for IPS course

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{ips}[2001/10/02]

%%%----- Document class stuff -----------------------------------------------

\newif\ifipsslides \ipsslidesfalse
\newif\ifipsanswers \ipsanswerstrue
\InputIfFileExists{ips.cfg}\relax\relax

\ifipsslides
  \PassOptionsToClass{a4, slidesonly}{seminar}
\else
  \PassOptionsToClass{a4, article, twoside}{seminar}
\fi
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{seminar}}
\ProcessOptions
\LoadClass{seminar}

%%%----- Required packages --------------------------------------------------

\RequirePackage[palatino, helvetica, courier, maths=cmr]{mdwfonts}
\RequirePackage{fancyhdr}
\RequirePackage[T1]{fontenc}
\RequirePackage{mdwtab, mathenv, crypto, mdwthm, mdwmath}
\RequirePackage[within=section]{exercise}
\RequirePackage{amssymb}
\RequirePackage{url}

\ifx\pdfoutput\@@undefined
  \PassOptionsToPackage{dvips}{xy}
\else
  \ifarticle
    \pdfpagewidth\paperwidth
    \pdfpageheight\paperheight
  \else
    \pdfpagewidth\paperheight
    \pdfpageheight\paperwidth
  \fi
\fi
\RequirePackage[all]{xy}

%%%----- Miscellaneous settings ---------------------------------------------

\errorcontextlines=999

%%%----- Style choices ------------------------------------------------------

\renewcommand{\qedsymbol}{$\square$}
\def\hex#1{\texttt{0x#1}}
\let\emptystring\lambda
\let\epsilon\varepsilon
\let\phi\varphi
\let\emptyset\varnothing
\bibliographystyle{mdwalpha}
\sloppy

%%%----- Exercise typesetting -----------------------------------------------

\exctrcheck{section}{\answrite{\protect\subsection*{Section \thesection}}}

%%%----- Diagram typesetting ------------------------------------------------

\turnradius={4pt}

\def\ocat{
  \setbox\z@\hbox{$\scriptstyle\|$}%
  \dimen8\ht\z@\advance\dimen8\dp\z@%
  \mathbin{\xy *=<\dimen8>[o][F]{\box\z@} \endxy}%
}

%%%------ Sectioning and headings -------------------------------------------

\ifarticle
  \fancyhf{}
  \fancyhead[LE]{\textbf{\nouppercase{\leftmark}}}
  \fancyhead[RO]{\textbf{\nouppercase{\rightmark}}}
  \fancyfoot[LE, RO]{\thepage}
  \def\headrulewidth{\z@}
  \fancypagestyle{plain}{\fancyhead{}}
  \def\sectionmark#1{\markboth{\thesection.\ #1}{}}
  \def\subsectionmark#1{\markright{\thesubsection.\ #1}}
  \let\emptyslide\relax
  \let\topic\@gobble
  \pagestyle{fancy}
\else
  \def\sectionmark#1{\markboth{#1}{}}
  \def\subsectionmark#1{\markright{ -- #1}}
  \newcount\@emptyslide
  \newpagestyle{slide}%
    {\ifnum\@emptyslide=\c@slide\else\textbf{\thetitle}\hfil\fi}%
    {\ifnum\@emptyslide=\c@slide\else\leftmark\rightmark\hfil\theslide\fi}
  \slidepagestyle{slide}
  \def\emptyslide{\global\@emptyslide\c@slide}%
  \def\@sect#1#2#3#4#5#6[#7]#8{%
    \ifnum #2>\c@secnumdepth\else%
      \refstepcounter{#1}%
    \fi%
    \csname #1mark\endcsname{#7}%
    \csname #1summary\endcsname{#7}%
  }
  \def\@ssect#1#2#3#4#5{\relax}
  \newtoks\sm@toks
  \newcount\sm@count
  \def\sm@auxwrite#1#2{\immediate\write\@auxout
    {\noexpand\sm@entry{\the\sm@count}{#1}{#2}}}
  \def\sectionsummary{%
    \global\advance\sm@count\@ne%
    \expandafter\let\expandafter\@next%
      \csname sm@summary@\the\sm@count\endcsname%
    \ifx\@next\relax\else\@next\sm@endsummary\fi%
    \sm@auxwrite{section}%
  }
  \def\subsectionsummary{\sm@auxwrite{subsection}}
  \def\sm@entry#1#2#3{%
    \begingroup%
      \let\protect\@unexpandable@protect%
      \expandafter\ifx\csname sm@summary@#1\endcsname\relax%
        \expandafter\let\csname sm@summary@#1\endcsname\@empty%
      \fi%
      \expandafter\xdef\csname sm@summary@#1\endcsname{%
        \csname sm@summary@#1\endcsname%
        \expandafter\protect\csname sm@do#2\endcsname{#3}%
      }%
    \endgroup%
  }
  \def\sm@dosection#1{%
    \begingroup\slide%
    \emptyslide%
    \noindent\vadjust{}%
    \vskip10\p@%
    \centerline{\sffamily\bfseries\Large #1 -- overview}
    \vskip10\p@%
    \begingroup\itemize
  }
  \def\sm@endsummary{\enditemize\endgroup\endslide\endgroup}
  \def\sm@dosubsection#1{\item\relax\@tempswatrue#1}
  \def\sm@dotopic#1{\if@tempswa:\@tempswafalse\else;\fi\space#1}
  \def\topic{\sm@auxwrite{topic}}
\fi

\newcounter{sequence}
\def\thesequence{\arabic{sequence}}
\def\seq{\stepcounter{sequence}\thesequence}
\def\resetseq{\setcounter{sequence}{0}}

\def\head#1{{\sffamily\bfseries\large #1}\par}

%%%------ Page layout for notes ---------------------------------------------

\ifarticle
  \raggedbottom
  \def\slide@clearpage{\if@nobreak\else\par\penalty\z@\fi}
\fi

%%%----- Slide layout -------------------------------------------------------

\slidewidth=260mm
\slideheight=160mm

\slideplacement{here}
\slideframewidth=1pt
\centerslidesfalse

\def\slidefuzz{20pt}

\def\slideleftmargin{15mm} \let\sliderightmargin=\slideleftmargin
\def\slidetopmargin{15mm} \let\slidebottommargin=\slidetopmargin

\def\slidearraystretch{1}

%%%----- Hacking xcomment ---------------------------------------------------
%%%
%%% Supports \xcalways...\x which is an escape from the usual commenting.

\begingroup
\catcode`\!=12\catcode`\[=12\catcode`\]=12\catcode`\"=12
\lccode`\!=`\\\lccode`\[=`\{\lccode`\]=`\}\lccode`\"=`\%
\catcode`\~=\active\lccode`\~=`\^^M
\lowercase{
  %%
  %% Hack the main loop code
  %%
  \gdef\xcomment@@@#1\@nil{%
    \def\@tempa{#1}%
    \ifx\@tempa\@empty
      \let\next\xcomment@
    \else
      \def\next{\xcomment@@#1\@nil}%
      \@testtrue
      \xc@checkbegin#1\relax begin[]\relax\relax
      \if@test
        \xc@checkend#1\relax end[]\relax\relax
        \if@test
          \xc@checkinput#1\relax input[]\relax\relax
          \if@test
            \xc@checkinclude#1\relax include[]\relax\relax
            \if@test
              \xc@checkendinput#1\relax endinputss\relax\relax
              \if@test
                \xc@checkalways#1\relax xcalwaysfoo!x\relax\relax
    \fi\fi\fi\fi\fi\fi
    \next}
  %%
  %% New command support
  %%
  \gdef\xc@checkalways#1xcalways#2!x#3\relax#4\relax{%
    \def\@tempa{#1}%
    \ifx\@tempa\@empty%
      \@testfalse%
      \endgroup%
      \toks@{#2}\rescan\toks@%
      \def\next{\the\toks@\xc@always#3\@nil}%
    \fi%
  }
}
\endgroup

\def\xc@always#1\@nil{\xc@begin#1}
\def\xcalways#1\x{#1}

%%%----- Bugfix -------------------------------------------------------------

\def\@bugfix#1\let\@listi#2#3\@@%
  {\def\normalsize{#1#3}}
\expandafter\@bugfix\normalsize\@@

\toks@\expandafter{\slidebox@restore}
\edef\slidebox@restore{\the\toks@\advance\topsep\parskip}

\ifarticle
  \def\@bugfix#1\penalty\z@#2\@@{\def\outputloop@savedslides{#1#2}}
  \expandafter\@bugfix\outputloop@savedslides\@@
  \toks@\expandafter{\output@savedslide}
  \def\@strange@hack{\begingroup\output{\unvbox\@cclv}\penalty-\@M\endgroup}
  \edef\output@savedslide{\noexpand\@strange@hack\the\toks@}
\fi

\def\@makeslide{%
  \setbox\@slidebox\vbox{%
    \@begindvi % added 1997/04/15 SPQR
    \unvbox\@cclv
    \ifvoid\slide@footins\else
      \vskip\skip\slide@footins
      \vfil
      \footnoterule
      \unvbox\slide@footins
      \unskip
    \fi
    \vskip\z@}}

\def\@makecol{%
   \setbox\@outputbox \box\@cclv
   \xdef\@freelist{\@freelist\@midlist}%
   \global \let \@midlist \@empty
   \@combinefloats
   \ifvoid\footins \else
     \setbox\@outputbox \vbox {%
       \boxmaxdepth \@maxdepth                   
       \unvbox \@outputbox
       \vskip \skip\footins
       \vfil
       \footnoterule
       \unvbox \footins
       \unskip
       }%
   \fi
   \ifvbox\@kludgeins
     \@makespecialcolbox
   \else
     \setbox\@outputbox \vbox to\@colht {%
       \@texttop
       \dimen@ \dp\@outputbox
       \unvbox \@outputbox
       \vskip -\dimen@
       \@textbottom
       }%
   \fi
   \global \maxdepth \@maxdepth
}

%%%----- That's all, folks --------------------------------------------------

\endinput
