% UNIT MACROS for LaTeX2e, version 1.02,
% written by Herman Haverkort, 16 february 1995
% See end of file for information on warranty, distribution etc.

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{hhunits}[1995/02/16 Units system]
\RequirePackage{hhqueue}[1995/02/14]

% =============================================================================
% \unit UNITNAME:DIMENSION returns the largest of the specified DIMENSION
% and the current value of the specified unit. Furthermore the value of the
% specified unit is set to the returned value.
% Thus the value of the unit will eventually reach the maximum value which
% is passed to it during the processing of the document. That value is
% saved after processing the document, and reloaded before processing it
% a second time. During the second run the same value will be used throughout
% the entire document, and that value will be large enough to fit all
% dimensions which are specified for the unit.
% =============================================================================

\def\unit#1:#2{%
  \@ifundefined{unit@#1}%
    {#2\h@newunit #1:{#2}{#2}}%
    {\ifdim\@nameuse{unit@#1}<#2%
       #2\h@setunit #1:{#2}%
     \else
       \@nameuse{unit@#1}%
     \fi
     \ifdim\@nameuse{newunit@#1}<#2%
       \global\csname newunit@#1\endcsname #2%
     \fi}}
% The unit macro keeps track of the current unit value and a new
% unit value. The current unit value is initialized by reading the
% file jobname.aux. The initial value is based on a previous run.
% If no extra action were undertaken, the unit value would not drop
% down to a reasonable value anymore if a previous run on the document
% drove it far up. Therefore a new unit value is maintained; it is
% initialized to zero in the beginning of every compiling session.
% The current unit value is used for type-setting, but when the end
% of the document is reached the new unit value is saved.

\def\h@presetunit#1:#2{\h@newunit #1:{#2}{#2}}
% \h@presetunit is meant to be used in style files to initalize unit values.
% It sets both the current and the new unit value. This prevents
% the unit value from dropping below its preset value if it is not used.
% Such dropping could cause problems, because the dropped value would be
% reloaded the next time the document is processed. The reloading of the
% unit values takes place after the reading of the style files which
% contain the presettings, so if the new unit value was not initialized,
% the presetting would lose its effect after compiling the document once.

\def\h@resetunit#1:#2{%
  \@ifundefined{unit@#1}%
    {\h@newunit #1:{#2}{\z@}}
    {\global\csname unit@#1\endcsname #2}}
% Designed to be used in jobname.aux. Do not use this macro directly.

\def\h@newunit#1:#2#3{%
  \expandafter\newdimen\csname unit@#1\endcsname
  \global\csname unit@#1\endcsname #2%
  \expandafter\newdimen\csname newunit@#1\endcsname
  \global\csname newunit@#1\endcsname #3%
  \edef\@@@unt{#1}\h@enqueue{@@@unt}{@units}}

\def\h@setunit#1:#2{%
  \global\csname unit@#1\endcsname #2%
  \global\@unitschangedtrue}

\AtEndDocument{%
  \clearpage
  \if@filesw\h@writeunits\immediate\closeout\@unitsfile\fi}

\def\h@writeunits{%
  {\def\\{\h@writeunit}\h@processq{@units}}%
  \if@unitschanged
    \PackageWarningNoLine{hhunits}{Unit values may have changed.\MessageBreak
                                   Rerun to get them right}\fi}

\def\h@writeunit#1{%
  \ifdim\@nameuse{newunit@#1}<\@nameuse{unit@#1}%
    \global\csname unit@#1\endcsname \@nameuse{newunit@#1}%
    \global\@unitschangedtrue
  \fi
  \immediate\write\@unitsfile{%
    \string\h@resetunit \space #1:{\the\@nameuse{unit@#1}}}}

% **********************
% *   INITIALIZATION   *
% **********************

\h@initqueue{@units}{}
\newif\if@unitschanged
\newwrite\@unitsfile
\@input{\jobname.uni}
\if@filesw
  \immediate\openout\@unitsfile=\jobname.uni
  \immediate\write\@unitsfile{\relax}\fi

\endinput% **********************
% * WARRANTY, DISTRIBUTION ETC. *
% *******************************
%
% This is a TeX style file by Herman Haverkort. This file is distributed
% in the hope that it will be useful, but without any warranty; without
% even the implied warranty of merchantability or fitness for a particular
% purpose. The author does not accept any responsability for any damage,
% loss, injury, psychosis, annoyance, marital problems, murders etc. which
% are caused by this file.
%
% This style file has not been tested systematically, so there are
% probably undiscoverd errors in it. If you find one, if you have
% trouble using this file, if you have a suggestion for an extension or
% another modification, or if you are just a happy user, please report
% it to:
%                       Herman Haverkort
%                       Heyenoordseweg 40
%                      NL-6813 GA  Arnhem
%                      herman@fgbbs.iaf.nl
%
% You are allowed to change this file, but you are not allowed to distribute
% the changed version unless you obey the following:
% -  give the changed file another name than the original one;
% -  record clearly in the changed file:
%    -  the original name of the file;
%    -  its original author;
%    -  that it is a changed version;
% -  do not remove the restrictions on distributing the file.
%
% You are not allowed to take money for the distribution or use of either
% this file or a changed version, except for a nominal charge for copying
% etc.
%
% Have fun!


