% kw article class

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{kwart}
\newif\if@justify
\DeclareOption{justify}{\@justifytrue}
\DeclareOption{nojustify}{\@justifyfalse}
\newif\if@tuftestyle
\DeclareOption{tuftestyle}{\@tuftestyletrue}
\DeclareOption{notuftestyle}{\@tuftestylefalse}
\newif\if@includemarginpar
\DeclareOption{includemarginpar}{\@includemarginpartrue}
\DeclareOption{noincludemarginpar}{\@includemarginparfalse}
\newif\if@wider
\DeclareOption{wider}{\@widertrue}
\DeclareOption{nowider}{\@widerfalse}
\DeclareOption{draft}{\def\@textbottom{\leaders\vrule width 1pc\vskip -0.1pt plus.00001fil\relax}\let\@texttop\relax}
\DeclareOption{final}{\raggedbottom}
\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\ExecuteOptions{letterpaper,10pt,justify,notuftestyle,noincludemarginpar}
\ProcessOptions
\LoadClass{article}

\if@includemarginpar\@mparswitchfalse\fi

\long\def\junk#1\endjunk{}

% My style is all about a regular, consistent, uniform look.
% This means:
% - lines are separated by the same amount of space.  No
%   extra space between paragraphs.  No stretching or shrinking
%   vertically to justify the text to the page height.
% - words are very rarely hyphenated.
% - words are separated by very similar amounts of space (in
%   unjustified mode).
% - no extra space at the ends of sentences.

%%% Font change commands

% For ragged right, there's a three-way tradeoff between
%   1. Raggedness
%   2. Frequency of hyphenations
%   3. Irregularity of spacing
% The idea is to make spacing much more regular than in justified text.
% Normally space is (30+15-10)/90 em units.
% Here I set (30+1-4)/90.
% There are usually about a dozen spaces in a line, so the total interword
% stretch ends up being about (+12-48)/90.

\if@justify
\def\set@ragged{}
\else
\def\set@ragged{%
   \@rightskip 1.25pc plus 1.25pc minus 1.25pc\rightskip\@rightskip
   \spaceskip .33333em plus .03333em minus .03333em
%  \@rightskip 0pc plus 2.5pc\rightskip\@rightskip
%  \spaceskip .33333em plus .01111em minus .04444em
   \xspaceskip\spaceskip
}
% FIXME: probably need to do this to the \maketitle variant in article.cls too
\renewcommand\@makefntext[1]{%
  \parindent1em%
  \noindent
  \set@ragged
  \hb@xt@1.8em{\hss\@makefnmark}#1}
\fi

\AtBeginDocument{
\DeclareFontShape{OT1}{cmr}{m}{n}{
  <-6> cmr5
  <6-7> cmr6
  <7-8> cmr7
  <8-9> cmr8
  <9-10> cmr9
  <10-12> cmr10
  <12-17> cmr12
  <17-> cmr17}{}
\DeclareFontShape{OT1}{cmr}{m}{it}{
  <-8> cmti7
  <8-9> cmti8
  <9-10> cmti9
  <10-12> cmti10
  <12-> cmti12}{}
\DeclareFontShape{OT1}{cmr}{m}{sc}{
  <-> cmcsc10}{}

\DeclareFontShape{OML}{cmm}{m}{it}{
  <-6> cmmi5
  <6-7> cmmi6
  <7-8> cmmi7
  <8-9> cmmi8
  <9-10> cmmi9
  <10-12> cmmi10
  <12-> cmmi12}{}
\DeclareFontShape{OMS}{cmsy}{m}{n}{
  <-6> cmsy5
  <6-7> cmsy6
  <7-8> cmsy7
  <8-9> cmsy8
  <9-10> cmsy9
  <10-> cmsy10}{}
\DeclareFontShape{OMX}{cmex}{m}{n}{
  <-> cmex10}{}

\DeclareFontFamily{U}{msa}{}
\DeclareFontShape{U}{msa}{m}{n}{
  <-7> msam5
  <7-10> msam7
  <10-> msam10}{}
\DeclareFontFamily{U}{msb}{}
\DeclareFontShape{U}{msb}{m}{n}{
  <-7> msbm5
  <7-10> msbm7
  <10-> msbm10}{}

\DeclareFontFamily{T1}{htx}{}
\DeclareFontShape{T1}{htx}{m}{n}{
  <-6> htxr
  <6-7> htxr
  <7-8> htxr
  <8-9> htxr
  <9-10> htxr
  <10-12> htxr
  <12-17> htxr
  <17-> htxr}{}
\DeclareFontShape{T1}{htx}{m}{it}{
  <-8> htxi
  <8-9> htxi
  <9-10> htxi
  <10-12> htxi
  <12-> htxi}{}
\DeclareFontShape{T1}{htx}{m}{sc}{
  <-> htxrcsc}{}

%% \fontencoding{T1}\fontfamily{htx}\selectfont

% BUG: For its own inexplicable reasons, LaTeX starts screwing
% up the floats if the size is set to 10.5 rather than 10...
% What the fuck?
\newcommand\oldstyle{\usefont{OML}{cmm}{m}{it}}
\renewcommand\Huge{\@setfontsize\Huge\@xxvpt{30}\set@ragged}
\renewcommand\huge{\@setfontsize\huge\@xxpt{25}\set@ragged}
\renewcommand\LARGE{\@setfontsize\LARGE\@xviipt{22}\set@ragged}
\renewcommand\Large{\@setfontsize\Large\@xivpt{18}\set@ragged}
\renewcommand\large{\@setfontsize\large{11.5}{14}\set@ragged}
\renewcommand\normalsize{%
% If you change this change \topskip and \baselineskip, below
   \@setfontsize\normalsize{10.5}{14}%
   \set@ragged
   \abovedisplayskip 4pt plus 1pt minus .5pt
   \abovedisplayshortskip \abovedisplayskip
% FIXME: problem here that the short line needs a strut.  actually any
% previous line should get a strut.
   \advance\abovedisplayshortskip-\baselineskip
   \belowdisplayskip \abovedisplayskip
   \belowdisplayshortskip \abovedisplayskip
% Correct for strut added by \[
   \advance\abovedisplayskip-.2ex
   \advance\abovedisplayshortskip-.2ex
% Ordinarily there will be 4pt between the top of a parenthesis
% and the bottom of a paren in the previous line
   \lineskip 3pt
   \lineskiplimit\lineskip
   \normallineskip\lineskip
   \normallineskiplimit\lineskiplimit
   \let\@listi\@listI}
\normalsize
\renewcommand\small{%
   \@setfontsize\small\@ixpt{13}%
   \set@ragged
   \abovedisplayskip 3pt plus 1pt minus .5pt
   \abovedisplayshortskip \abovedisplayskip
   \advance\abovedisplayshortskip-\baselineskip
   \belowdisplayshortskip \abovedisplayskip
   \belowdisplayskip \abovedisplayskip
% Correct for strut added by \[
   \advance\abovedisplayskip-.2ex
   \advance\abovedisplayshortskip-.2ex
   \lineskip 2pt
   \lineskiplimit\lineskip
   \normallineskip\lineskip
   \normallineskiplimit\lineskiplimit
   \def\@listi{\leftmargin\leftmargini
               \topsep 6\p@ \@plus2\p@ \@minus2\p@
               \parsep 3\p@ \@plus2\p@ \@minus\p@
               \itemsep \parsep}%
}
\renewcommand\footnotesize{%
   \@setfontsize\footnotesize\@viiipt{10}%
   \set@ragged
   \abovedisplayskip 2pt plus 1pt minus .5pt
   \abovedisplayshortskip \abovedisplayskip
   \advance\abovedisplayshortskip-\baselineskip
   \belowdisplayshortskip \abovedisplayskip
   \belowdisplayskip \abovedisplayskip
% Correct for strut added by \[
   \advance\abovedisplayskip-.2ex
   \advance\abovedisplayshortskip-.2ex
   \lineskip 1pt
   \lineskiplimit\lineskip
   \normallineskip\lineskip
   \normallineskiplimit\lineskiplimit
   \def\@listi{\leftmargin\leftmargini
               \topsep 4\p@ \@plus2\p@ \@minus2\p@
               \parsep 2\p@ \@plus\p@ \@minus\p@
               \itemsep \parsep}%
}
\renewcommand\scriptsize{\@setfontsize\scriptsize\@viipt\@viiipt\set@ragged}
\renewcommand\tiny{\@setfontsize\tiny\@vpt\@vipt\set@ragged}
}

%%% Spacing parameters

\topskip=10.5pt
\maxdepth=.5\topskip
\baselineskip=14pt
\parskip=0pt
\parindent=1pc
\frenchspacing
\fussy

% TeX default is \hyphenpenalty=50
\hyphenpenalty=120
% Penalty for breaking after hyphen appearing in the original text
\exhyphenpenalty=75
% Disable the pretolerance algorithm; my machine is fast
\pretolerance=-1

% HACK: fix displays where the prev line has no descenders
% by adding a strut
\def\[{\relax\ifmmode\@badmath\else
    \ifvmode
      \nointerlineskip \makebox[.6\linewidth]%
    \fi\ifvmode\else\strut\fi$$%$$
\fi}

\if@tuftestyle
\smallskipamount=10pt plus 2pt minus 1pt
\medskipamount=15pt plus 3pt minus 1.5pt
\bigskipamount=20pt plus 4pt minus 2pt
\else
\smallskipamount=4pt plus .8pt minus .4pt
\medskipamount=6pt plus 1.2pt minus .6pt
\bigskipamount=8pt plus 1.6pt minus .8pt
\fi

%%% Page layout

%% horizontal and vertical offsets should be zero
\setlength\hoffset{0pt}
\setlength\voffset{0pt}

\newlength\maxtextwidth
\newlength\mintopmargin
\newlength\minsidemargin

\setlength\headheight{1.5pc}
\setlength\headsep{20pt}
\setlength\mintopmargin{6pc}
\setlength\minsidemargin{6pc}
\setlength\footskip{2pc}
\setlength\marginparsep{1.5pc}
\setlength\marginparpush{1pc}
\if@wider
\setlength\maxtextwidth{30pc}
\else
\setlength\maxtextwidth{26pc}
\fi
% In ragged mode the lines tend to be shorter on average.
\if@justify\else\addtolength\maxtextwidth{0.5pc}\fi

\setlength\@tempdima{\paperheight}
\addtolength\@tempdima{-2\mintopmargin}
\addtolength\@tempdima{1pc}   % bottom margin will end up being 1pc smaller
\addtolength\@tempdima{-\topskip}
\divide\@tempdima\baselineskip
\@tempcnta=\@tempdima
\setlength\textheight{\@tempcnta\baselineskip}
\addtolength\textheight{\topskip}

\topmargin=\paperheight
\addtolength\topmargin{-\textheight}
\addtolength\topmargin{-.5\topmargin}
\addtolength\topmargin{.5pc}  % put the page a little below center
\addtolength\topmargin{-\headsep}
\addtolength\topmargin{-\headheight}
\addtolength\topmargin{-1in}
\@settopoint\topmargin

% FIXME: add calculations for twoside, twocolumn
\if@includemarginpar

% Here we're including the margin paragraphs in the centering.
% When we're confined to narrow space, make margin par 1/3 as wide
% as the main column.
\@tempdima=\paperwidth
\advance\@tempdima by-2\minsidemargin
\advance\@tempdima by 1pc       % we will make the right margin 1pc smaller
\advance\@tempdima by-\marginparsep
\advance\@tempdima by-.25\@tempdima
\@tempdimb=\maxtextwidth
\ifdim\@tempdima>\@tempdimb\relax
  \textwidth\maxtextwidth
\else
  \textwidth\@tempdima
\fi
\@settopoint\textwidth
\marginparwidth\paperwidth
\advance\marginparwidth-2\minsidemargin
\advance\marginparwidth 1pc     % make right margin 1pc smaller
\advance\marginparwidth-\textwidth
\advance\marginparwidth-\marginparsep

\@tempdima=\paperwidth
\advance\@tempdima by-\textwidth
\advance\@tempdima by-\marginparsep
\advance\@tempdima by-\marginparwidth
\advance\@tempdima by 1pc       % make right margin 1pc smaller
\oddsidemargin=.5\@tempdima
\advance\oddsidemargin by -1in
\@settopoint\oddsidemargin
\evensidemargin=\paperwidth
\advance\evensidemargin by -2in
\advance\evensidemargin by -\textwidth
\advance\evensidemargin by -\oddsidemargin
\if@mparswitch\else
  \advance\evensidemargin by -\marginparsep
  \advance\evensidemargin by -\marginparwidth
  \advance\evensidemargin by 1pc
\fi
\@settopoint\evensidemargin

\else

% Center the column and put margin paragraphs off to the side,
%   not coming closer than \marginparsep to the physical edge of the page.
\@tempdima=\paperwidth
\advance\@tempdima by-2\minsidemargin
\@tempdimb=\maxtextwidth
\ifdim\@tempdima>\@tempdimb\relax
  \textwidth\@tempdimb
\else
  \textwidth\@tempdima
\fi
\@settopoint\textwidth
\marginparwidth\minsidemargin
\advance\marginparwidth-2\marginparsep

\@tempdima=\paperwidth
\advance\@tempdima by-\textwidth
\oddsidemargin=.5\@tempdima
\advance\oddsidemargin by -1in
\@settopoint\oddsidemargin
\evensidemargin=\paperwidth
\advance\evensidemargin by -2in
\advance\evensidemargin by -\textwidth
\advance\evensidemargin by -\oddsidemargin
\@settopoint\evensidemargin

\fi


%%% List metrics
\def\@listi{\leftmargin\leftmargini
            \parsep 6pt
            \topsep 4pt
            \itemsep 6pt}
\let\@listI\@listi
\@listi


\newtoks\@extoks
\def\@expandinit{\futurelet\@temp\@expandinit@case}
\def\@expandinit@case{%
  \let\@next\@expand@initchar
  \ifcat\noexpand\@temp\bgroup
  \else\ifcat\noexpand\@temp\egroup
  \else\ifcat\noexpand\@temp\@sptoken
    \let\@next\@expand@space
  \else\ifx\@temp\@expand@end
    \let\@next\@expand@finish
  \fi\fi\fi\fi
  \@next}
\def\@expand{\futurelet\@temp\@expand@case}
\def\@expand@case{%
  \let\@next\@expand@char
  \ifcat\noexpand\@temp\bgroup
  \else\ifcat\noexpand\@temp\egroup
  \else\ifcat\noexpand\@temp\@sptoken
    \let\@next\@expand@space
  \else\ifx\@temp\@expand@end
    \let\@next\@expand@finish
  \fi\fi\fi\fi
  \@next}
\def\@expand@initchar#1{\@expush{\@exfirst{#1}}\@expand}
\def\@expand@char#1{\@expush{\@exrest{#1}}\@expand}
\def\@expand@space{\@expush{\@sptoken}\afterassignment\@expand\let\@temp= }
\def\@expand@finish\@expand@end{\egroup}
\def\@expush#1{\expandafter\global\expandafter\@extoks\expandafter{\the\@extoks#1}}
\def\@doexpand#1{\global\@extoks{}\bgroup\@expandinit#1\@expand@end}

%%% Headings
\newskip\headerexpand \headerexpand=0.1em
\def\@headerstyle{\def\@exfirst##1{##1}\def\@exrest##1{\kern\headerexpand##1}}

\def\do@footertitle#1{\expandby\initexpand\@expand#1\@expand@end}
\newtoks\@footertitle
\def\setfootertitle#1{%
  \@doexpand{#1}%
  \@footertitle=\@extoks%
}
\if@twoside
\def\ps@footertitle{%
  \def\@oddhead{\hfil{\scshape\@headerstyle\expandafter\lowercase\expandafter
    {\the\@footertitle}}\kern1pc{\oldstyle\thepage}}%
  \def\@evenhead{{\oldstyle\thepage}\kern1pc
    {\scshape\@headerstyle\expandafter\lowercase\expandafter{\the\@footertitle}}\hfil}%
  \let\@oddfoot\@empty\let\@evenfoot\@oddfoot
  \let\@mkboth\@gobbletwo
  \let\sectionmark\@gobble
  \let\subsectionmark\@gobble
}
\else
\def\ps@footertitle{%
  \def\@oddhead{\small\thepage\kern1pc
    \expandafter\uppercase\expandafter{\the\@footertitle}\hfil}%
  \let\@evenhead\@oddhead
  \let\@oddfoot\@empty\let\@evenfoot\@oddfoot
  \let\@mkboth\@gobbletwo
  \let\sectionmark\@gobble
  \let\subsectionmark\@gobble
}
\fi


%%% Use section symbol and a period after section number
\if@tuftestyle
\def\@seccntformat#1{{\upshape\csname the#1\endcsname}\kern1pc\relax}
\else
\def\@seccntformat#1{\S\kern.22222em\csname the#1\endcsname.{\rm\secspace}}
\fi

%%% Use period after run-in section title
%\let\save@xsect\@xsect
%\def\@xsect#1{%
%  \let\save@svsechd\@svsechd
%  \def\@svsechd{\save@svsechd.}%
%  \save@xsect{#1}}

\def\secspace{\kern.66667em }

\if@tuftestyle
\def\heading#1{\par\noindent{\itshape #1}\kern1pc\ignorespaces}
\let\iheading\heading
\else
\def\heading#1{\par{\bfseries #1.}\secspace\ignorespaces}
\def\iheading#1{\par{\itshape #1.}\secspace\ignorespaces}
\fi

\newskip\initexpand \initexpand=0.1em
\newif\if@doinitword \@doinitwordtrue
%\newcommand\initword[1]{\if@doinitword
%  \bgroup\scshape\expandby\initexpand
%  \@expandinit#1\@expand@end\else#1\fi}
\newcommand\initword[1]{\if@doinitword\bgroup
    \@doexpand{#1}%
    \def\@exfirst##1{{\large##1}}%
    \def\@exrest##1{\kern\initexpand##1}%
    \scshape\the\@extoks\egroup
  \else#1\fi}

\if@tuftestyle
\renewcommand\section{\@startsection{section}{1}{0pt}%
                                   {-\bigskipamount}%
                                   {6pt}%
                                   {\large\itshape}}
\renewcommand\subsection{\@startsection{subsection}{2}{0pt}%
                                   {-\medskipamount}%
                                   {6pt}%
                                   {\reset@font\itshape}}
\renewcommand\subsubsection{\@startsection{subsubsection}{3}{0pt}%
                                   {-\medskipamount}%
                                   {6pt}%
                                   {\reset@font\itshape}}
\renewcommand\paragraph{\@startsection{paragraph}{4}{0pt}%
                                   {\smallskipamount}%
                                   {-\parindent}%
                                   {\reset@font\itshape}}
\renewcommand\subparagraph{\@startsection{subparagraph}{5}{0pt}%
                                   {\smallskipamount}%
                                   {-\parindent}%
                                   {\reset@font\itshape}}
\else
\@doinitwordfalse
\renewcommand\section{\@startsection{section}{1}{\parindent}%
                                   {\bigskipamount}%
                                   {-\parindent}%
                                   {\reset@font\bfseries}}
\renewcommand\subsection{\@startsection{subsection}{2}{\parindent}%
                                   {\medskipamount}%
                                   {-\parindent}%
                                   {\reset@font\bfseries}}
\renewcommand\subsubsection{\@startsection{subsubsection}{3}{\parindent}%
                                   {\medskipamount}%
                                   {-\parindent}%
                                   {\reset@font\bfseries}}
\renewcommand\paragraph{\@startsection {paragraph}{4}{\parindent}%
                                   {\smallskipamount}%
                                   {-\parindent}%
                                   {\reset@font\bfseries}}
\renewcommand\subparagraph{\@startsection {subparagraph}{5}{\parindent}%
                                   {\smallskipamount}%
                                   {-\parindent}%
                                   {\reset@font\itshape}}
\fi

%%% I have no idea why the standard book.cls doesn't do this:
\def\cleardoublepage{\clearpage\if@twoside \ifodd\c@page\else
    \thispagestyle{empty}\hbox{}\newpage\if@twocolumn\hbox{}\newpage\fi\fi\fi}
