%%%
%%%	PAGESIZE.STY
%%%
%%%	A LaTeX style file for easy layout of page
%%%
%%%	Copyright (C) 1994 by Hideo Umeki. All rights reserved.
%%%	email:	umeki@ssel.toshiba.co.jp
%%%
\def\fileversion{v1.2}
\def\filedate{94/06/09}
%
% This is the "pagesize" style option which allows you set size of text
% region easily and automatically makes it centering in the paper.
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation.
%
% We define a "\pagesize{...}" macro which specifies page layout.
% This should be placed in the preamble, i.e. before "\begin{document}".
%
% Examples:
%	\pagesize{paper=a4,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{scale=0.98}
%		is equivalent to "hscale=0.98,vscale=0.98".
%		
%	\pagesize{scale=0.98,hoffset=2mm,voffset=-4mm}
%		shifts the region horizontally and vertically.
%		
%	\pagesize{orientation=landscape,width=20cm}
%		sets \textwidth=20cm, leaves \textheight with default
%		value(see above), and makes the region centering 
%		in the landscaped A4 paper.
%		
%	\pagesize{type=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 "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.
%
% NOTES:
%	If you don't use \pagesize macro, nothing will be done.
%	so if you want to make the default setting active,
%	just do \pagesize{} in the preamble.
%
%	If width or height which you set is longer than the paper's
%	width or height respectively, warning message is typed out.
%
%
% We also define \pagesizeverbose.
% If you set this control sequence before "\pagesize{..}",
% then \typeout commands in the styles become active and 
% selected keys and values are reported. 
% In default, no messages are typed out. 
%
%
% Details:
% KEYS 	 	VALUES
%==================================================================
% paper		a4	(210 x 294)[mm]		... DEFAULT!
%		a5	(148 x 210)[mm]
%		b4	(257 x 363)[mm]
%		b5	(182 x 257)[mm]
%
% orientation	portrait			... DEFAULT!
%		landscape
%--------- 
%							(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 (at both sides)
% vmargin	<dimen>	vertical margin (at both sides)
% margin	<dimen>	hmargin=vmargin=<dimen>
%---------
%							(default)
% hoffset	<dimen>	horizontal offset		(= 0pt)
% voffset	<dimen>	vertical offset			(= 0pt)
%
% head		<dimen> headsep  (if <dimen>=0pt, \headheight=0pt too.)
% foot		<dimen>	footskip
%
% type		(j)postcard	Japanese official postcard
%				(width=100mm,height=148mm)
%==================================================================
\@ifundefined{pgsz@width}{}{\endinput}
\typeout{Style-Option: `pagesize' \fileversion
         \space\space <\filedate>.}
%%
\newif\ifpagesizeverbose\pagesizeverbosefalse
\def\pagesizeverbose{\pagesizeverbosetrue}

\newif\if@pgsz@paper
\newif\if@pgsz@width
\newif\if@pgsz@height
\newif\if@pgsz@hmargin
\newif\if@pgsz@vmargin
\newif\if@pgsz@landscape
\newdimen\pgsz@paper@width
\newdimen\pgsz@paper@height
\newdimen\pgsz@width
\newdimen\pgsz@height
\newdimen\pgsz@hmargin
\newdimen\pgsz@vmargin
\newdimen\pgsz@totheight
\def\string@aiv{a4}
\def\string@av{a5}
\def\string@biv{b4}
\def\string@bv{b5}
\def\string@landscape{landscape}
\def\string@postcard{postcard}
\def\string@jpostcard{jpostcard}
%
\def\@warningpgsz@paper#1{%
\typeout{Pagesize Warning: Unknown paper size `#1'. A4-size is set.}}
\def\@warningpgsz@type#1{%
\typeout{Pagesize Warning: Unknown type `#1'.}}
\def\@warningpgsz@orientation#1{%
\typeout{Pagesize Warning: Unknown orientation `#1'. Portrait is set.}}
\def\@warningpgsz@widthover{%
\typeout{Pagesize Warning: Text width overflow.}}
\def\@warningpgsz@heightover{%
\typeout{Pagesize Warning: Text height overflow.}}
%
% initialize
%
\def\pgsz@init{%
	\@pgsz@paperfalse
	\@pgsz@widthfalse
	\@pgsz@heightfalse
	\@pgsz@hmarginfalse
	\@pgsz@vmarginfalse
	\@pgsz@landscapefalse
	\hoffset\z@\voffset\z@
	\pgsz@paper@width=210mm  % A4
	\pgsz@paper@height=297mm % A4
	\def\pgsz@hscale{.8}
	\def\pgsz@vscale{.9}
}
%
% parse parameters
%
\def\parse@pgsz@params#1{%
\@for\@pgszparam:=#1\do{\expandafter\pgsz@setparams\@pgszparam,}}
\def\pgsz@setparams#1=#2,{\@nameuse{@setpgsz@#1}{#2}}
%
% define \@setpgsz@*** 's
%
\def\@setpgsz@paper#1{%		"paper=..."
	\@pgsz@papertrue
	\edef\@tmparg{#1}
	\ifx\@tmparg\string@aiv
		\ifpagesizeverbose\typeout{paper size: A4}\fi
		\pgsz@paper@width=210mm\pgsz@paper@height=297mm
	\else\ifx\@tmparg\string@av
		\ifpagesizeverbose\typeout{paper size: A5}\fi
		\pgsz@paper@width=148mm\pgsz@paper@height=210mm
	\else\ifx\@tmparg\string@biv
		\ifpagesizeverbose\typeout{paper size: B4}\fi
		\pgsz@paper@width=257mm\pgsz@paper@height=363mm
	\else\ifx\@tmparg\string@bv
		\ifpagesizeverbose\typeout{paper size: B5}\fi
		\pgsz@paper@width=182mm\pgsz@paper@height=257mm
	\else\@warningpgsz@paper{\@tmparg}
	\fi\fi\fi\fi
}
\def\@setpgsz@orientation#1{%	"orientation=..."
	\edef\@tmparg{#1}
	\ifx\@tmparg\string@landscape
		\@pgsz@landscapetrue
	\else\ifx\@tmparg\string@portrait
		\@pgsz@landscapefalse
	\else\@warningpgsz@orientation{\@tmparg}
	\fi\fi
}
\def\@setpgsz@hscale#1{%	"hscale=..."
	\ifpagesizeverbose
		\typeout{text hscale: #1 x paperwidth}
	\fi
	\def\pgsz@hscale{#1}
}
\def\@setpgsz@vscale#1{%	"vscale=..."
	\ifpagesizeverbose
		\typeout{text vscale: #1 x paperheight%
			(incl. head and foot)}
	\fi
	\def\pgsz@vscale{#1}
}
\def\@setpgsz@scale#1{%		"scale=..."
	\ifpagesizeverbose
		\typeout{text scale: #1 x papersize%
			(width and height)}
	\fi
	\def\pgsz@hscale{#1}
	\def\pgsz@vscale{#1}
}
\def\@setpgsz@width#1{%		"width=..."
	\ifpagesizeverbose\typeout{text width: #1}\fi
	\@pgsz@widthtrue
	\pgsz@width=#1\relax
}
\def\@setpgsz@height#1{%	"height=..."
	\ifpagesizeverbose\typeout{text height: #1}\fi
	\@pgsz@heighttrue
	\pgsz@height=#1\relax
}
\def\@setpgsz@hmargin#1{%	"hmargin=..."
	\ifpagesizeverbose
		\typeout{paper hmargin: #1 (both sides)}
	\fi
	\@pgsz@hmargintrue
	\pgsz@hmargin=#1\relax
}
\def\@setpgsz@vmargin#1{%	"vmargin=..."
	\ifpagesizeverbose
		\typeout{paper vmargin: #1 (both sides)}
	\fi
	\@pgsz@vmargintrue
	\pgsz@vmargin=#1\relax
}
\def\@setpgsz@margin#1{%	"margin=..."
	\ifpagesizeverbose
		\typeout{paper margin: #1 (all sides)}
	\fi
	\@pgsz@hmargintrue
	\@pgsz@vmargintrue
	\pgsz@hmargin=#1\relax
	\pgsz@vmargin=#1\relax
}
\def\@setpgsz@head#1{%		"head=..."
	\ifpagesizeverbose
		\typeout{headsep: #1}
	\fi
	\headsep=#1\relax
	\ifdim\headsep=\z@ \headheight\z@ \fi
}
\def\@setpgsz@foot#1{%		"foot=..."
	\ifpagesizeverbose
		\typeout{footskip: #1}
	\fi
	\footskip=#1\relax
%%%%	\ifdim\footskip=\z@ \footheight\z@ \fi
}
\def\@setpgsz@hoffset#1{%	"hoffset=..."
	\ifpagesizeverbose\typeout{paper hoffset: #1}\fi
	\hoffset=#1\relax
}
\def\@setpgsz@voffset#1{%	"voffset=..."
	\ifpagesizeverbose\typeout{paper voffset: #1}\fi
	\voffset=#1\relax
}
\def\@setpgsz@type#1{%		"type=..."
	\ifpagesizeverbose\typeout{pagesize type: #1}\fi
	\edef\@tmparg{#1}
	\@pgsz@widthtrue
	\@pgsz@heighttrue
	\ifx\@tmparg\string@postcard
		\pgsz@width=100mm\pgsz@height=148mm
	\else\ifx\@tmparg\string@jpostcard
		\pgsz@width=100mm\pgsz@height=148mm
	\else\@warningpgsz@type{\@tmparg}
	\fi\fi
}
%
%  \pagesize{...}
%
\def\pagesize#1{
	\pgsz@init
	\parse@pgsz@params{#1}
	\if@pgsz@paper\else
		\ifpagesizeverbose\typeout{paper size: A4}\fi
	\fi
%-- landscape
	\if@pgsz@landscape
		\ifpagesizeverbose\typeout{orientation: landscape}\fi
		\@tempdima\pgsz@paper@width
		\pgsz@paper@width\pgsz@paper@height
		\pgsz@paper@height\@tempdima
	\fi
%-- (h|v)scale
	\textwidth=\pgsz@hscale\pgsz@paper@width
	\textheight=\pgsz@vscale\pgsz@paper@height
	\pgsz@totheight\textheight
	\advance\textheight-\headsep
	\advance\textheight-\headheight
	\advance\textheight-\footskip
%-- (h|v)margin
	\if@pgsz@hmargin
		\textwidth=\pgsz@paper@width
		\advance\textwidth-2\pgsz@hmargin
	\fi
	\if@pgsz@vmargin
		\textheight=\pgsz@paper@height
		\advance\textheight-2\pgsz@vmargin
		\pgsz@totheight\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
		\pgsz@totheight\textheight
		\advance\pgsz@totheight\headsep
		\advance\pgsz@totheight\headheight
		\advance\pgsz@totheight\footskip
	\fi
	\ifdim\textwidth>\pgsz@paper@width
		\@warningpgsz@widthover
	\fi
	\ifdim\textheight>\pgsz@paper@height
		\@warningpgsz@heightover
	\fi
%
%-- horizontal centering --
%
	\oddsidemargin\pgsz@paper@width
	\advance\oddsidemargin-\textwidth
	\oddsidemargin=.5\oddsidemargin
	\advance\oddsidemargin-1in
	\evensidemargin\oddsidemargin
%
%-- vertical centering --
%
	\topmargin\pgsz@paper@height
	\advance\topmargin-\pgsz@totheight
	\topmargin=.5\topmargin
	\advance\topmargin-1in
}
\endinput
% End of file `pagesize.sty'.
%%
%%	pagesize-demo.tex
%%
\documentstyle[pagesize]{article}

\pagesizeverbose   %<--- verbose mode

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

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

\begin{document}

\makeatletter
\def\@oddhead{\hbox to\textwidth{\strut\vrule\hrulefill
\ \sf header\ \hrulefill\vrule}}\let\@evenhead\@oddhead
\def\@oddfoot{\hbox to\textwidth{\strut\vrule\hrulefill
\ \sf footer\ \hrulefill\vrule}}\let\@evenfoot\@oddfoot
\noindent
\hbox to\textwidth{\strut\vrule\hrulefill
\ \sf the first line of text\ 
\hrulefill\vrule}
\vfill\begin{center}\LARGE\bf 
pagesize.sty\\[.5cm]
\fileversion\\[1cm]
\filedate
\end{center}
\vfill\noindent
\hbox to\textwidth{\strut\vrule\hrulefill
\ \sf the last line of text\ 
\hrulefill\vrule}
\makeatother

\end{document}
