% geometries.sty v1.2 2005/11/14
%
% Copyright 2005 Stephan Otto
%
% Contact software@stephan-otto.net
%
% 
% Package geometries
% ==================
%
%
% Description
% -----------
%
% With the help of this package you can define multiple geometries for
% your LaTeX document. It was written to supplement the powerful
% 'geometry' package, but it works well with any geometry command
% limited to the preamble.
%
%
% License
% -------
%
% This file may be distributed and/or modified under the conditions of
% the LaTeX Project Public License, either version 1.3 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.3
% or later is part of all distributions of LaTeX version 2003/12/01 or
% later.
%
%
% Installation
% ------------
%
% Put this file somewhere TeX can find it. See http://faq.tug.org if
% you have questions.
%
%
% Usage
% -----
%
% Include this package with
%
%   \usepackage[<option>]{geometries}
%
% with the possible <option> 'reset' explained below. Put each
% geometry definition in your preamble inside an environment
%
%   \begin{makegeometry}{<name>}
%   ...
%   \end{makegeometry}
%
% Apply a geometry definition at any page of your document by
%
%   \usegeometry{<name>}
%
% Note that the changes made inside 'makegeometry' persist outside it
% unless you use the 'reset' option. Be aware, however, that this is
% probably a waste of memory. The commands used by 'makegeometry' are
%
%   \newgeometry{<name>}
%   \savegeometry{<name>}
%
% which do just what they say.
%
%

\NeedsTeXFormat{LaTeX2e}

\ProvidesPackage{geometries}

\let\resetge@metry0

\DeclareOption{reset}{
  \let\resetge@metry1
}

\ProcessOptions

\newcommand{\newgeometry}[1]{
  \expandafter\newskip\csname#1evensidemargin\endcsname
  \expandafter\newskip\csname#1oddsidemargin\endcsname
  \expandafter\newskip\csname#1marginparsep\endcsname
  \expandafter\newskip\csname#1marginparwidth\endcsname
  \expandafter\newskip\csname#1marginparpush\endcsname
  \expandafter\newskip\csname#1textwidth\endcsname
  \expandafter\newskip\csname#1topmargin\endcsname
  \expandafter\newskip\csname#1headheight\endcsname
  \expandafter\newskip\csname#1headsep\endcsname
  \expandafter\newskip\csname#1textheight\endcsname
  \expandafter\newskip\csname#1footskip\endcsname
}

\newcommand{\savegeometry}[1]{
  \global\csname#1evensidemargin\endcsname=\evensidemargin
  \global\csname#1oddsidemargin\endcsname=\oddsidemargin
  \global\csname#1marginparsep\endcsname=\marginparsep
  \global\csname#1marginparwidth\endcsname=\marginparwidth
  \global\csname#1marginparpush\endcsname=\marginparpush
  \global\csname#1textwidth\endcsname=\textwidth
  \global\csname#1topmargin\endcsname=\topmargin
  \global\csname#1headheight\endcsname=\headheight
  \global\csname#1headsep\endcsname=\headsep
  \global\csname#1textheight\endcsname=\textheight
  \global\csname#1footskip\endcsname=\footskip
}  

\newcommand{\usegeometry}[1]{
  \global\evensidemargin=\csname#1evensidemargin\endcsname
  \global\oddsidemargin=\csname#1oddsidemargin\endcsname
  \global\marginparsep=\csname#1marginparsep\endcsname
  \global\marginparwidth=\csname#1marginparwidth\endcsname
  \global\marginparpush=\csname#1marginparpush\endcsname
  \global\textwidth=\csname#1textwidth\endcsname
  \global\topmargin=\csname#1topmargin\endcsname
  \global\headheight=\csname#1headheight\endcsname
  \global\headsep=\csname#1headsep\endcsname
  \global\textheight=\csname#1textheight\endcsname
  \global\footskip=\csname#1footskip\endcsname
  % needed for rubber spaces etc.
  \global\hsize=\textwidth
  \global\columnwidth=\textwidth
  \global\vsize=\textheight
  \global\@colht=\textheight
  \global\@colroom=\textheight
}  

\newenvironment{makegeometry}[1]{
  \if\resetge@metry1
    \newgeometry{@ldge@metry}
    \let\resetge@metry2
  \fi
  \if\resetge@metry2
    \savegeometry{@ldge@metry}
  \fi
  \def\newge@metry{#1}
}{
  \newgeometry{\newge@metry}
  \savegeometry{\newge@metry}
  \if\resetge@metry2
    \usegeometry{@ldge@metry}
  \fi
  \def\newge@metry{}
}

