%Article: 10393 of comp.text.tex
%From: tvz@zandtwerk.Princeton.EDU (Timothy Van Zandt)
%Newsgroups: comp.text.tex
%Subject: Here is a 2up.sty for LaTeX.
%Message-ID: <14650@princeton.Princeton.EDU>
%Date: 27 Sep 91 19:27:46 GMT
%Organization: Princeton University, Princeton NJ
%
%I seem to remember recent inquiries here or in comp.lang.postscript about
%printing TeX documents two-up. This can be achieved using a PostScript
%filter and/or a dvidvi converter.
%
%Here is another solution. It is a document style option for LaTeX which
%makes a few modifications to LaTeX's output routine. It does not require
%a PostScript output device.
%
%
%---------------------------BEGIN 2up.sty -----------------------------
\def\fileversion{v0.9}
\def\filedate{91/09/26}
%%
%% \CharacterTable
%%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%%   Digits        \0\1\2\3\4\5\6\7\8\9
%%   Exclamation   \!     Double quote  \"     Hash (number) \#
%%   Dollar        \$     Percent       \%%     Ampersand     \&
%%   Acute accent  \'     Left paren    \(     Right paren   \)
%%   Asterisk      \*     Plus          \+     Comma         \,
%%   Minus         \-     Point         \.     Solidus       \/
%%   Colon         \:     Semicolon     \;     Less than     \<
%%   Equals        \=     Greater than  \>     Question mark \?
%%   Commercial at \@     Left bracket  \[     Backslash     \\
%%   Right bracket \]     Circumflex    \^     Underscore    \_
%%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%%   Right brace   \}     Tilde         \~}
%%
%% Document style option `2up', for use with LaTeX v2.09
%% By Timothy Van Zandt, tvz@Princeton.EDU
%%
%% It is recommended that this documentation be kept with the style file.
%%
%% *******************************************************************
%% **                         DESCRIPTION                           **
%% *******************************************************************
%%
%% This is style prints a document two-up. It does this by making a few
%% simple changes to LaTeX's output routine. It does not involve a dvidvi
%% converter or PostScript code. It produces a standard dvi file.
%%
%% 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 high.
%% Non-standard font sizes are needed if the document is reduced.
%% This is often necessary so that the two pages fit on the sheet of paper.
%%
%% 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.
%%
%% Otherwise, this style option should be compatible with any LaTeX styles
%% or options that do not change LaTeX's \@outputpage and \enddocument,
%% macros and that do not modify the commands and parameters that these
%% use in a non-standard way.
%%
%% In this documentation, 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.
%%
%% 
%% *******************************************************************
%% **                          PARAMETERS                           **
%% *******************************************************************
%%
%% After inputing this file, you must set the following parameters
%%    ***       ONCE AND ONLY ONCE, AND IN THIS ORDER.     ***
%%
%%
%% 1. Target magnification. Use the command
%%
%%     \targetmag{<num>}
%%
%% where
%%
%%   a. <num> is integer nearest to 1000 times the magnification, or
%%   b. <num> is \magstep<n> for a magnification of 1.2^n (n=0,...,5), or
%%   c. <num> is \magstepminus<n> for a magnification of 1.2^{-n} (n=0,...,5).
%%
%% To minimize the number of non-standard font magnifications, use (b) or (c),
%% and choose the magnification closest to 1 such that the source pages
%% fit properly in the target output.
%%
%%
%% 2. Source magnification. Use the command
%%
%%     \sourcemag{<num>}
%%
%% where <num> is as in (1), but for the source output.
%%
%%
%% 3. Target paper width and height. Use:
%%
%%    \targetpaperwidth{<width>true<unit>}
%%    \targetpaperheight{<height>true<unit>}
%%
%% where <unit> is cm, in, pt, etc., and <width> and <height> are the
%% true width and height of the paper, when looking at the target output
%% right side up.
%%
%%
%% 4. Source paper width and height. Use:
%%
%%    \sourcepaperwidth{<width><unit>}
%%    \sourcepaperheight{<height><unit>}
%%
%% where <unit>, <width> and <height> are as in (2), but for the source
%% output.
%%
%%
%% 5. Target layout. Use
%%
%%    \targetlayout{<layout>}
%%
%% where <layout> is "topbottom" if the two source pages are to be printed
%% one on top of the other, or "sidebyside" if the two source pages are
%% to be printed side by side. With the sidebyside layout and LaTeX's twoside
%% option, odd pages always appear on the right and even pages appear on the
%% left; blank pages are inserted if necessary.
%%
%%
%%
%% 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.
%%
%% -- You can use \emptytwouppage to insert a completely blank half page in
%% the target, and \cleartwouppage to eject a complete target page. Ordinarily
%% you do not need to use these commands.
%%
%%
%% *******************************************************************
%% **                          DEFAULTS                             **
%% *******************************************************************
%%
%% The following commands set the parameters for some common sources and
%% targets. The definitions of these commands are also useful examples.
%%
%%
%% I. \twouparticle.
%%
%% Source: A portrait document on 8.5x11 inch paper with the standard margins
%% used in the article document style and without marginal notes.
%%
%% Target: A landscape document on the same size paper, with the source
%% pages printed side by side.
%%
%% Definition:
%%
%%    \targetmag{\magstepminus1}
%%    \sourcemag{\magstep0}
%%    \targetpaperwidth{11truein}
%%    \targetpaperheight{8.5truein}
%%    \sourcepaperwidth{8.5in}
%%    \sourcepaperheight{11in}
%%    \targetlayout{sidebyside}
%%
%% If you use narrower side margins than the default ones in the article
%% document style, 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: 
%%
%%    \targetmag{\magstepminus1}
%%    \sourcemag{\magstep0}
%%    \targetpaperwidth{14truein}
%%    \targetpaperheight{8.5truein}
%%    \sourcepaperwidth{8.5in}
%%    \sourcepaperheight{11in}
%%    \targetlayout{sidebyside}
%%
%%
%% 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:
%%
%%    \targetmag{\magstepminus2}
%%    \sourcemag{\magstep0}
%%    \targetpaperwidth{8.5truein}
%%    \targetpaperheight{11truein}
%%    \sourcepaperwidth{11in}
%%    \sourcepaperheight{8.5in}
%%    \targetlayout{topbottom}
%%
%%
%% *******************************************************************
%% **                      IMPLEMENTATION                           **
%% *******************************************************************
%%
%%
\typeout{Document style option: `2up' \fileversion \space\space <tvz>}
%%
%% Parameter registers:
\newcount\@sourcemag
\newdimen\@targetpaperwidth
\newdimen\@targetpaperheight
\newdimen\@sourcepagewidth
\newdimen\@sourcepageheight
\newif\if@sidebyside
\@sidebysidetrue
%%
%% Registers used by output routing.
\newif\if@firstpage
\@firstpagetrue
\newbox\@leftpage
\newbox\@rightpage
%%
\def\targetmag#1{\mag #1}
\def\sourcemag#1{\@sourcemag #1}
\def\targetpaperwidth#1{\@targetpaperwidth #1}
\def\targetpaperheight#1{\@targetpaperheight #1}
%%
%% \@sourcepagewidth and |\@sourcepageheight should be the unmagnified
%% dimensions of the source page when measured without TeX's 1 inch margins.
\def\sourcepaperwidth#1{%
  \@sourcepagewidth #1
  \advance\@sourcepagewidth by -2in
  \divide\@sourcepagewidth by \@sourcemag
  \multiply\@sourcepagewidth by 1000\relax}
\def\sourcepaperheight#1{%
  \@sourcepageheight #1
  \advance\@sourcepageheight by -2in
  \divide\@sourcepageheight by \@sourcemag
  \multiply\@sourcepageheight by 1000\relax}
\def\targetlayout#1{%
  \def\@tempa{sidebyside}%
  \def\@tempb{topbottom}%
  \def\@tempc{#1}
  \ifx\@tempa\@tempc
    \@sidebysidetrue
  \else
    \ifx\@tempb\@tempc
      \@sidebysidefalse
    \else
      \@latexerr{Valid target layouts are "sidebyside" and "topbottom"}\@eha
    \fi
  \fi}
%%
\def\magstepminus#1{%
  \ifcase#1 \@m\or 833\or 694\or 579\or 482\or 401\fi\relax}
\def\twouparticle{%
  \targetmag{\magstepminus1}%
  \sourcemag{\magstep0}%
  \targetpaperwidth{11truein}%
  \targetpaperheight{8.5truein}%
  \sourcepaperwidth{8.5in}%
  \sourcepaperheight{11in}%
  \targetlayout{sidebyside}}
\def\twouplegaltarget{%
  \targetmag{\magstepminus1}%
  \sourcemag{\magstep0}%
  \targetpaperwidth{14truein}%
  \targetpaperheight{8.5truein}%
  \sourcepaperwidth{8.5in}%
  \sourcepaperheight{11in}%
  \targetlayout{sidebyside}}
\def\twouplandscape{%
  \targetmag{\magstepminus2}%
  \sourcemag{\magstep0}%
  \targetpaperwidth{8.5truein}%
  \targetpaperheight{11truein}%
  \sourcepaperwidth{11in}%
  \sourcepaperheight{8.5in}%
  \targetlayout{topbottom}}
%%
%% LaTeX's output page routine is modified so that it saves each page and
%% prints out every two. Each page is centered in a half-page
\def\@outputpage{%
  \if@firstpage
    \global\@firstpagefalse
    \def\next{%
      \@makepagebox\@leftpage
      \stepcounter{page}}%
    \if@sidebyside
      \if@twoside
        \ifodd\count\z@
          \def\next{%
            \@makeemptypage\@leftpage
            \@outputpage}%
        \fi
      \fi
    \fi
  \else
    \global\@firstpagetrue
    \def\next{%
      \@makepagebox\@rightpage
      \@shiptwouppage
      \stepcounter{page}}
    \if@sidebyside
      \if@twoside
        \ifodd\count\z@
        \else
          \def\next{%
            \@makeemptypage\@rightpage
            \@shiptwouppage
            \@outputpage}%
        \fi
      \fi
    \fi
  \fi
  \next}
%%
%% This is the \shipout routine.
\def\@shiptwouppage{
  \if@sidebyside
    \shipout\vbox{%
      \vskip -1truein
      \hbox{\hskip -1truein\box\@leftpage\box\@rightpage}}
  \else
    \shipout\hbox{%
      \hskip -1truein
      \vbox{\offinterlineskip\vskip -1truein \box\@leftpage\box\@rightpage}}
  \fi}
%%
%% \@makepagebox is just like \@outputpage in \LaTeX's standard output
%% routine, except that it saves the page in a box instead of shipping it out.
\def\@makepagebox#1{%
  \begingroup\catcode`\ =10 
    \let\-\@dischyph \let\'\@acci \let\`\@accii \let\=\@acciii
    \if@specialpage 
      \global\@specialpagefalse\@nameuse{ps@\@specialstyle}%
    \fi
    \if@twoside 
      \ifodd\count\z@
        \let\@thehead\@oddhead \let\@thefoot\@oddfoot
        \let\@themargin\evensidemargin
      \else
        \let\@thehead\@evenhead \let\@thefoot\@evenfoot
        \let\@themargin\oddsidemargin
      \fi
    \else
    \fi
    \setbox#1=\vbox to \@sourcepageheight{%
      \normalsize \baselineskip\z@ \lineskip\z@
      \let\par\@@par
      \vskip \topmargin
      \hbox to \@sourcepagewidth{%
        \hskip \@themargin
        \vbox{%
          \setbox\@tempboxa=\vbox to\headheight{%
            \vfil
            \hbox to\textwidth{%
              \let\label\@gobble \let\index\@gobble \@thehead}}%
          \dp\@tempboxa\z@
          \box\@tempboxa
          \vskip \headsep
          \box\@outputbox
          \baselineskip\footskip
          \hbox to\textwidth{%
            \let\label\@gobble \let\index\@gobble  \@thefoot}}
        \hss}
      \vss}%
    \if@sidebyside
      \global\setbox#1=\vbox to \@targetpaperheight{%
        \vss
        \hbox to .5\@targetpaperwidth{\hss\box#1\hss}
        \vss}
    \else
      \global\setbox#1=\vbox to .5\@targetpaperheight{%
        \vss
        \hbox to \@targetpaperwidth{\hss\box#1\hss}
        \vss}
    \fi
    \global\@colht\textheight
  \endgroup
  \let\firstmark\botmark}
%%
%% This is used to generate an empty page.
\def\@makeemptypage#1{%
  \if@sidebyside
    \global\setbox#1=\vbox to \@targetpaperheight{\vss
      \hbox to .5\@targetpaperwidth{\hss}}
  \else
     \global\setbox#1=\vbox to .5\@targetpaperheight{\vss
       \hbox to .5\@targetpaperwidth{\hss\box#1\hss}}
  \fi}
%%
%% Eject a page, even when there is only one source page on it.
\def\cleartwouppage{%
  \clearpage
  \if@firstpage
  \else
    \twoupemptypage
  \fi}
%%
\def\twoupemptypage{%
  \clearpage
  {\let\@makepagebox\@makeemptypage
  \@outputpage}
  \addtocounter{page}{-1}}
%%
%% This is LaTeX's standard \enddocument, except that we change \clearpage
%% to \cleartwouppage.
\def\enddocument{\@checkend{document}\cleartwouppage\begingroup  
  \if@filesw \immediate\closeout\@mainaux 
  \def\global\@namedef##1##2{}\def\newlabel{\@testdef r}%
  \def\bibcite{\@testdef b}\@tempswafalse \makeatletter\input \jobname.aux
  \if@tempswa \@warning{Label(s) may have changed.  Rerun to get
  cross-references right}\fi\fi\endgroup\deadcycles\z@\@@end}
%%
\endinput

