% This package provides these commands and package options:
%
%   \SetMargins{VMARG,HMARG}   -- Sets left and right margins to HMARG.
%                                 Sets top and bottom margins to VMARG.
%                                 The default margins are 1in.
% 
%   NoHeaderSpace (option)     -- Nukes any space allocated for headers.
%   \NoHeaderSpace                In the absence of headers, this means
%                                 the top margin will be correct.
%                                 In the presence of headers, instant
%                                 death of the author by electrocution
%                                 will result.
%   
%   FooterInMargin (option)    -- Ignores the footer (usually page number)
%   \FooterInMargin               when arranging the bottom margin.
%                                 The bottom margin extends from the
%                                 bottom of the text (not including
%                                 footer) to the bottom of the page.
%                                 This is the default.
%
%   FooterNotInMargin (option) -- Considers the bottom of the text to be
%   \FooterNotInMargin            the bottom of the footer.
%                                 The bottom margin extends from the
%                                 bottom of the footer to the bottom of
%                                 the page.
%
% All other dimensions are magically set to the appropriate values by
% loading this package and/or using any of the provided commands. 
% If you do \NoHeaderSpace, to undo this later in the document, set
% \headheight and \headsep and then invoke one of the other commands to
% perform the necessary magic. 

\NeedsTeXFormat{LaTeX2e}[1994/12/01]
\ProvidesPackage{jbw-margins}[1996/11/23 Convenient margin setting]

\DeclareOption{NoHeaderSpace}{\NoHeaderSpace}
\DeclareOption{FooterInMargin}{\FooterInMargin}
\DeclareOption{FooterNotInMargin}{\FooterNotInMargin}

\newcommand{\SetMargins}[2]{%
  \renewcommand{\@VertMargin}{#1}%
  \renewcommand{\@HorizMargin}{#2}%
  \@UpdateMargins
}
\newcommand{\@VertMargin}{1in}
\newcommand{\@HorizMargin}{1in}

\newcommand{\NoHeaderSpace}{%
  % Since I'm not using headers, I must make them zero-height or else my
  % top margin will appear to be larger. 
  \setlength{\headheight}{0pt}%
  \setlength{\headsep}{0pt}%
  \@UpdateMargins
}

\newcommand{\FooterInMargin}{%
  \renewcommand{\@FooterMarginPolicy}{}%
  \@UpdateMargins
}
\newcommand{\FooterNotInMargin}{%
  \renewcommand{\@FooterMarginPolicy}{%
    \advance\textheight by -\footskip
  }%
  \@UpdateMargins
}
\newcommand{\@FooterMarginPolicy}{}

% Internal function to set all of the values correctly given the current
% paper size, desired margins, and header and footer policy.
\newcommand{\@UpdateMargins}{%
  %
  % Set horizontal space between margins.
  \setlength{\textwidth}{\paperwidth}
  \advance\textwidth by -\@HorizMargin
  \advance\textwidth by -\@HorizMargin
  %
  % Set left margin.
  \setlength{\oddsidemargin}{\@HorizMargin}
  \advance\oddsidemargin by -1in%
  \setlength{\evensidemargin}{\@HorizMargin}
  \advance\evensidemargin by -1in%
  %
  % Set vertical space between margins.
  \setlength{\textheight}{\paperheight}
  \advance\textheight by -\@VertMargin
  \advance\textheight by -\@VertMargin
  %
  % Set top margin.
  \setlength{\topmargin}{\@VertMargin}
  \advance\topmargin by -1in%
  %
  % Diminish \textheight by amount of headers and footers.
  \advance\textheight by -\headheight
  \advance\textheight by -\headsep
  \@FooterMarginPolicy
}

% This is for backward compatibility only, now that LaTeX2e has an
% ``a4paper'' option.
%
%\SetPaperSizeAndMargins{11in}{8.5in}{1in}{1in}% US letter
%\SetPaperSizeAndMargins{29.7cm}{21cm}{1in}{1in}% A4
%\SetPaperSizeAndMargins{vsize}{hsize}{vmargin}{hmargin}
\newcommand{\SetPaperSizeAndMargins}[4]{%
  %
  % Set horizontal space between margins.
  \setlength{\paperheight}{#1}%
  \setlength{\paperwidth}{#2}%
  \SetMargins{#3}{#4}%
}

% If user specified options when loading package, handle them now.
\ProcessOptions

% This was a nice idea, but begin-document hooks are executed after \hsize
% and \columnwidth are set by \document.
%\AtBeginDocument{\@UpdateMargins}

% Install default values.
\@UpdateMargins
