%
% setmarg.sty : a package to set margins
%
% checksum = "33854 186 722 6009"
% 
% Package options :
%  show    show computed margin values
%  cm      show margins in centimeters
%  in      show margins in inches
%  pt      show margins in points
%  pc      show margins in picas
%  bp      show margins in big points
%  mm      show margins in millimeters
%  dd      show margins in Didot points
%  cc      show margins in ciceros
%
% Package commands :
%  \setmargins{<key=dim>[,<key=dim>]}
% 
% Possible keys :
%  top    to set the top margin
%  bottom to set the bottom margin
%  left   to set the left margin for odd pages
%         and the right margin for even pages
%  right  to set the right margin for odd pages
%         and the left margin for even pages
%
% All margins are set relative to the page border. TeX default 
% 1in offset is taken into account by the package.
%
% If the bottom margin is not specified, it is set equal to the 
% top margin.
%
% Fr\'{e}d\'{e}ric Boulanger  <Frederic.Boulanger@supelec.fr>
% Sup\'{e}lec - Service Informatique
% Plateau de Moulon
% F-91192 Gif-sur-Yvette cedex
%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{setmarg}[1999/05/15 v1.0 by Frederic Boulanger]
\RequirePackage{keyval}
\RequirePackage{calc}

\DeclareOption{show}{\@showmarginstrue} % show computed margins
%
% Options to chose the display unit for margins
\DeclareOption{cm}{%
  \def\margins@fact{2.54}\def\margins@unit{cm}%
}
\DeclareOption{in}{%
  \def\margins@fact{1}\def\margins@unit{in}%
}
\DeclareOption{pt}{%
  \def\margins@fact{72.27}\def\margins@unit{pt}%
}
\DeclareOption{pc}{%
  \def\margins@fact{6.0225}\def\margins@unit{pc}%
}
\DeclareOption{bp}{%
  \def\margins@fact{72}\def\margins@unit{bp}%
}
\DeclareOption{mm}{%
  \def\margins@fact{25.4}\def\margins@unit{mm}%
}
\DeclareOption{dd}{%
  \def\margins@fact{67.541510501}\def\margins@unit{dd}%
}
\DeclareOption{cc}{%
  \def\margins@fact{5.6284592084}\def\margins@unit{cc}%
}

% Display margins in inches by default
\ExecuteOptions{in}

% The only public command of this package:
%  set the top, left, right bottom margins for the document
%  using key/value syntax.
\newcommand{\setmargins}[1]{%
  \setkeys{margins}{#1}\margins@compute
}

%%%%
% Private part
%%%%
\newif\if@topmargin % has the top margin been specified ?
\newif\if@botmargin % has the bottom margin been specified ?
\newif\if@showmargins % should the computed margins be displayed ?
\newlength{\bot@margin} % bottom margin does not really exist in LaTeX.
\newcount\margins@intpart  % integer part of the margin to be displayed
\newcount\margins@fracpart % fractional part of the margin to be displayed

% Declare the possible keys in the argument of \setmargins
\define@key{margins}{top}{% top margin, compensate for the 1in TeX offset
  \@topmargintrue\setlength{\topmargin}{#1 - 1in}%
}
\define@key{margins}{bottom}{% bottom margin (no offset here)
  \@botmargintrue\setlength{\bot@margin}{#1}%
}

\define@key{margins}{left}{% left margin (left on an odd page)
  \setlength{\oddsidemargin}{#1 - 1in}%
}
\define@key{margins}{right}{% right margin (right on an odd page)
  \setlength{\evensidemargin}{#1 - 1in}%
}

% Convert a dimension to the chosen unit (use \margin@fact)
%  #1 is the text to be displayed before the dimension
%  #2 is the dimension
%  #3 is the text to be displayed after the dimension
\newcommand{\margins@format}[3]{%
  \setlength{\@tempdima}{#2}%
  % convert the dimension to millions of inches
  % the 1000000 factor allows to work with integers (counters)
  \setlength{\@tempdima}{0.21113586637\@tempdima}% inches*1000000
  % use \margins@fact to convert the dimension to the chosen unit
  \setlength{\@tempdima}{\margins@fact\@tempdima}%
  % copy it to counters (converts to integer)
  \margins@fracpart=\@tempdima
  \margins@intpart=\margins@fracpart
  % scale down by the 1000000 factor to get the effective integer part
  \divide\margins@intpart by 1000000
  % get the remainder of the scale down operation, this is the
  % fractional part.
  \@tempcnta=\margins@intpart
  \multiply\@tempcnta by 1000000
  \advance\margins@fracpart by -\@tempcnta
  % display the dimension.
  \message{%
    #1\the\margins@intpart.\the\margins@fracpart\margins@unit#3%
  }
}

%
% Compute the TeX margins according to paper size and user chosen
% margins
%
\newcommand{\margins@compute}{%
  % Text width is paper width minus the side margins and the two 1in
  % TeX offsets. I count two TeX offsets since there is one for odd
  % pages and one % for even pages
  \setlength{\textwidth}{%
    \paperwidth-2in-\oddsidemargin-\evensidemargin
  }%
  % Text height is paper heigh minus the top 1in offset and everything 
  % LaTeX puts on top and bottom of a page (top margin, header and 
  % footer).
  \setlength{\textheight}{%
    \paperheight-1in-\topmargin-\headheight-\headsep-\footskip
  }%
  \if@botmargin
  \else % no bottom margin specified => use top margin
    \setlength{\bot@margin}{\topmargin}%
    \if@topmargin % if top margin is user specified, take the 1in 
                  % offset into account.
      \addtolength{\bot@margin}{1in}%
    \fi
  \fi
  \addtolength{\textheight}{-\bot@margin}%
  %
  % We are done with the margins.
  % If we must display the computed margins, do so using 
  % \margins@format.
  % We add 1in to these margins since the user is interested in
  % the effective blank space from the border of the sheet.
  \if@showmargins
    \message{%
      ^^J** Margins computed by \string\setmargins\space **^^J%
    }
    \margins@format{%
      \space\space\space Top margin:\space\space\space\space
    }{\topmargin+1in}{^^J}%
    \margins@format{\space\space\space Bottom margin:\space}%
                   {\bot@margin}{^^J}%
    \margins@format{%
      \space\space\space Left margin:\space\space\space
    }{\oddsidemargin+1in}{^^J}%
    \margins@format{%
      \space\space\space Right margin:\space\space
    }{\evensidemargin+1in}{^^J^^J}%
  \fi
}

\ProcessOptions\relax
\endinput
%%%%%%%%%%%%%%%%%%%%%%%%%%%
