%-------------------------------------------------------------------------------
%-------------------------------------------------------------------------------
% Identification
%-------------------------------------------------------------------------------
%-------------------------------------------------------------------------------
\def\fileversion{3.7}
\def\filedate{1999/06/12}
\def\filename{pageup}
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{\filename}%
	[\filedate\space vs\fileversion\space Meervoudige pagina uitvoer]
%-------------------------------------------------------------------------------
% grotendeels gebaseerd op 2up.tex vs.1.1
% door Timothy Van Zandt, tvz@Princeton.EDU
%-------------------------------------------------------------------------------
%-------------------------------------------------------------------------------
% Waarde uit dimenregister halen
%-------------------------------------------------------------------------------
%-------------------------------------------------------------------------------
% zet dimen register in getallen om:
% dimen=12.345pt -> resp. "12.345", "12", "345"
% let op! de namen @dimvalue etc mogen 'p' en 't' niet bevatten.
{%	lokale code met globale definities
	\@makeother{p}\@makeother{t}%
	\gdef\@fdimval#1pt{#1}%
	\gdef\@ndimval#1.#2pt{#1}%
	\gdef\@mdimval#1.#2pt{#2}%
}%	eind lokale code
\providecommand*{\dimenvalue}[1]{\expandafter\@fdimval\the#1}		  % full value
\providecommand*{\dimenentier}[1]{\expandafter\@ndimval\the#1}		% entier
\providecommand*{\dimenfraction}[1]{\expandafter\@mdimval\the#1}	% remainder
%-------------------------------------------------------------------------------
\@ifundefined{seprulethickness}{%
	\newdimen\seprulethickness
	\seprulethickness=.4pt}{}
%-------------------------------------------------------------------------------
% default settings included
\newif\if@pageseprules					% no rules between pages
\def\@pageupchoice{}      			% type of page combination
\newcommand{\pageupmag}{1000}		% reduction magstep number
\newcount\katern          			% nr of booklet page
%-------------------------------------------------------------------------------
%-------------------------------------------------------------------------------
% (1) side-by-side
\newcommand\@twoup{\switch@shipout}
\@onlypreamble\@twoup
%-------------------------------------------------------------------------------
% (2) top-bottom
\newcommand\@twotop{\switch@shipout
	\@targetwidth\inv@mag\paperwidth
	\@targetheight\inv@mag\paperheight
	}
\@onlypreamble\@twotop
%-------------------------------------------------------------------------------
% (3) four-on-a-page in order: 	1 - 2
%     							3 - 4
\newcommand\@fourup{\@twotop
	\@targetheight.5\@targetheight
	}
\@onlypreamble\@fourup
%-------------------------------------------------------------------------------
% gevulde katernen in de trant: 8--1,2--7,6--3,4--5
% dus b.v. katern met 4 bladen = 16 bladzijden
\newcommand\@booklet{\switch@shipout
	% reserve box registers for half of the pages (= 2 x katern size)
	\advance\katern\katern
	\count@\katern
	\loop
	\ifnum\count@>\z@
	  \expandafter\newbox\csname bookletbox\the\count@\endcsname
	  \advance\count@\m@ne
	\repeat
	\advance\katern\katern
	\multiply\maxdeadcycles\katern
		% install functions
	\def\ship@@@leftpage{\save@booklet\@leftpage}%
	\def\ship@@@rightpage{\save@booklet\@rightpage}%
	}
\@onlypreamble\@booklet
%-------------------------------------------------------------------------------
%-------------------------------------------------------------------------------
% Options
\DeclareOption{seprule}{\@pageseprulestrue}
\DeclareOption{noseprule}{\@pageseprulesfalse}
%
\DeclareOption{twoup}{\let\@pageupchoice=\@twoup}
\DeclareOption{twotop}{\let\@pageupchoice=\@twotop}
\DeclareOption{fourup}{\let\@pageupchoice=\@fourup}
\DeclareOption{booklet}{\let\@pageupchoice=\@booklet}
%
\DeclareOption{unreduced}{\edef\pageupmag{\magstep0}}
\DeclareOption{reduced}{\edef\pageupmag{\magstep1}}
\DeclareOption{Reduced}{\edef\pageupmag{\magstep2}}
\DeclareOption{REduced}{\edef\pageupmag{\magstep3}}
\DeclareOption{REDuced}{\edef\pageupmag{\magstep4}}
\DeclareOption{REDUCED}{\edef\pageupmag{\magstep5}}
%
\DeclareOption{tinyquire}{\katern=2\relax}
\DeclareOption{thinquire}{\katern=3\relax}
\DeclareOption{normalquire}{\katern=4\relax}
\DeclareOption{thickquire}{\katern=5\relax}
\DeclareOption{fatquire}{\katern=6\relax}
%
\DeclareOption*{\PackageError{\filename}{Unknown option `\CurrentOption'}}
%
%-------------------------------------------------------------------------------
\ExecuteOptions{noseprule,unreduced,normalquire}
\ProcessOptions
\AtEndOfPackage{\@pageupchoice}
%-------------------------------------------------------------------------------
%-------------------------------------------------------------------------------
% More Declarations
%
% since at this point (after \ProcessOptions) is known which functions
% are needed, only these are defined. By this we inhibite unhealthy
% on the fly redefinition of the output scheme.
%-------------------------------------------------------------------------------
%-------------------------------------------------------------------------------
\@ifundefined{pageheight}%
	{\newlength{\pageheight}\setlength{\pageheight}{\paperheight}}{}
\@ifundefined{pagewidth}%
	{\newlength{\pagewidth}\setlength{\pagewidth}{\paperwidth}}{}
%-------------------------------------------------------------------------------
% installeer registers en initialiseer
\newcount\phys@page
\newlength{\@targetwidth}
\newlength{\@targetheight}
\newbox\@leftpage
\newbox\@rightpage
\newif\if@leftpage 		% signaleert dat een linkerpagina present is
\newif\if@tophalf
%-------------------------------------------------------------------------------
% schakel over naar twoup shipout
% deze routine wordt in alle macros gebruikt
% let op:	in \end{document} eerst \@enddocumenthook dan pas \clearpage;
%			handhaaf dit schema (voor het geval extra toevoegingen gepleegd!)
%			trigger op dat moment pas \twoup@eject via \clearpage
% let op:	herdefinitie \protect voorkomt expansie \contentsline in \write
% \@targetwidth and \@targetheight set to the *unmagnified* dimensions
% of the target page; \inv@mag is inverse of the magnification
\newcommand\switch@shipout{%
	\let\normal@shipout\shipout
	\let\shipout\twoup@shipout
	\gdef\twoupeject{\clearpage\twoup@eject}%
	\AtEndDocument{%
		\let\@@clearpage=\clearpage
		\renewcommand{\clearpage}{%
		{\let\protect\@unexpandable@protect\@@clearpage\twoup@eject}}}%
	\count@\pageupmag\relax
	\dimen@\@M\p@
	\divide\dimen@\count@ 
	\multiply\dimen@ 100
	\mag\dimenentier\dimen@
	\dimen@\count@\p@
	\divide\dimen@\@m
	\edef\inv@mag{\dimenvalue\dimen@}%
	\setlength{\@targetwidth}{\inv@mag\paperheight}%
	\setlength{\@targetheight}{\inv@mag\paperwidth}%
	}
\@onlypreamble\switch@shipout
%-------------------------------------------------------------------------------
% By default, TeX's \shipout primitive is modified so that it saves 
% each page and prints out every two, using the \ship@twoup macro.
\newcommand\twoup@shipout{%
	\if@leftpage					% er zit een linkerpagina
		\global\@leftpagefalse
		\def\next{\afterassignment\ship@rightpage\global\setbox\@rightpage=}%
	\else							% er zit geen linkerpagina
		\global\@leftpagetrue
		\def\next{\afterassignment\ship@leftpage\global\setbox\@leftpage=}%
		\if@twoside
			\ifodd\value{page}%
				\global\setbox\@leftpage\null
				\make@@halfpage\@leftpage\ship@@@leftpage
				\def\next{\shipout}%
		\fi\fi
	\fi
	\next}
%-------------------------------------------------------------------------------
% \ship@rightpage invoke \ship@@leftpage at the right time.
% \shipout is followed by an \hbox, \vbox or \vtop,
% in which case \ship@rightpage is invoked after the opening {, or it is
% followed by a \box or \copy, in which case \ship@rightpage is invoked after
% the full assignment. \ship@rightpage figures out which is the case by
% checking whether \@rightpage is void.
%
\newcommand\ship@leftpage{%
	\ifvoid\@leftpage\aftergroup\ship@@leftpage\else\ship@@leftpage\fi}
\newcommand\ship@rightpage{%
	\ifvoid\@rightpage\aftergroup\ship@@rightpage\else\ship@@rightpage\fi}
%
% \ship@@leftpage takes output box and with \make@halfpage turns it into
% a fully-size source page, center this in both dimensions in half of a
% target page with \make@@halfpage. Then ship them.
%
\newcommand\ship@@leftpage{\make@halfpage\@leftpage\ship@@@leftpage}
\newcommand\ship@@rightpage{\make@halfpage\@rightpage\ship@@@rightpage}
\newcommand\make@halfpage[1]{\dp#1\z@
	\setbox#1\vbox to\pageheight{%
		\vss
		\hbox to\pagewidth{\hss\box#1\hss}% \hss before added 97/12/30
		\vss}%
	\make@@halfpage#1}				% note: \make@@halfpage is type dependent!
%-------------------------------------------------------------------------------
% This is the macro that actually ships out each pair of pages.
\newcommand\ship@twoup{%
	\begingroup
    	\voffset-\inv@mag in 		% compensate TeX's 1 inch offsets
    	\hoffset\voffset
    	\global\advance\phys@page\@ne
    	\count\z@\phys@page
    	\normal@shipout\make@fullpage
	\endgroup}						% note: \make@fullpage is type dependent!
\let\ship@@@leftpage=\relax
\let\ship@@@rightpage=\ship@twoup
%-------------------------------------------------------------------------------
% horizontal and vertical rules (geplaatst 'zonder ruimte in te nemen')
\newcommand\pagesep@vertical{%
	\if@pageseprules
		\kern-.5\seprulethickness
		\vbox to\@targetheight{%
			\vss
			\hbox{\vrule\@height.9\@targetheight\@width\seprulethickness}%
			\vss}%
		\kern-.5\seprulethickness
	\fi}
\newcommand\pagesep@horizontal{%
	\if@pageseprules
		\vskip-.5\seprulethickness\relax
		\hbox to\@targetwidth{\hss
			\vrule\@height\seprulethickness\@width.9\@targetwidth\hss}%
    \vskip-.5\seprulethickness\relax
	\fi}
%-------------------------------------------------------------------------------
%-------------------------------------------------------------------------------
% (1) side-by-side
% side-by-side extra definitions
\ifx\@pageupchoice\@twoup
\newcommand\make@@halfpage[1]{%
	\global\setbox#1\vbox to\@targetheight{%
		\vss
		\hbox to.5\@targetwidth{\null\box#1\hss}%
		\vss}%
	}
\newcommand\make@fullpage{\hbox{\box\@leftpage\pagesep@vertical\box\@rightpage}}
\newcommand\twoup@eject{\if@leftpage\shipout\null\fi}
\fi
%-------------------------------------------------------------------------------
% (2) top-bottom
\ifx\@pageupchoice\@twotop
% top-bottom extra definitions
\newcommand\make@@halfpage[1]{%
	\global\setbox#1\vbox to.5\@targetheight{%
		\vss
		\hbox to\@targetwidth{\null\box#1\hss}%
		\vss}%
	}
\newcommand\make@fullpage{%
	\vbox{\offinterlineskip\box\@leftpage\pagesep@horizontal\box\@rightpage}}
\newcommand\twoup@eject{\if@leftpage\shipout\null\fi}
\fi
%-------------------------------------------------------------------------------
% (3) four-on-a-page in order: 	1 - 2
%     				            			3 - 4
\ifx\@pageupchoice\@fourup
\newbox\@tophalfpage
% four-on-page extra definitions
\newcommand\make@@halfpage[1]{%
	\global\setbox#1\vbox to\@targetheight{%
		\vss
		\hbox to.5\@targetwidth{\null\box#1\hss}%
		\vss}%
	}
\newcommand\makefull@sidebyside{\hbox{\box\@leftpage\pagesep@vertical\box\@rightpage}}
\newcommand\make@fullpage{%
	\vbox{\offinterlineskip\box\@leftpage\pagesep@horizontal\box\@rightpage}}
\renewcommand\ship@@@rightpage{%
	\if@tophalf
		\global\setbox\@rightpage\makefull@sidebyside
		\global\setbox\@leftpage\box\@tophalfpage
		\ship@twoup
		\global\@tophalffalse
	\else
		\global\setbox\@tophalfpage\makefull@sidebyside
		\global\@tophalftrue
	\fi}
\newcommand\twoup@eject{% both two- and fourup selections
	\if@tophalf
		\if@leftpage\shipout\null\fi
	\else
		\if@leftpage\shipout\null\fi
		\shipout\null
		\shipout\null
	\fi}
\fi
%-------------------------------------------------------------------------------
% gevulde katernen
\ifx\@pageupchoice\@booklet
%-------------------------------------------------------------------------------
% extra booklet definitions
\newcount\leftpagenumber
\newcount\rightpagenumber
\newcount\bookletpage
%-------------------------------------------------------------------------------
% saving a page or dumping two combined pages
\newcommand\save@booklet[1]{%
		% advance pagenr in katern modulo nr of katern pages
	\ifnum\bookletpage=\katern
		\global\bookletpage\@ne
	\else
		\global\advance\bookletpage\@ne
	\fi
		% saving or dumping decided on 1st-2nd half of katern
	\count@\katern
	\divide\count@\tw@
	\ifnum\bookletpage>\count@
		% combine this page with its partner and dump
		\count@\katern
		\advance\count@\@ne
		\advance\count@-\bookletpage
		\ifodd\bookletpage
		  \setbox\@rightpage\box#1
		  \setbox\@leftpage\box\csname bookletbox\the\count@\endcsname
		\else
		  \setbox\@leftpage\box#1
		  \setbox\@rightpage\box\csname bookletbox\the\count@\endcsname
		\fi
		\ship@twoup
	\else
		% save this page
    \global\setbox\csname bookletbox\the\bookletpage\endcsname\box#1
	\fi}
\newcommand\make@fullpage{%
	\hbox{\box\@leftpage\pagesep@vertical\box\@rightpage}}
%-------------------------------------------------------------------------------
% dumping leftover pages in case of incomplete booklet
\newcommand\twoup@eject{%
	\count@\katern
	\advance\count@-\bookletpage
	\ifnum\count@>\bookletpage \bookletpage\count@ \fi
	\ifnum\bookletpage<\katern
		\loop
		\newpage\null\newpage
		\ifnum\bookletpage<\katern \repeat
	\fi}
\newcommand\make@@halfpage[1]{%
	\global\setbox#1\vbox to\@targetheight{%
		\vss
		\hbox to.5\@targetwidth{\null\box#1\hss}%
		\vss}%
	}
%-------------------------------------------------------------------------------
\fi
%-------------------------------------------------------------------------------
\endinput
