\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{tocenter}
         [2002/01/31 v1.00 Centering Page Layout (NCC)]
%
% The package provides two commands
%
%  \ToCenter[hfm]{text width}{text height}
%  \FromMargins[hfm]{left margin}{right margin}{top margin}{bottom margin}
%
% which customize the position and dimensions of document pages. The first
% one calculates margins in such a way to center the specified text area
% together with specified header or footer or margins (the first optional
% parameter) on a sheet of paper. The last one calculates text width and
% height using specified margins and adjusts the text area on specified
% margins.
%
% The optional parameter contains a combination of letters h (header),
% f (footer), and m (marginal notes). It determines fields to be taken
% into account in calculations of document margins and text area dimensions.
%
% If twoside mode is turned on, the left and right margins in \FromMargins
% command are considered for odd pages. For even pages, their values are
% swapped.
%
% If m-letter is specified in the optional parameter, the margins are
% determined depending on twoside and twocolumn switches. In twocolumn mode
% marginal notes are posed on both sides of paper, but in onecolumn mode
% the marginal notes are posed on the outer side of page in twoside mode
% and to the right of text area in oneside mode. The reverse margin mode is
% also supported.
%
% The star-forms of these commands
%
%  \ToCenter*{text width}{text height}
%  \FromMargins*{left margin}{right margin}{top margin}{bottom margin}
%
% are intended for positioning of simple short documents without header,
% footer, marginal notes, cross-references, and table of contents.
% Additionaly, the empty page style is set and the writing to aux-file
% is supressed.
%
\newcommand{\ToCenter}{%
  \@ifstar{\pagestyle{empty}\nofiles\NCC@center[]}{\NCC@center}%
}
\newcommand{\FromMargins}{%
  \@ifstar{\pagestyle{empty}\nofiles\NCC@margin[]}{\NCC@margin}%
}
\newcommand*{\NCC@center}[3][]{\NCC@pos{#1}%
  \setlength\textwidth{#2}%
  \@tempdimb\paperwidth
  \@tempdima\textwidth \NCC@m\@tempdima
  \if@twocolumn
    \NCC@m\@tempdima 
    \advance\@tempdimb -\@tempdima \@tempdima .5\@tempdimb
    \NCC@m\@tempdima
    \oddsidemargin\@tempdima
    \evensidemargin\@tempdima
  \else
    \advance\@tempdimb -\@tempdima \@tempdima .5\@tempdimb
    \if@reversemargin
      \evensidemargin\@tempdima
      \NCC@m\@tempdima
      \oddsidemargin\@tempdima
    \else
      \oddsidemargin\@tempdima
      \NCC@m\@tempdima
      \evensidemargin\@tempdima
    \fi
  \fi
  \setlength\textheight{#3}%
  \@tempdimb\paperheight
  \@tempdima\textheight \NCC@h\@tempdima \NCC@f\@tempdima
  \advance\@tempdimb -\@tempdima \topmargin .5\@tempdimb
  \ifx\NCC@h\@gobble
    \advance\topmargin -\headsep
    \advance\topmargin -\headheight
  \fi
  \advance \oddsidemargin  -1in
  \advance \evensidemargin -1in
  \advance \topmargin      -1in
}
\newcommand*{\NCC@margin}[5][]{\NCC@pos{#1}%
  \setlength\oddsidemargin{#2}%
  \setlength\evensidemargin{#3}%
  \textwidth\paperwidth
  \@tempdima\oddsidemargin \advance\@tempdima\evensidemargin 
  \NCC@m\@tempdima
  \if@twocolumn
    \NCC@m\@tempdima \advance\textwidth -\@tempdima
    \NCC@m\oddsidemargin \NCC@m\evensidemargin
  \else
    \advance\textwidth -\@tempdima
    \if@reversemargin
      \NCC@m\oddsidemargin
    \else
      \NCC@m\evensidemargin
    \fi
  \fi
  \setlength\topmargin{#4}%
  \textheight\paperheight
  \setlength\@tempdima{#5}\advance\@tempdima\topmargin
  \NCC@h\@tempdima \NCC@f\@tempdima
  \advance\textheight -\@tempdima
  \ifx\NCC@h\@gobble
    \advance\topmargin -\headsep
    \advance\topmargin -\headheight
  \fi
  \advance \oddsidemargin  -1in
  \advance \evensidemargin -1in
  \advance \topmargin      -1in
}
\@onlypreamble\ToCenter
\@onlypreamble\FromMargins
\@onlypreamble\NCC@center
\@onlypreamble\NCC@margin
%
% \NCC@pos{PARAM} defines \NCC@h, \NCC@f, and \NCC@m to calculate
% header height, footer height, and marginpar width. The calculation is
% provided as advancing of dimen register used in parameter.
% This command is useful in the cropmark package also.
%
\def\NCC@pos#1{%
  \let\NCC@h\@gobble \let\NCC@f\@gobble \let\NCC@m\@gobble
  \@tfor\@tempa:=#1\do{%
    \if h\@tempa
      \def\NCC@h##1{\advance##1\headsep \advance##1\headheight}%
     \else
      \if f\@tempa
        \def\NCC@f##1{\advance##1\footskip}%
       \else
        \if m\@tempa
          \def\NCC@m##1{\advance##1\marginparwidth \advance##1\marginparsep}%
        \fi
      \fi
    \fi
  }%
}
\endinput
