From: zrawi01@mailserv.zdv.uni-tuebingen.de (Jochen Wiedmann)
Subject: Re: 2-up A5 formatting/printing
Organization: Comp. Center (ZDV) U of Tuebingen, FRG
Date: Tue, 10 Nov 1992 09:15:05 GMT

Hi, Graham,

below you will find the 2up.sty. This should work, but you will have to use
dvips in Landscape-Mode.

		Jochen


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2up.tex %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\fileversion{0.9c}
\def\filedate{92/01/30}
%%
%% File 2up.tex, for use with Generic TeX.
%% By Timothy Van Zandt, tvz@Princeton.EDU
%%
% *******************************************************************
% **                         DESCRIPTION                           **
% *******************************************************************
%
% This style option allows one to print a document two-up, with considerable
% flexibility as to paper size and layout. It does this by making a few
% simple changes to TeX's output routine. It does not involve a dvidvi
% converter or PostScript code. It produces a standard dvi file. It should
% work with any TeX macro package (but see the next section).
%
% As long as you have or can generate the needed font bitmaps (e.g., pk
% files), fonts will not be scaled and output quality will be higher than
% when a PostScript filter is used to print a file twoup. However, there is
% no particular advantage to using these macros instead of dvidvi (except
% it makes some calculations a little easier).
%
%
% *******************************************************************
% **                        COMPATIBILITY                          **
% *******************************************************************
%
% These macros should work with most TeX macro packages, except for the
% following possible compatibility problems.
%
% If you have used `true' dimensions in your document, you cannot reduce
% it and you may find it impossible to fit the two pages on a single one.
% `true' dimensions are a no-no if you want to change the magnification
% of a document.
%
% Page cross-references will not be accurate, because finished pages are not
% immediately shipped out.  If you use page cross-references (as opposed,
% say, to an equation cross reference), or an index, or a table of contents,
% etc., follow this procedure:
%
%   1. Get to the point where everything is working right and you are ready
%   to print.
%   2. Run the job without the 2up macros, until all cross-references
%   are properly resolved. Run any index utility at this point.
%   3. Run the job ONE MORE TIME with the 2up macros; it will use the
%   correct cross-references from the previous runs.
% 

% If you do not end documents using the standard Plain TeX \bye command or
% the LaTeX \end{document} command,  then you need to somehow ensure that
% the last target page is printed, using, e.g., \ejecttwouppage. See the
% definitions of \bye and \enddocument at the end of this file for examples.
%
% If you meddle with TeX's \shipout primitive after loading these macros,
% then these macros probably will not work (because they also meddle with
% this primitive).
%
% These macros use only TeX primitives, except for the following commands
% from Plain TeX, that are part of most macro packages:
%   \newbox, \newcount, \newdimen and \newif
%
%
% *******************************************************************
% **                          PARAMETERS                           **
% *******************************************************************
%
% Terminology: The "source" layout is the layout of your document
%              the way it is usually printed.
%              The "target" layout is the layout when it
%              is printed two-up.
%
% After inputing this file, you must set the target and source layout:
%
%      \source{<mag>}{<width>}{<height>}
%      \target{<mag>}{<width>}{<height>}
%
%    a. <mag> is
%       - the integer nearest to 1000 times the magnification, or
%       - \magstep<n> for a magnification of 1.2^n (n=0,...,5), or
%       - \magstepminus<n> for a magnification of 1.2^{-n} (n=0,...,5).
%    To minimize the number of non-standard font magnifications, use one
%     of the last two options, and choose the target magnification closest to 1
%     such that the source pages fit properly in the target output,
%     keeping in mind that each source page is centered in half a target page.
%
%    b. <width> and <height> are the true width and height of the paper,
%     when looking at the output right side up.
%
% You can also specify some options:
%
%     \targetlayout{<options>}
%
% where <options> is a comma separated list with no extraneous spaces. Valid
% options are:
%
%    a. topbottom - The two source pages are printed one on top of the other.
%    b. twosided  - Odd pages are always printed on the right or the bottom.
%    c. booklet   - Pages are printed like a booklet.
%
% For the booklet option, you must have enough memory and box registers
% to hold all the pages in the document.
%
%
% MAKE NO OTHER CHANGES TO YOUR DOCUMENT EXCEPT THE FOLLOWING:
%
% -- You have to see to it that the target output is printed with the
% correct orientation (landscape or portrait).
%
% -- You can, but do not need to, use the following commands:
%
%    -- \twoupemptypage inserts a completely blank half page in the target.
%
%    -- LaTeX users: \cleartwouppage ejects a complete target page.
%
%    -- Plain TeX users: \ejecttwouppage ejects a complete target page.
%
%
% *******************************************************************
% **                          DEFAULTS                             **
% *******************************************************************
%
% The following commands set the parameters for some common sources and
% targets. The definitions of these commands are also useful examples.
%
%
% I. \twouparticle -- THIS IS THE DEFAULT
%
% Source: A portrait document on 8.5x11 inch paper with wide margins as
% used in LaTeX's article style, and without marginal notes.
%
% Target: A landscape document on the same size paper, with the source
% pages printed side by side.
%
% Definition:
%
%    \target{\magstepminus1}{11in}{8.5in}
%    \source{\magstep0}{8.5in}{11in}}
%
% If you use narrower side margins, or if you use marginal notes, then you
% will need to to reduce the magnication to \magstepminus2 or lower or use
% \twouplegaltarget, defined below.
%
%
% II. \twouplegaltarget.
%
% Source: A portrait document on 8.5x11 inch paper with just about any
% margins and perhaps with marginal notes.
%
% Target: A landscape document on legal size paper (8.5x14 inches),
% with the source pages printed side by side.
%
% Definition: 

%
%   \target{\magstepminus1}{14in}{8.5in}%
%   \source{\magstep0}{8.5in}{11in}%
%
%
% III. \twouplandscape.
%
% Source: A landscape document on 8.5x11 inch paper with just about any
% margins.
%
% Target: A portrait document on the same size paper, with the source pages
% stacked vertically.
%
% Definition:
%
%   \target{\magstepminus2}{8.5in}{11in}%
%   \source{\magstep0}{11in}{8.5in}%
%   \targetlayout{topbottom}}
%
%
% *******************************************************************
% **                         CHANGES                               **
% *******************************************************************
%
% V0.9a: Everything redone. Too many differences to point out. Main
% changes to user interface are that \targetpaperwidth is now
% \targetwidth, etc., and there is a new twosidebyside layout. Previous
% version was for LaTeX only.
%
% V0.9b: Interface redone again. booklet option added.
%
% V0.9c: Pages ship out a little earlier. A few bugs fixed.
%
% *******************************************************************
% **                      IMPLEMENTATION                           **
% *******************************************************************
%
\edef\theatcode{\the\catcode`\@}
\ifnum\theatcode=13\let\theat @\fi
\catcode`\@=11\relax
%
\message{\space\space v\fileversion\space\space \filedate\space\space <tvz>}
%
% Parameter registers:
\newcount\@targetmag
\newcount\@sourcemag
\newdimen\@targetwidth
\newdimen\@targetheight
\newdimen\@sourcewidth
\newdimen\@sourceheight
\newdimen\twoup@hoffset
\newdimen\twoup@voffset
\newif\if@sidebyside
\@sidebysidetrue
\newif\if@twosided
%
% Registers used by output routine.
\newif\if@leftpage
\@leftpagetrue
\newbox\@leftpage
\newbox\@rightpage
%
% Registers used with the booklet option:
\newcount\bookletpage
\bookletpage=0
\newcount\leftpagenumber
\newcount\rightpagenumber
%
% A useful extension of the \magstep macro.
\def\magstepminus#1{%
  \ifcase#1 \@m\or 833\or 694\or 579\or 482\or 401\fi\relax}
%
% \@targetwidth and \@targetheight are set to the *unmagnified* dimensions
% of the target page (hence the division by \@targetmag and multiplication
% by 1000).
\def\target#1#2#3{%
  \mag #1\relax\@targetmag #1
  \@targetwidth 10in
  \divide\@targetwidth by \@targetmag
  \multiply\@targetwidth by 100
  \edef\@targetinch{\the\@targetwidth}%
  \@targetwidth #2
  \divide\@targetwidth by \@targetmag
  \multiply\@targetwidth by 1000
  \@targetheight #3
  \divide\@targetheight by \@targetmag
  \multiply\@targetheight by 1000\relax}
%
% \@sourcewidth and \@sourceheight are set to the *unmagnified* dimensions
% of the source page when measured *without* TeX's 1 inch margins.
\def\source#1#2#3{%
  \@sourcemag #1
  \@sourcewidth #2
  \advance\@sourcewidth by -2in
  \divide\@sourcewidth by \@sourcemag
  \multiply\@sourcewidth by 1000
  \@sourceheight #3
  \advance\@sourceheight by -2in
  \divide\@sourceheight by \@sourcemag
  \multiply\@sourceheight by 1000\relax}
%
% \targetlayout does a loop that reads the comma separated arguments.
% There can be no extraneous spaces.
\def\targetlayout#1{\process@targetlayout#1,\@nil,\@nil}
\def\process@targetlayout#1,{%
  \def\@tempa{booklet}%
  \def\@tempb{twosided}%
  \def\@tempc{topbottom}%
  \def\@tempd{\@nil}%
  \def\@tempe{#1}%
  \let\next\relax
  \ifx\@tempa\@tempe
    \let\shipout\shipout@booklet
    \let\twoup@eject\twoup@eject@booklet
    \let\next\process@targetlayout
  \fi
  \ifx\@tempb\@tempe
    \@twosidedtrue
    \let\next\process@targetlayout
  \fi
  \ifx\@tempc\@tempe
    \let\ship@twoup\ship@topbottom
    \let\calc@offsets\calc@topbottom
    \let\next\process@targetlayout
    \@sidebysidefalse
  \fi
  \ifx\@tempd\@tempe
    \def\next##1\@nil{}%
  \fi
  \ifx\next\relax
    \begingroup
      \errhelp{Valid target layouts are "topbottom", "twosided"
            and "booklet".}
      \errmessage{`#1' is invalid 2up target layout - ignored.}
    \endgroup
    \let\next\process@targetlayout
  \fi
  \next}
%
% This is default macro for calculating the horizontal and vertical offsets
% for positioning each half-page.
\def\calc@offsets{%
  \begingroup
    \globaldefs=1
    \twoup@hoffset .5\@targetwidth
    \advance\twoup@hoffset by -\@sourcewidth
    \divide\twoup@hoffset by 2
    \twoup@voffset \@targetheight
    \advance\twoup@voffset by -\@sourceheight
    \divide\twoup@voffset by 2
    \let\calc@offsets\relax
  \endgroup}
% This is the macro for calculating offsets for the topbottom layout:
\def\calc@topbottom{%
  \begingroup
    \globaldefs=1
    \twoup@hoffset \@targetwidth
    \advance\twoup@hoffset by -\@sourcewidth
    \divide\twoup@hoffset by 2\relax
    \twoup@voffset .5\@targetheight
    \advance\twoup@voffset by -\@sourceheight
    \divide\twoup@voffset by 2
    \let\calc@offsets\relax
  \endgroup}
%
% By default, TeX's \shipout primitive is modified so that it saves each page
% and prints out every two, using the \ship@twoup macro.
\let\normal@shipout\shipout
\def\shipout{%
  \calc@offsets
  \if@leftpage
    \global\@leftpagefalse
    \def\next{\global\setbox\@leftpage=}%
    \if@sidebyside
      \if@twosided
        \ifodd\count\z@
          \global\setbox\@leftpage=\vbox{}%
          \def\next{\shipout}%
        \fi
      \fi
    \fi
  \else
    \global\@leftpagetrue
    \def\next{\afterassignment\ship@rightpage\global\setbox\@rightpage=}%
    \if@sidebyside
      \if@twosided
        \ifodd\count\z@
        \else
          \global\setbox\@rightpage=\vbox{}%
          \ship@twoup
          \def\next{\shipout}%
        \fi
      \fi
    \fi
  \fi
  \next}
%
% \ship@rightpage assigns the box to \@rightpage, and then invokes \ship@twoup
% to ship out the full page. \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.
\def\ship@rightpage{%
  \ifvoid\@rightpage\aftergroup\ship@twoup\else\ship@twoup\fi}
%
% With the booklet option, all the pages are accumulated, and are then
% printed with the \ship@booklet, \booklet@loop and \ship@twoup macros.
% The \begingroup\let\newbox\relax ... \endgroup business is one way of
% overcoming the fact that \newbox may be defined to be outer.
\begingroup\let\newbox\relax
\gdef\shipout@booklet{%
  \let\calc@offsets\relax
  \global\advance\bookletpage by 1
  \expandafter\newbox\csname bookletbox\the\bookletpage\endcsname
  \def\next{\expandafter\global\expandafter\setbox
    \csname bookletbox\the\bookletpage\endcsname=}
  \if@twosided
    \count@\bookletpage
    \advance\count@ by -\count\z@
    \ifodd\count@
      \expandafter\global\expandafter\setbox
        \csname bookletbox\the\bookletpage\endcsname\hbox{}
      \def\next{\shipout}
    \fi
  \fi
  \next}
\def\ship@booklet{%
  \ifnum\leftpagenumber>\bookletpage
    \setbox\@leftpage=\hbox{}
  \else
    \setbox\@leftpage=\expandafter\box
      \csname bookletbox\the\leftpagenumber\endcsname
  \fi
  \ifnum\rightpagenumber>\bookletpage
    \setbox\@rightpage=\hbox{}
  \else
    \setbox\@rightpage=\expandafter\box
      \csname bookletbox\the\rightpagenumber\endcsname
  \fi
  \count\z@\rightpagenumber
  \ship@twoup
  \booklet@loop}
\endgroup
\def\booklet@loop{%
  \advance\rightpagenumber by 2
  \advance\leftpagenumber by -2
  \ifnum\leftpagenumber<1
  \else
    \ship@booklet
  \fi}
%
% This is the default macro that actually ships out each pair of pages.
\def\ship@twoup{%
  \normal@shipout\vbox{%
    \vskip -\@targetinch
    \hbox{%
      \hskip -\@targetinch
      \hbox to .5\@targetwidth{%
        \hskip \twoup@hoffset
        \vbox to \@targetheight{%
          \vskip \twoup@voffset
          \box\@leftpage
          \vss}%
        \hss}%
      \twoup@pagesep
      \hbox to .5\@targetwidth{%
        \hskip \twoup@hoffset
        \vbox to \@targetheight{%
          \vskip \twoup@voffset
          \box\@rightpage
          \vss}%
        \hss}}}}
\let\twoup@pagesep\relax
%
% This is the macro for shipping out each pair of pages with the
% topbottom option.
\def\ship@topbottom{%
  \normal@shipout\hbox{%
    \hskip -\@targetinch
    \hskip \twoup@hoffset
    \vbox{%
      \offinterlineskip
      \vskip -\@targetinch
      \vbox to .5\@targetheight{%
        \vskip\twoup@voffset\box\@leftpage\vss}%
      \twoup@pagesep
      \vbox to .5\@targetheight{%
        \vskip\twoup@voffset\box\@rightpage\vss}}}}
%
% This one is easy:
\def\twoupemptypage{\shipout\hbox{}}
%
% This clears a whole target page if there is a saved left page. Note that
% this does not invoke the output routine; i.e., it is not like \clearpage
% or \supereject. See \twoupclearpage and \twoupeject below.
\def\twoup@eject{%
  \if@leftpage\else
    \global\setbox\@rightpage\hbox{}\ship@twoup%
  \fi}
  

% This is the definition of \twoup@eject with the booklet option:
\def\twoup@eject@booklet{%
  \leftpagenumber\bookletpage
  \advance\leftpagenumber by 3
  \divide\leftpagenumber by 4
  \multiply\leftpagenumber by 4
  \rightpagenumber=1
  \ship@booklet}
%
% For LaTeX:
\expandafter\ifx\csname @latexerr\endcsname\relax\else
  \def\twoupclearpage{\clearpage\twoup@eject}
  \expandafter\@temptokena\expandafter{\enddocument}
  \edef\enddocument{\noexpand\twoupclearpage\the\@temptokena}
\fi
%
% For Plain TeX:
\def\twoupeject{\par\vfil\supereject\twoup@eject}
\outer\def\bye{\twoupeject\end}  

%
% Defaults:
\def\twouparticle{%
  \target{\magstepminus1}{11in}{8.5in}%
  \source{\magstep0}{8.5in}{11in}}
\def\twouplegaltarget{%
  \target{\magstepminus1}{14in}{8.5in}%
  \source{\magstep0}{8.5in}{11in}}%
\def\twouplandscape{%
  \target{\magstepminus2}{8.5in}{11in}%
  \source{\magstep0}{11in}{8.5in}%
  \targetlayout{topbottom}}
\twouparticle
%
\expandafter\catcode`\@=\theatcode\relax
\ifnum\theatcode=13\let @\theat\fi
\endinput
%%%%%%%%%%%%%%%%%%%%%%%%% END 2up.tex %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

--
Jochen Wiedmann			E-Mail: wiedmann@mailserv.zdv.uni-tuebingen.de