%%
%%      P A G E S I Z E
%%
%%      Page layout and automatic page centering
%%
%%      Copyright (C) 1994-96 Hideo Umeki
%%      email:  umeki@ssel.toshiba.co.jp
%%      This package may be freely transmitted, reproduced, or modified for
%%      non-commercial purposes provided that this notice is left intact.
%%
\def\fileversion{v2.51}
\def\filedate{1996/01/30}
%%
\newif\if@latextwoe
\newif\if@pgsz@verbose\@pgsz@verbosefalse
\newif\if@pgsz@landscape\@pgsz@landscapefalse
\newif\if@pgsz@landscapedone\@pgsz@landscapedonefalse
\newif\if@pgsz@width
\newif\if@pgsz@height
\newif\if@pgsz@hmargin
\newif\if@pgsz@vmargin
\newdimen\pgsz@paperwidth
\newdimen\pgsz@paperheight
\newdimen\pgsz@width
\newdimen\pgsz@height
\newdimen\pgsz@hmargin
\newdimen\pgsz@vmargin
\newdimen\pgsz@twosideshift

% These values are to be modified below. 
\pgsz@width 100mm
\pgsz@height 100mm
\pgsz@twosideshift 0pt

\def\setps@@#1#2#3{%
\if@pgsz@verbose\typeout{paper size: #1}\fi%
\pgsz@paperwidth=#2\relax\pgsz@paperheight=#3}

\def\setps@aiii{\setps@@{A3}{297mm}{420mm}}
\def\setps@aiv{\setps@@{A4}{210mm}{297mm}}
\def\setps@av{\setps@@{A5}{148mm}{210mm}}
\def\setps@biii{\setps@@{B3}{353mm}{500mm}}
\def\setps@biv{\setps@@{B4}{250mm}{353mm}}
\def\setps@bv{\setps@@{B5}{176mm}{250mm}}
\def\setps@let{\setps@@{letter}{8.5in}{11in}}
\def\setps@leg{\setps@@{legal}{8.5in}{14in}}
\def\setps@exe{\setps@@{executive}{7.25in}{10.5in}}

\@ifundefined{if@compatibility}{\@latextwoefalse}{\@latextwoetrue}

%
% for LaTeX2e
%
\if@latextwoe
        \NeedsTeXFormat{LaTeX2e}
        \ProvidesPackage{pagesize}[\filedate\space\fileversion\space
        Page layout and automatic page centering (HU)]
        \DeclareOption{verbose}{\@pgsz@verbosetrue}
        \DeclareOption{a3}{\setps@aiii}
        \DeclareOption{a3paper}{\setps@aiii}
        \DeclareOption{a4}{\setps@aiv}
        \DeclareOption{a4paper}{\setps@aiv}
        \DeclareOption{a5}{\setps@av}
        \DeclareOption{a5paper}{\setps@av}
        \DeclareOption{b3}{\setps@biii}
        \DeclareOption{b3paper}{\setps@biii}
        \DeclareOption{b4}{\setps@biv}
        \DeclareOption{b4paper}{\setps@biv}
        \DeclareOption{b5}{\setps@bv}
        \DeclareOption{b5paper}{\setps@bv}
        \DeclareOption{letterpaper}{\setps@let}
        \DeclareOption{legalpaper}{\setps@leg}
        \DeclareOption{executivepaper}{\setps@exe}
        \DeclareOption{landscape}{\@pgsz@landscapetrue}
        \DeclareOption{twoside}{\@twosidetrue}
        \DeclareOption{portrait}{\@pgsz@landscapefalse}
        \DeclareOption*{%
          \PackageWarning{pagesize}{Unknown option `\CurrentOption'}}
        \ExecuteOptions{a4paper,portrait}% default setting (A4,portrait) 
        \ProcessOptions
        \@ifundefined{paperwidth}{}
        {\ifdim\paperheight<\paperwidth\@pgsz@landscapetrue\fi}
%
% for LaTeX209
%
\else
        \immediate\typeout{LaTeX option style: pagesize \fileversion\space(HU)}
        \setps@aiv % default setting (A4)
\fi
%
% initialize
%
\def\pgsz@init{%
        \@pgsz@widthfalse
        \@pgsz@heightfalse
        \@pgsz@hmarginfalse
        \@pgsz@vmarginfalse
        \hoffset\z@
        \voffset\z@
        \def\pgsz@hscale{.8}
        \def\pgsz@vscale{.9}
%
        \def\string@true{true}
        \def\string@false{false}
        \def\string@aiii{a3}\def\string@aiiipaper{a3paper}
        \def\string@aiv{a4}\def\string@aivpaper{a4paper}
        \def\string@av{a5}\def\string@avpaper{a5paper}
        \def\string@biii{b3}\def\string@biiipaper{b3paper}
        \def\string@biv{b4}\def\string@bivpaper{b4paper}
        \def\string@bv{b5}\def\string@bvpaper{b5paper}
        \def\string@let{letterpaper}
        \def\string@leg{legalpaper}
        \def\string@exe{executivepaper}
        \def\string@landscape{landscape}
        \def\string@jpostcard{jpostcard}
}
%
% parse parameters
%
\def\parse@pgsz@params#1{%
\@for\@pgszparam:=#1\do{\expandafter\pgsz@setparams\@pgszparam,}}
\def\pgsz@setparams#1=#2,{%
\@ifundefined{@setpgsz@#1}{%
\if@latextwoe
  \PackageError{pagesize}{Undefined option: `#1'}{%
Available options are:\MessageBreak\space
verbose, paper, landscape, twoside,\MessageBreak\space
(h|v)scale, width, height, (h|v)margin,\MessageBreak\space
(h|v)offset, head, headheight,headsep, foot, special.}
\else
\@latexerr{Undefined option: `#1'}{%
Available options are:^^J\space
verbose, paper, landscape, twoside,^^J\space
(h|v)scale, width, height, (h|v)margin,^^J\space
(h|v)offset, head,headheight,headsep, foot,footskip, special.}
\fi}{\@nameuse{@setpgsz@#1}{#2}}}
%
% define \@setpgsz@*** 's
%
\def\@setpgsz@paper#1{%         "paper=..."
        \edef\@tmparg{#1}
        \ifx     \@tmparg\string@aiii     \setps@aiii
        \else\ifx\@tmparg\string@aiiipaper\setps@aiii
        \else\ifx\@tmparg\string@aiv      \setps@aiv
        \else\ifx\@tmparg\string@aivpaper \setps@aiv
        \else\ifx\@tmparg\string@av       \setps@av
        \else\ifx\@tmparg\string@avpaper  \setps@av
        \else\ifx\@tmparg\string@biii     \setps@biii
        \else\ifx\@tmparg\string@biiipaper\setps@biii
        \else\ifx\@tmparg\string@biv      \setps@biv
        \else\ifx\@tmparg\string@bivpaper \setps@biv
        \else\ifx\@tmparg\string@bv       \setps@bv
        \else\ifx\@tmparg\string@bvpaper  \setps@bv
        \else\ifx\@tmparg\string@let      \setps@let %<-- v2.51 
        \else\ifx\@tmparg\string@leg      \setps@leg %<-- v2.51
        \else\ifx\@tmparg\string@exe      \setps@exe %<-- v2.51
        \else
        \if@latextwoe
        \PackageWarningNoLine{pagesize}{Unknown paper=`\@tmparg'. A4 is set}
        \else
        \typeout{Pagesize Warning: Unknown paper=`\@tmparg'. A4 is set.}
        \fi
        \fi\fi\fi\fi\fi \fi\fi\fi\fi\fi \fi\fi\fi\fi\fi
        \@pgsz@landscapedonefalse
        \let\@tmparg\@undefined
}
\def\@setpgsz@verbose#1{%   "verbose=..."
        \edef\@tmparg{#1}
        \ifx\@tmparg\string@false
                \@pgsz@verbosefalse
        \else\@pgsz@verbosetrue\fi
        \let\@tmparg\@undefined
}
\def\@setpgsz@landscape#1{%   "landscape=..."
        \edef\@tmparg{#1}
        \ifx\@tmparg\string@false\@pgsz@landscapefalse
        \else\@pgsz@landscapetrue\fi
        \let\@tmparg\@undefined
}
\def\@setpgsz@twoside#1{%   "twoside=..."
        \edef\@tmparg{#1}
        \ifx\@tmparg\string@false\@twosidefalse
        \else\@twosidetrue
                \ifx\@tmparg\string@true\else
                \ifx\@tmparg\@empty\else
                \pgsz@twosideshift=#1
                \if@pgsz@verbose\typeout{twoside shift: #1}\fi
                \fi\fi
        \fi
        \let\@tmparg\@undefined
}
\def\@setpgsz@hscale#1{%        "hscale=..."
        \if@pgsz@verbose
                \typeout{text hscale: #1 x paperwidth}
        \fi
        \def\pgsz@hscale{#1}
}
\def\@setpgsz@vscale#1{%        "vscale=..."
        \if@pgsz@verbose
                \typeout{text vscale: #1 x paperheight%
                        (incl. head and foot)}
        \fi
        \def\pgsz@vscale{#1}
}
\def\@setpgsz@scale#1{%         "scale=..."
        \if@pgsz@verbose
                \typeout{text scale: #1 x papersize%
                        (width and height)}
        \fi
        \def\pgsz@hscale{#1}
        \def\pgsz@vscale{#1}
}
\def\@setpgsz@width#1{%         "width=..."
        \if@pgsz@verbose\typeout{text width: #1}\fi
        \@pgsz@widthtrue
        \pgsz@width=#1\relax
}
\def\@setpgsz@height#1{%        "height=..."
        \if@pgsz@verbose\typeout{text height: #1}\fi
        \@pgsz@heighttrue
        \pgsz@height=#1\relax
}
\def\@setpgsz@hmargin#1{%       "hmargin=..."
        \if@pgsz@verbose
                \typeout{paper hmargin: #1}
        \fi
        \@pgsz@hmargintrue
        \pgsz@hmargin=#1\relax
}
\def\@setpgsz@vmargin#1{%       "vmargin=..."
        \if@pgsz@verbose
                \typeout{paper vmargin: #1}
        \fi
        \@pgsz@vmargintrue
        \pgsz@vmargin=#1\relax
}
\def\@setpgsz@margin#1{%        "margin=..."
        \if@pgsz@verbose
                \typeout{paper margin: #1}
        \fi
        \@pgsz@hmargintrue
        \@pgsz@vmargintrue
        \pgsz@hmargin=#1\relax
        \pgsz@vmargin=#1\relax
}
\def\@setpgsz@head#1{%                     "head=..."  <-- v2.50
        \if@pgsz@verbose
                \typeout{headheight: #1}
        \fi
        \headheight=#1\relax
        \ifdim\headheight=\z@ \headsep\z@ \fi
}
\def\@setpgsz@headheight#1{%          "headheight=..."   <-- v2.50
        \if@pgsz@verbose
                \typeout{headheight: #1}
        \fi
        \headheight=#1\relax
}
\def\@setpgsz@headsep#1{%                "headsep=..."   <-- v2.50
        \if@pgsz@verbose
                \typeout{headsep: #1}
        \fi
        \headsep=#1\relax
}
\def\@setpgsz@foot#1{%                   "foot=..."       <-- v2.50
        \if@pgsz@verbose
                \typeout{footskip: #1}
        \fi
        \footskip=#1\relax
        \@ifundefined{footheight}{}{\ifdim\footskip=\z@
          \footheight\z@\fi}
}
\def\@setpgsz@footskip#1{%              "footskip=..."     <-- v2.50
        \if@pgsz@verbose
                \typeout{footskip: #1}
        \fi
        \footskip=#1\relax
}
\def\@setpgsz@footheight#1{%              "footheight=..."     <-- v2.50
        \@ifundefined{footheight}{%
        \typeout{Warning: footheight is not used.}
        }{%
        \if@pgsz@verbose
                \typeout{footheight: #1}
        \fi
        \footheight=#1\relax
        }
}

\def\@setpgsz@hoffset#1{%       "hoffset=..."
        \if@pgsz@verbose\typeout{paper hoffset: #1}\fi
        \hoffset=#1\relax
}
\def\@setpgsz@voffset#1{%       "voffset=..."
        \if@pgsz@verbose\typeout{paper voffset: #1}\fi
        \voffset=#1\relax
}
\def\@setpgsz@special#1{%          "special=..."
        \edef\@tmparg{#1}
        \ifx\@tmparg\string@jpostcard
                \if@pgsz@verbose\typeout{special: #1}\fi
                \@pgsz@widthtrue
                \@pgsz@heighttrue
                \pgsz@width=100mm\pgsz@height=148mm
        \else
                \if@latextwoe
                \PackageWarningNoLine{pagesize}{Unknown special=`\@tmparg'}
                \else
                \typeout{Pagesize Warning: Unknown special=`\@tmparg'.}
                \fi
        \fi
        \let\@tmparg\@undefined
}
%
%  \pagesize{...}
%
\newcommand{\pagesize}[1]{
        \pgsz@init
        \parse@pgsz@params{#1}
%-- landscape
        \if@pgsz@landscape
                \if@pgsz@verbose\typeout{paper: landscape}\fi
                \if@pgsz@landscapedone\else
                        \@pgsz@landscapedonetrue
                        \@tempdima\pgsz@paperwidth
                        \pgsz@paperwidth\pgsz@paperheight
                        \pgsz@paperheight\@tempdima
                \fi
        \else
                \if@pgsz@verbose\typeout{paper: portrait}\fi
        \fi
        \@ifundefined{paperwidth}{}{\paperwidth\pgsz@paperwidth}%    <-- v2.51
        \@ifundefined{paperheight}{}{\paperheight\pgsz@paperheight}% <-- v2.51
%-- (h|v)scale
        \textwidth=\pgsz@hscale\pgsz@paperwidth
        \textheight=\pgsz@vscale\pgsz@paperheight
        \@tempdimb\textheight
        \advance\textheight-\headsep
        \advance\textheight-\headheight
        \advance\textheight-\footskip
%-- (h|v)margin
        \if@pgsz@hmargin
                \textwidth=\pgsz@paperwidth
                \advance\textwidth-2\pgsz@hmargin
        \fi
        \if@pgsz@vmargin
                \textheight=\pgsz@paperheight
                \advance\textheight-2\pgsz@vmargin
                \@tempdimb\textheight
                \advance\textheight-\headsep
                \advance\textheight-\headheight
                \advance\textheight-\footskip
        \fi
%-- width and height are given top priority --
        \if@pgsz@width
                \textwidth\pgsz@width
        \fi
        \if@pgsz@height
                \textheight\pgsz@height
                \@tempdimb\textheight
                \advance\@tempdimb\headsep
                \advance\@tempdimb\headheight
                \advance\@tempdimb\footskip
        \fi
        \ifdim\textwidth>\pgsz@paperwidth
                \if@latextwoe
                \PackageWarningNoLine{pagesize}{Text width overflow}
                \else
                \typeout{Pagesize Warning: Text width overflow.}
                \fi
        \fi
        \ifdim\textheight>\pgsz@paperheight
                \if@latextwoe
                \PackageWarningNoLine{pagesize}{Text height overflow}
                \else
                \typeout{Pagesize Warning: Text height overflow.}
                \fi
        \fi
%
%-- horizontal centering --
%
        \oddsidemargin\pgsz@paperwidth
        \advance\oddsidemargin-\textwidth
        \oddsidemargin=.5\oddsidemargin
        \advance\oddsidemargin-1in
        \evensidemargin\oddsidemargin
        \if@twoside
                \if@pgsz@verbose\typeout{paper: twoside}\fi
                \advance\oddsidemargin+\pgsz@twosideshift
                \advance\evensidemargin-\pgsz@twosideshift
        \fi
%
%-- vertical centering --
%
        \topmargin\pgsz@paperheight
        \advance\topmargin-\@tempdimb
        \topmargin=.5\topmargin
        \advance\topmargin-1in
%
%-- epilog --
%
        \let\string@true\@undefined
        \let\string@false\@undefined
        \let\string@aiii\@undefined\let\string@aiiipaper\@undefined
        \let\string@aiv\@undefined\let\string@aivpaper\@undefined
        \let\string@av\@undefined\let\string@avpaper\@undefined
        \let\string@biii\@undefined\let\string@biiipaper\@undefined
        \let\string@biv\@undefined\let\string@bivpaper\@undefined
        \let\string@bv\@undefined\let\string@bvpaper\@undefined
        \let\string@let\@undefined
        \let\string@leg\@undefined
        \let\string@exe\@undefined
        \let\string@landscape\@undefined
        \let\string@jpostcard\@undefined

        \if@pgsz@verbose\immediate\typeout{}\fi
}
% end of \pagesize definition

\pagesize{}% default execution

\endinput
%------------------------------------End of `pagesize.sty'.
%
% User interface: 
%
%       \pagesize{...}
%       This must be placed in the preamble.
%
% Examples:
%
%       \pagesize{paper=a4paper,hscale=0.8,vscale=0.9}
%               ,which is actually equivalent to "\pagesize{}", 
%               sets paper size=A4,
%               \textwidth=0.8*(width of A4 paper),
%               \textheight=0.9*(height of A4 paper)-(head)-(foot),
%               and makes the text-region centering in the A4 paper.
%
%       \pagesize{verbose=,scale=0.98}
%               is equivalent to "hscale=0.98,vscale=0.98"
%               and switches to the verbose mode.
%               
%       \pagesize{scale=0.98,hoffset=2mm,voffset=-4mm}
%               shifts the region horizontally and vertically.
%               
%       \pagesize{landscape=true,width=20cm}
%               sets \textwidth=20cm, leaves \textheight with default
%               value(see above), and makes the region centering 
%               in the landscaped A4 paper.
%               
%       \pagesize{special=jpostcard}
%               specifies the writable region with japanese official
%               postcard. (100 x 148[mm])
%
%       \pagesize{width=12cm,height=20cm,head=0pt,foot=0pt}
%               sets \textwidth=12cm and \textheight=20cm 
%               without head and foot regions, and then
%               makes it centering in the paper A4.
%               ( Caution: Setting large `height' may cause 
%                 foot and/or head regions stick out of paper. 
%                 If you don't use head and foot regions, 
%                 then setting "head=0pt,foot=0pt" is the best way.)
%
%       \pagesize{hmargin=2cm,vmargin=3cm}
%               sets \textwidth=(width of A4 paper)-(2cm x 2) and
%               \textheight=(width of A4 paper)-(3cm x 2)-(head)-(foot),
%               and then makes it centering in the paper A4.
%
%       \pagesize{twoside=}
%               sets \@twosidetrue but still 
%               \oddsidemargin = \evensidemargin.
%
%       \pagesize{twoside=1cm}
%               sets \@twosidetrue, modifies margins such that
%               \oddsidemargin  <== \oddsidemargin  + 1cm
%               \evensidemargin <== \evensidemargin - 1cm.
%
% Notes:
%
%    o  If you specify `\usepackage{pagesize}' for LaTeX2e or `pagesize'
%       as an option style for LaTeX209, then \pagesize{} will be executed
%       as a default, which equivalent to
%               \pagesize{paper=a4paper,hscale=0.8,vscale=0.9}.
%
%    o  If width or height which you set is longer than the paper's
%       width or height respectively, warning message is typed out.
%
%    o  The verbose option, if necessary, must precede any others.
%
%   < For LaTeX2e >
%    o  The following package options are available:
%               verbose, landscape, twoside,
%               a3,a3paper,a4,a4paper,a5,a5paper,
%               b3,b3paper,b4,b4paper,b5,b5paper.
%               letterpaper,legalpaper,executivepaper.
%       You can use like
%               \usepackage[verbose,a4paper,landscape]{pagesize}.
%    o  You can use `landscape' as a class option, say
%               \documentclass[landscape]{article}
%               \usepackage{pagesize}
%       yields A4 landscape mode.
%
% Options:
% KEY           VALUE
%==================================================================
% paper         a3paper or a3   (297 x 420)[mm]
%               a4paper or a4   (210 x 297)[mm] ... default
%               a5paper or a5   (148 x 210)[mm]
%               b3paper or b3   (353 x 500)[mm]
%               b4paper or b4   (250 x 353)[mm]
%               b5paper or b5   (176 x 250)[mm]
%               letterpaper     (8.5 x 11 )[in]
%               legalpaper      (8.5 x 14 )[in]
%               executivepaper  (7.25x10.5)[in]
%
% verbose       "true" (or none)
%               "false"                   ... default
%
% landscape     "true" (or none)
%               "false"                   ... default
%
% twoside       "true" (or none)
%               "false"                   ... default
%               <dimen> shift from odd and evenside margin
%--------- 
%                                                       (default)
% hscale        <rate>  rate of text to paper width     (= 0.8)
% vscale        <rate>  rate of text to paper height    (= 0.9)
% scale         <rate>  hscale=vscale=<rate>
%
% width         <dimen> text width
% height        <dimen> text height
%
% hmargin       <dimen> horizontal margin (ie. top=bottom=<dimen>)
% vmargin       <dimen> vertical margin   (ie. left=right=<dimen>)
% margin        <dimen> hmargin=vmargin=<dimen>
%---------
%                                                       (default)
% hoffset       <dimen> horizontal offset               (= 0pt)
% voffset       <dimen> vertical offset                 (= 0pt)
%
% headheight    <dimen> headheight
% headsep       <dimen> headsep
% head           same as "headheight",
%                except that if <dimen>=0pt, \headsep=0pt.
%
% footskip      <dimen> footskip
% footheight    <dimen> footheight if defined.
% foot           same as "footskip", 
%                except that if <dimen>=0pt, \footheight=0pt (if defined).
%
% special       "jpostcard"   Japanese official postcard
%                             (width=100mm,height=148mm)
%
%==================================================================
%%%
%%%      Demo
%%%
% LaTeX2e
\documentclass{article}
\usepackage{pagesize}
%\usepackage[verbose,a5paper,landscape]{pagesize}% with options

% LaTeX209
%\documentstyle[pagesize]{article}

%\pagesize{}% default
%\pagesize{paper=a4paper,hscale=0.8,vscale=0.9}% default
%\pagesize{hscale=0.9,vscale=0.75}
%\pagesize{verbose=,scale=0.98}
%\pagesize{scale=0.98,hoffset=2mm,voffset=-4mm}
%\pagesize{landscape=true,width=20cm}
%\pagesize{special=jpostcard}
%\pagesize{width=12cm,height=20cm,head=0pt,foot=0pt}
%\pagesize{hmargin=2cm,vmargin=3cm}
%\pagesize{twoside=1cm}

\pagesize{width=12cm,height=20cm}

\begin{document}
\makeatletter
\def\@oddhead{\hbox to\textwidth{\strut\vrule\hrulefill
 \ header\ \hrulefill\vrule}}\let\@evenhead\@oddhead
\def\@oddfoot{\hbox to\textwidth{\strut\vrule\hrulefill
 \ footer\ \hrulefill\vrule}}\let\@evenfoot\@oddfoot
\def\showlayout{\noindent
\hbox to\textwidth{\strut\vrule\hrulefill
\ the first line of text\ \hrulefill\vrule}
\vfill\begin{center}\LARGE pagesize.sty\end{center}
\vfill\noindent\hbox to\textwidth{\strut\vrule\hrulefill
\ the last line of text\ \hrulefill\vrule}}
\makeatother

\showlayout

\end{document}
