%%
%% This is file 'grid.sty',
%%
%% 
%% grid.sty Copyright (C) 2009 River Valley Technologies
%%          URL: http://www.river-valley.com
%%          Email: <latex.support@river-valley.com>
%% 
%% This file may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.2
%% 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.2 or later is part of all distributions of LaTeX
%% version 1999/12/01 or later.
%%
%% $Id: grid.sty 14 2009-06-16 07:00:25Z cvr $
%% $URL: http://lenova.river-valley.com/svn/grid/trunk/grid.sty $
%%
%% 
\NeedsTeXFormat{LaTeX2e}%
\ProvidesPackage{grid}[2009/06/16 v1.0 Grid Layout]

\RequirePackage{keyval}

%%%
%%  Process Keyvalue Options
%
\def\ProcessOptionsKV{\@ifnextchar[%]
  {\@ProcessOptionsKV}{\@ProcessOptionsKV[]}}%
\def\@ProcessOptionsKV[#1]#2{%
  \let\@tempa\@empty
  \@tempcnta\z@
  \if#1p\@tempcnta\@ne\else\if#1c\@tempcnta\tw@\fi\fi
  \ifodd\@tempcnta
   \edef\@tempa{\@ptionlist{\@currname.\@currext}}%
  \else
    \@for\CurrentOption:=\@classoptionslist\do{%
      \@ifundefined{KV@#2@\CurrentOption}%
      {}{\edef\@tempa{\@tempa,\CurrentOption,}}}%
    \ifnum\@tempcnta=\z@
      \edef\@tempa{\@tempa,\@ptionlist{\@currname.\@currext}}%
    \fi
  \fi
  \edef\@tempa{\noexpand\setkeys{#2}{\@tempa}}%
  \@tempa
  \AtEndOfPackage{\let\@unprocessedoptions\relax}}%

\def\Grid@baseline{10\p@}
\def\Grid@fontsize{12\p@}
\def\Grid@lines{40}
\def\Grid@textheight{%
       \@tempdima=\Grid@baseline%
       \multiply\@tempdima by \Grid@lines%
       \textheight=\the\@tempdima%
}

\define@key{Grid}{baseline}{\edef\Grid@baseline{#1}}
\define@key{Grid}{fontsize}{\edef\Grid@fontsize{#1}}
\define@key{Grid}{lines}{\edef\Grid@lines{#1}}

\def\Grid@setkeys{\setkeys{Grid}}%
\ProcessOptionsKV{Grid}

\Grid@textheight

\renewcommand\normalsize{%
   \baselineskip=\Grid@baseline%
   \@setfontsize\normalsize{\Grid@fontsize}{\Grid@baseline}%
   \lineskip=0pt
   \lineskiplimit=-\Grid@fontsize%
   \abovedisplayskip \baselineskip%
   \abovedisplayshortskip .5\baselineskip%
   \belowdisplayskip \abovedisplayskip
   \belowdisplayshortskip \abovedisplayshortskip
   \let\@listi\@listI}
\normalsize
\parskip 0pt

\newdimen\floatunit
\newskip\allfloats
\setlength\floatunit{\the\baselineskip}

\setlength\allfloats{\floatunit}

\setlength\floatsep{\allfloats}
\setlength\textfloatsep{\allfloats}
\setlength\intextsep{\allfloats}
\setlength\dblfloatsep{\allfloats}
\setlength\dbltextfloatsep{\allfloats}

\setlength\@fptop{\z@}
\setlength\@fpsep{\z@}
\setlength\@fpbot{\z@}
\setlength\@dblfptop{\z@}
\setlength\@dblfpsep{\z@}
\setlength\@dblfpbot{\z@}

\begingroup
  \catcode`P=12
  \catcode`T=12
  \lowercase{
    \def\x{\def\rem@decimal##1.##2PT{##1}}}
  \expandafter\endgroup\x
\def\strip@decimal{\expandafter\rem@decimal\the}

\begingroup
  \catcode`P=12
  \catcode`T=12
  \lowercase{
    \def\y{\def\rem@dot##1.##2PT{##1##2}}}
  \expandafter\endgroup\y
\def\strip@dot{\expandafter\rem@dot\the}

\newdimen\halfbaselineskip
\halfbaselineskip=\floatunit
\divide\halfbaselineskip by 2

\newdimen\figboxht

\long\def\roundoff{\figboxht=\fight%
    \advance\figboxht by \baselineskip%
    \multiply\figboxht by 10%
    \xdef\xbaselineskip{\strip@dot\baselineskip}%
    \divide\figboxht by \xbaselineskip%
    \xdef\mylines{\strip@decimal\figboxht}%
    \figboxht=\baselineskip%
    \multiply\figboxht by\mylines%
    \advance\figboxht -\fight%
    \ifdim\the\figboxht>\the\halfbaselineskip%
      \advance\figboxht by -\floatunit%
    \else\fi%
    }

%%%
%%  Sections
%
\renewcommand\section{\@startsection {section}{1}{\z@}%
                                   {-.999\baselineskip}%
                                   {0.001\baselineskip}%
                                   {\bfseries\mathversion{bold}}}
\renewcommand\subsection{\@startsection{subsection}{2}{\z@}%
                                   {\baselineskip}%
                                   {-.35\baselineskip}%
                                   {\bfseries\unskip}}
\renewcommand\subsubsection{\@startsection{subsubsection}{3}{\z@}%
                                   {\baselineskip}%
                                   {-.35\baselineskip}%
                                   {\itshape\unskip}}

%%%
%%  Floats
%
\let\oldfigure\figure
\let\oldendfigure\endfigure
\expandafter\let\csname oldfigurest\expandafter%
              \endcsname\csname figure*\endcsname
\expandafter\let\csname oldendfigurest\expandafter%
              \endcsname\csname endfigure*\endcsname

\let\oldtable\table
\let\oldendtable\endtable
\expandafter\let\csname oldtablest\expandafter%
              \endcsname\csname table*\endcsname
\expandafter\let\csname oldendtablest\expandafter%
              \endcsname\csname endtable*\endcsname

\renewenvironment{figure}
         {\oldfigure\begin{gridfltenv}}
         {\end{gridfltenv}\oldendfigure}
\renewenvironment{figure*}
         {\oldfigurest\begin{gridfltenv}}
         {\end{gridfltenv}\oldendfigurest}

\renewenvironment{table}
         {\oldtable\begin{gridfltenv}}
         {\end{gridfltenv}\oldendtable}
\renewenvironment{table*}
         {\oldtablest\begin{gridfltenv}}
         {\end{gridfltenv}\oldendtablest}

\newenvironment{gridfltenv}
         {\global\setbox0=\vbox\bgroup}
         {\egroup%
          \xdef\fight{\the\ht0}%
          \roundoff%
          \leavevmode\vadjust{\box0\vskip\figboxht}\hfil\break%
         }

%%%
%%  Equations
%
\newenvironment{gridenv}
         {\global\setbox0=\vbox\bgroup}
         {\egroup%
          \xdef\fight{\the\ht0}%
          \roundoff%
          \leavevmode%
          \vadjust{\vskip0.5\figboxht%
                   \box0%
                   \vskip0.5\figboxht%
                   }\hfil\break%%
         }

\jot=\baselineskip

%%%
%%  Extra vertical skip removed from the environments 
%   like verse, quotation, quote and description
\renewenvironment{verse}
               {\let\\\@centercr
                \list{}{\itemsep      \z@
                        \itemindent   -1.5em%
                        \listparindent\itemindent
                        \rightmargin  \leftmargin
                        \advance\leftmargin 1.5em}%
                        \topsep\z@\partopsep\z@\parskip\z@%
                        \itemsep\z@\@outerparskip\z@\parsep\z@%
                        \@topsep\z@\@topsepadd\z@%
                \item\relax}
               {\endlist}

\renewenvironment{quotation}
               {\list{}{\listparindent 1.5em%
                        \itemindent    \listparindent
                        \rightmargin   \leftmargin
                        \parsep        \z@ \@plus\p@}%
                        \topsep\z@\partopsep\z@\parskip\z@%
                        \itemsep\z@\@outerparskip\z@\parsep\z@%
                        \@topsep\z@\@topsepadd\z@%
                \item\relax}
               {\endlist}

\renewenvironment{quote}
               {\list{}{\rightmargin\leftmargin}%
                        \topsep\z@\partopsep\z@\parskip\z@%
                        \itemsep\z@\@outerparskip\z@\parsep\z@%
                        \@topsep\z@\@topsepadd\z@%
                \item\relax}
               {\endlist}

\renewenvironment{description}
               {\list{}{\labelwidth\z@ \itemindent-\leftmargin
                        \topsep\z@\partopsep\z@\parskip\z@%
                        \itemsep\z@\@outerparskip\z@\parsep\z@%
                        \@topsep\z@\@topsepadd\z@%
                        \let\makelabel\descriptionlabel}}
               {\endlist}

%%%
%%  Lists
%
\def\enumerate{%
  \ifnum \@enumdepth >\thr@@\@toodeep\else
    \advance\@enumdepth\@ne
    \edef\@enumctr{enum\romannumeral\the\@enumdepth}%
      \expandafter
      \list
        \csname label\@enumctr\endcsname
        {\usecounter\@enumctr\def\makelabel##1{\hss\llap{##1}}%
         \topsep\z@\partopsep\z@\parskip\z@\itemsep\z@%
         \@outerparskip\z@\parsep\z@\@topsep\z@\@topsepadd\z@}%
  \fi}

\def\itemize{%
  \ifnum \@itemdepth >\thr@@\@toodeep\else
    \advance\@itemdepth\@ne
    \edef\@itemitem{labelitem\romannumeral\the\@itemdepth}%
    \expandafter
    \list
      \csname\@itemitem\endcsname
      {\def\makelabel##1{\hss\llap{##1}}%
       \topsep\z@\partopsep\z@\parskip\z@\itemsep\z@%
       \@outerparskip\z@\parsep\z@\@topsep\z@\@topsepadd\z@}%
  \fi}

%%%
%%  References
%
\renewenvironment{thebibliography}[1]
     {\section*{\refname}%
      \@mkboth{\MakeUppercase\refname}{\MakeUppercase\refname}%
      \list{\@biblabel{\@arabic\c@enumiv}}%
           {\settowidth\labelwidth{\@biblabel{#1}}%
            \leftmargin\labelwidth
            \advance\leftmargin\labelsep
            \@openbib@code
            \usecounter{enumiv}%
            \let\p@enumiv\@empty
            \topsep\z@\partopsep\z@\parskip\z@\itemsep\z@%
            \@outerparskip\z@\parsep\z@\@topsep\z@\@topsepadd\z@%
            \renewcommand\theenumiv{\@arabic\c@enumiv}}%
      \sloppy
      \clubpenalty4000
      \@clubpenalty \clubpenalty
      \widowpenalty4000%
      \sfcode`\.\@m}
     {\def\@noitemerr
       {\@latex@warning{Empty `thebibliography' environment}}%
      \endlist}

\endinput

%% Enunciations (theorem, lemma etc) were not done
%% Square bracket option not currently supported for floats! \begin{figure}[!tbp]
%% Footnotes are not aligned correctly.
%% 
%% End of file 'grid.sty'
%%