\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{setpaper}[1994/08/01 Command to set the paper size.]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   The purspose of this package is to allow us to describe the format of   %
%   a page in terms of the traditional dimensions, rather than the ones     %
%   TeX uses.  To use the package, simply set appropriate values for the    %
%   dimensions described below and then call "\setpaper", this will set the %
%   TeX page description.                                                   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   First we define the dimensions of the paper.                            %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% \newlenth{\paperwidth}	% These are now included in LaTeX2e
% \newlenth{\paperheight}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Now we define the adjustment for the binding.                             %
% Use this dimension to say how much of the page will be consumed by the    %
% binding process.							    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newlength{\binding}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Next we define the size of the margins.                                 %
%                                                                           %
%   Because the units traditionally used to specify margins are different   %
%   from (and not exactly compatable with) the units traditionally  used to %
%   specify the paper, it is usual to only specify the back margin and      %
%   head margin and infer the size of the other margins from                %
%   these margins and the type size (see below).                            %
%                                                                           %
%   The back margin is the one from the binding to the edge of the text.    %
%   The head margin is the one at the top of the page.                      %
%                                                                           %
%   Note that the figure for the back margin will only be used if the       %
%   document is in twosided mode.                                           %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newlength{\backmargin}
\newlength{\headmargin}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Next we define the type size.                                           %
%                                                                           %
%   Note that the type area include all printed matter on the page,         %
%   that is it includes the header and footer as well.                      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newlength{\typewidth}
\newlength{\typeheight}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Next we define the size of the header and the footer.                     %
%                                                                           %
% We will give both the size of the text of the header and footer           %
% (\headsize and \footsize) and the amount of white space between the       %
% the header and footer, and the body of the text (\headgap, \footgap).     %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newlength{\headsize}
\newlength{\headgap}
\newlength{\footsize}
\newlength{\footgap}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   TeX does not use these standards for describing the appearance of       %
%   a page.   Instead it uses the following lengths to describe how your    %
%   page should look:                                                       %
%       \oddsizemargin                                                      %
%           Space from the left edge of an odd numbered page to the body    %
%           of the text.                                                    %
%       \evensidemargin                                                     %
%           Space from the left edge of an even numbered page to the body   %
%           of the text.                                                    %
%       \topmargin                                                          %
%           Space from the top of the page to the type area.                %
%       \headheight                                                         %
%           Space devoted to the running header.                            %
%       \headsep                                                            %
%           Space between the running header and the body of the text.      %
%       \footskip                                                           %
%           Distance from the bottom of the body of the text to the         %
%           bottom of the first line of the running footer.                 %
%           The gap between the foot text and the body of the text will     %
%           therefore be \footskip - \baselineskip.                         %
%       \textwidth                                                          %
%           The width of the text on the page.                              %
%       \textheight                                                         %
%           The height of the body of the text (does not include headers    %
%           or footers).                                                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand{\setpaper}{
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% If we are using PDF LaTeX we need to tell it how big the paper is.        %
% Why it doesn't use the same variables as ordinary LaTeX god only knowns.  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\@ifundefined{pdfpagewidth}{}{
  \setlength{\pdfpagewidth}{\paperwidth}
  \setlength{\pdfpageheight}{\paperheight}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   So TeX doesn't have to do some much streching, we will shrink the       %
%   type height a little until it is a multiple of \baselineskip.           %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcount{\linecount}
\linecount=\typeheight
\divide\linecount by \baselineskip
\setlength{\typeheight}{\baselineskip}
\multiply\typeheight by \linecount

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   We do not want the standard 1 inch margins, so before we begin we will  %
%   set the margins so that they allign with the top left corner of the     %
%   paper.                                                                  %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\setlength{\topmargin}{-1truein}
\setlength{\oddsidemargin}{-1truein}
\setlength{\evensidemargin}{-1truein}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Now we set TeX's parameters so that they describe the page we want.     %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\if@twoside
    \addtolength{\oddsidemargin}{\binding}
    \addtolength{\oddsidemargin}{\backmargin}
    \addtolength{\evensidemargin}{\paperwidth}
    \addtolength{\evensidemargin}{-\binding}
    \addtolength{\evensidemargin}{-\backmargin}
    \addtolength{\evensidemargin}{-\typewidth}
\else
    \setlength{\oddsidemargin}{\paperwidth}
    \addtolength{\oddsidemargin}{-\typewidth}
    \addtolength{\evensidemargin}{0.5\oddsidemargin}
    \setlength{\oddsidemargin}{\evensidemargin}
    \addtolength{\evensidemargin}{-\binding}
    \addtolength{\oddsidemargin}{\binding}
\fi
\addtolength{\topmargin}{\headmargin}
\setlength{\headheight}{\headsize}
\setlength{\headsep}{\headgap}
\setlength{\footskip}{\footgap}
\addtolength{\footskip}{\baselineskip}
\setlength{\textwidth}{\typewidth}
\setlength{\textheight}{\typeheight}
\addtolength{\textheight}{-\headsize}
\addtolength{\textheight}{-\headgap}
\addtolength{\textheight}{-\footgap}
\addtolength{\textheight}{-\footsize}
}
