%%----------------------------------------------------------------------
%% Vmargin.sty
%
% LaTeX style file providing macros to aid margin setting. Also
% introduces paper sizes. Supersedes Vpage.sty.
% This file can be made part of a format by typing \input Vmargin.sty
% before dumping the format.
%
% Documentation & history after (last) \endinput.
%
% Works with both LaTeX2.09 and LaTeX2e.
% Supported = yes.
%
%
% (C) Copyright 1993, 1994 by:
%
% Volker Kuhlmann
% University of Canterbury
% EEE Dept
% Christchurch, New Zealand
% E-Mail: VOLKER@ELEC.CANTERBURY.AC.NZ
%
% Permission is granted to use this file free of charge, and to
% distribute it provided no charge is made for distribution.
% This file must not be altered without changing its name.
%
%%----------------------------------------------------------------------

\def\filename{Vmargin}
\def\fileversion{V2.1}
\def\filedate{20 Sep 1994}

\@ifundefined{Vmargin}{}{\endinput}

\edef\Vmargin{Style `\filename', \fileversion, \filedate}

\expandafter\everyjob\expandafter{\the\everyjob\typeout{\Vmargin}}
\typeout{\Vmargin}



% new lengths:  \PaperWidth, \PaperHeight
% new if:       \ifLandscape
%
\newdimen\PaperWidth
\newdimen\PaperHeight
%
\newif\ifLandscape


% \setpapersize
%
\def\setpapersize{\@ifnextchar[{\@@setps}{\@@setps[portrait]}}
\def\@@setps[#1]#2{%
  \@ifundefined{po@#1}{\@name@err{#1}}{\@nameuse{po@#1}}%
  \@ifundefined{paper@#2}{\@name@err{#2}}{}%
  \csname paper@#2\endcsname}
        % \usename{paper@#2} inside arg to \@ifundefined does not work
        % with papersize "custom".
\def\po@portrait{\Landscapefalse}
\def\po@landscape{\Landscapetrue}
\def\@po@{\ifLandscape\dimen0\PaperWidth
  \PaperWidth\PaperHeight\PaperHeight\dimen0\fi\relax
  \@ifundefined{paperwidth}{}{\paperwidth\PaperWidth}%
  \@ifundefined{paperheight}{}{\paperheight\PaperHeight}}
\def\@name@err#1{%
  \typeout{*****> \string\setpapersize: illegal parameter: #1}}


% pre-defined paper/envelope sizes
%
% A0, A1, A2, ..., A9, B0, B1, ..., B9, C0, C1, ..., C9
% USletter, USlegal, USexecutive
% custom
%
\def\@defmetricpaper#1#2#3{%
  \begingroup
  \count0=0
  \def\w{\dimen1 }\def\h{\dimen2 }\def\s{\dimen3 }%
  \w#2\h#3
  \def\l{11}
  \loop
    \begingroup
    \def\t{\the\count0}
    \catcode`\t=11      % letter
    \expandafter\xdef\csname paper@#1\the\count0\endcsname{%
      \PaperWidth\the\w\PaperHeight\the\h\noexpand\@po@}
    \endgroup
    \s\w\w.5\h\h\s
  \ifnum\the\count0<9
    \advance\count0 by 1
  \repeat
  \endgroup
}
%
\@defmetricpaper{A}{840.9mm}{1189.2mm}
\@defmetricpaper{B}{1000mm}{1414mm}
\@defmetricpaper{C}{917mm}{1297mm}
%
\let\@defmetricpaper=\relax     % delete definition to save memory
%
\def\paper@USletter{\PaperWidth 8.5in \PaperHeight 11in \@po@}
\def\paper@USlegal{\PaperWidth 8.5in \PaperHeight 14in \@po@}
\def\paper@USexecutive{\PaperWidth 7.25in\PaperHeight 10.5in \@po@}
%
\def\paper@custom#1#2{\PaperWidth#1\PaperHeight#2\@po@}


% margin@offset
%
% Compensates for the +1in/+1in top/left corner
% by either reducing the margins or \hoffset, \voffset by 1in.
% This macro is only defined here if it is not already defined!
% (see documentation at the end)
%
\newif\if@shiftmargins
\@shiftmarginsfalse     % this MUST be default (pageframe.sty)
%
\@ifundefined{margin@offset}{
\def\margin@offset{
  \if@shiftmargins
        \oddsidemargin -1in\evensidemargin -1in\topmargin -1in
        \hoffset 0in\voffset 0in\relax
  \else
        \oddsidemargin 0in\evensidemargin 0in\topmargin 0in
        \hoffset -1in\voffset -1in\relax
  \fi
}}{}
%
\def\shiftmargins{\@shiftmarginstrue}


% Setting margins
%
% \setmargins{leftmargin}{topmargin}{textwidth}{textheight}% 
%    {headheight}{headsep}{footheight}{footskip}
%
\newcommand\setmargins[8]{%
        \margin@offset
        \advance\oddsidemargin  #1
        \advance\evensidemargin \PaperWidth     % = paperwidth - left
        \advance\evensidemargin -#1             %       - width
        \advance\evensidemargin -#3
        \advance\topmargin      #2
        \textwidth      #3
        \textheight     #4
        \headheight     #5
        \headsep        #6
        \@ifundefined{footheight}{}{\footheight=#7}%
        \footskip       #8
        \chk@dimen{#1}{#2}{#3}{#4}%
}
%
% \setmargins{leftmargin}{topmargin}{rightmargin}{bottommargin}% 
%    {headheight}{headsep}{footheight}{footskip}
%
\newcommand\setmarginsrb[8]{%
        \margin@offset
        \textwidth              \PaperWidth     % = paperwidth
        \advance\textwidth      -#1             %  - left - right
        \advance\textwidth      -#3
        \textheight             \PaperHeight    % = paperheight - top
        \advance\textheight     -#2             %  - headheight
        \advance\textheight     -#5             %  - headsep
        \advance\textheight     -#6             %  - footskip - bottom
        \advance\textheight     -#8
        \advance\textheight     -#4
        \advance\oddsidemargin  #1
        \advance\evensidemargin \PaperWidth     % = paperwidth
        \advance\evensidemargin -#1             %  - left - width
        \advance\evensidemargin -\textwidth
        \advance\topmargin      #2
        \headheight     #5
        \headsep        #6
        \@ifundefined{footheight}{}{\footheight=#7}%
        \footskip       #8
        \chk@dimen{#1}{#2}{#3}{#4}%
}
%
% \setmargnohf{leftmargin}{topmargin}{textwidth}{textheight}
% headheight, headsep, footheight, footskip set to 0pt
\newcommand\setmargnohf[4]{%
        \setmargins{#1}{#2}{#3}{#4}{0pt}{0mm}{0pt}{0mm}%
        \pagestyle{empty}}
%
% \setmargnohfrb{leftmargin}{topmargin}{rightmargin}{bottommargin}
% headheight, headsep, footheight, footskip set to 0pt
\newcommand\setmargnohfrb[4]{%
        \setmarginsrb{#1}{#2}{#3}{#4}{0pt}{0mm}{0pt}{0mm}%
        \pagestyle{empty}}
%
% \setmarg{leftmargin}{topmargin}{textwidth}{textheight}
% headheight, headsep, footheight, footskip unchanged
\newcommand\setmarg[4]{%
        \setmargins{#1}{#2}{#3}{#4}%
        \headheight\headsep\footheight\footskip}
%
% \setmargnohfrb{leftmargin}{topmargin}{rightmargin}{bottommargin}
% headheight, headsep, footheight, footskip unchanged
\newcommand\setmargrb[4]{%
        \setmarginsrb{#1}{#2}{#3}{#4}%
        \headheight\headsep\footheight\footskip}
%
% h-warning if [leftmarg + textwidth > paperwidth] resp.
%           if [letmarg + rightmarg > paperwidth].
% v-warning if [topmarg + textheight > paperheight] resp.
%           if [topmarg + bottommarg > paperheight].
\def\chk@dimen#1#2#3#4{%
        \dimen0=        #1
        \advance\dimen0 by#3
        \advance\dimen0 -\PaperWidth
        \dimen1=        #2
        \advance\dimen1 by#4
        \advance\dimen1 \headheight
        \advance\dimen1 \headsep
        \advance\dimen1 \footskip
        \advance\dimen1 -\PaperHeight
        \ifnum\dimen0>\z@\typeout{Vmargin Warning: Horizontal dimensions
          exceed paper width by \the\dimen0}\fi\relax
        \ifnum\dimen1>\z@\typeout{Vmargin Warning: Vertical dimensions
          exceed paper height by \the\dimen1}\fi\relax
}


%
% DEFAULTS:
%
\setpapersize{A4}
\setmarginsrb{35mm}{20mm}{25mm}{15mm}{12pt}{11mm}{0pt}{11mm}


\endinput

%%----------------------------------------------------------------------

Page Size and Margins
=====================

These macros make it easy to set page margins for a chosen paper size.
Actual dimensions of the most common paper sizes are stored and need
not be remembered.

Two sided printing is supported, meaning that if on odd pages the left
margin is, say, 30mm and the right margin is 20mm, it will be vice
versa on even pages. This gives equal margins on the outer and the
inner edge of the paper, as expected e.g. for a book.

The size of the paper can be set with 

        \setpapersize{}

 can be A0, A1, ..., A9, B0, B1, ..., B9, C0, ..., C9,
USletter, USlegal, and USexecutive. The metric paper sizes are not
stored but calculated.

Landscape format is selected by using the optional argument

        \setpapersize[landscape]{}

which swaps the width and height dimensions of the paper.
\setpapersize[portrait]{} is allowed but is the default.

If you have a size which is not pre-defined use

        \setpapersize{custom}{}{}

For  and  insert the respective dimensions of your
paper.

\setpapersize stores the actual dimensions of the paper in the length
variables

        \PaperWidth
        \PaperHeight

which can be used further, if desired.

        \ifLandscape

yields true if a landscape format is selected. Do not write
\PaperWidth, \PaperHeight, or call \Landscapetrue or \Landscapefalse,
it will not work!!

The margins can be set with

        \setmargins{leftmargin}{topmargin}{textwidth}{textheight}%
                   {headheight}{headsep}{footheight}{footskip}

or with

        \setmarginsrb{leftmargin}{topmargin}{rightmargin}{bottommargin}%
                     {headheight}{headsep}{footheight}{footskip}

In the latter case \textwidth and \textheight are calculated using the
width and height of the selected paper. The first four parameters of
the above two commands are used to set \oddsidemargin, \evensidemargin,
\textwidth, \topmargin, and \textheight.

        \setmargnohf, \setmargnohfrb

Provide a page with no header and footer. They work the same as
\setmargins, \setmarginsrb except that they only need the first 4
parameter. The last 4 parameter are set to 0pt. These 2 commands set
the pagestyle to empty (\pagestyle{empty}) as there is no space for
headers or footers.

        \setmarg, \setmargrb

are the same as \setmargnohf, \setmargnohfrb apart from that the last 4
parameter to \setmargins, \setmarginsrb are unchanged.

Example:

        A4 paper, left margin 30mm, top, right, and bottom margin 20mm
        each, no headers or footers:

        \setpapersize{A4}
        \setmarginsrb{30mm}{20mm}{20mm}{20mm}{0pt}{0mm}{0pt}{0mm}
        \pagestyle{empty}

The same settings would result by:

        \setpapersize{A4}
        \setmargnohfrb{30mm}{20mm}{20mm}{20mm}

For the default settings please see the part after "DEFAULTS:" (last
part before \endinput). 

The default top and left margins of TeX are +1in. \setmargXXX call

        \margin@offset

which initialises \hoffset, \voffset to -1in and \oddsidemargin,
\evensidemargin, \topmargin to 0in. \setmargXXX then add the given
dimensions to \topmargin, \oddsidemargin, \evensidemargin. In some
cases it might be desired to use \XXXmargin instead of \Xoffset for
compensation. This can be achieved by telling \margin@offset to
initialise \Xoffset to 0in and \XXXmargin to -1in. This is done by
%
        \shiftmargins.
%
If \margin@offset is already defined at the time Vmargin is loaded it
is NOT redefined! Therefore if \margin@offset is defined before Vmargin
is loaded the above mentioned compensation can be replaced by a
different mechanism.  In any way \setmargXXX call \margin@offset and
then expect that \XXXmargin are set to useful values. \Xoffset are not
touched by \setmargXXX.  \margin@offset should be defined in a separate
file which is included BEFORE Vmargin, i.e. appears in the list of
document-substyles of the \documentstyle command before Vmargin.
%
Any better way of doing this? (grumble)

Example:  \documentstyle[...,margins,Vmargin,...]{...} 
          if \margin@offset is defined in a file called margins.sty.

LaTeX2e:  \documentclass[...]{...}
          \usepackage{...,margins,Vmargin,...}


LaTeX2e
-------

This style (or package) works with LaTeX2e. To keep it working under
LaTeX2.09 none of the special LaTeX2e package interface features have
been used. There will probably be a new version (under a new name) that
is specialised for LaTeX2e.

LaTeX2e (unless in compatibility mode) does not know \footheight any
more. This dimension is not set if it is not defined, but is set
otherwise (i.e. it is set in LaTeX2e compatibility mode).

LaTeX2e now has the dimensions \paperwidth, \paperheight which hold the
size of the paper. \PaperWidth, \PaperHeight are copied into
\paperwidth, \paperheight if the latter exist. This makes Vmargin work
correctly with anything that expects \paperwidth, \paperheight to be
set properly. The names \PaperWidth, \PaperHeight had been chosen in
the first place to avoid clashes with style files that also use these
names (namely pageframe.sty).


Inclusion in TeX formats
------------------------

This file may be loaded in initex before dumping the format, by typing
        \makeatletter
        \input Vmargin.sty
        \makeatother
Note: this produces a non-standard format.


Hints for using pageframe.sty
-----------------------------

Vmargin.sty and pageframe.sty can be used together if the following
points are considered:

Vmargin uses \hoffset and \voffset and writes negative values into it,
pageframe expects them to be zero to give a 1in space on the left and
the top where it prints additional information. Initially, set both to
0mm (in the pre-amble of your text) and adjust them later on.

pageframe needs to know the trimmed height of the paper (= the height
of the "page frame"). Unless the trimmed(!) size of the paper is
equivalent to one of the standard paper sizes (unlikely...) the size
should be specified with

        \setpapersize{custom}{}{}

The correct height of the trimmed page can then be given to pageframe
using

        \paperheight{\PaperHeight}

and the margins of the final product (inside the page frame) can be
specified using \setmargXXX.

Warning: if \setmargXXXrb is used the 3rd parameter (right margin) is
ignored. Instead, the dimension of the right margin has to be assigned
to \evensidemargin. This is because pageframe.sty re-defines the meaning
of \evensidemargin to be the right margin of your text, on all pages.

Remember: all these assignments and macro calls have to be in the
pre-amble of the document.


To do:
------

* The way how the metric paper sizes are stored takes up a lot of
  space. A metric size could be computed by \setpapersize.

* Make use of new LaTeX2e features.
  If I do I will probably give the package a new name.


SUMMARY:
========

new lengths:
  \PaperWidth
  \PaperHeight

new ifs:
  \ifLandscape

new macros:
  \setpapersize[]{}, 
  \setpapersize[]{custom}{}{}
         (optional) = landscape or portrait (default)
         = A4, B5, ...
        ,  = actual dimensions of the paper
  \setmargins{leftmargin}{topmargin}{textwidth}{textheight}%
             {headheight}{headsep}{footheight}{footskip}
  \setmarginsrb{leftmargin}{topmargin}{rightmargin}{bottommargin}%
               {headheight}{headsep}{footheight}{footskip}
  \setmargnohf{leftmargin}{topmargin}{textwidth}{textheight}
  \setmargnohfrb{leftmargin}{topmargin}{rightmargin}{bottommargin}
  \setmarg{leftmargin}{topmargin}{textwidth}{textheight}
  \setmargrb{leftmargin}{topmargin}{rightmargin}{bottommargin}
%
  \margin@offset
  \shiftmargins


If you have any comments (positive or negative) please let me know!



HISTORY:
========

        .
        .               Created out of Vpage.sty.
        .
V1.7      21 May 1994   Changed file header.
V1.72     21 May 1994   Fixed bug in \setpapersize{custom}
V1.8      28 May 94     Commented \chk@dimen; reduced load on TeX's
                        parameter stack (changed \chk@dimen).
                        Put a conditional around references 
                        to \footheight.
V1.9      22 Jun 94     Corrected spelling in comment.
V2.0      28 Jun 94     Added support for LaTeX2e \paperwidth,
                        \paperheight.
V2.1      20 Sep 94     \@defmetricpaper now defines \w, \h, \s locally.
                        Thanks to branderhorst@fgg.eur.nl!
%
%% EOF Vmargin.sty
%%----------------------------------------------------------------------
