%%
%% This is file `papermas.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% papermas.dtx  (with options: `package')
%% 
%% This is a generated file.
%% 
%% IMPORTANT NOTICE:
%% The package takes options.
%% 
%% The usual disclaimers apply:
%% If it doesn't work right that's your problem.
%% (Nevertheless, send an e-mail to the maintainer
%%  when you find an error in this package.)
%% 
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either
%% version 1.3c of this license or (at your option) any later
%% version. This version of this license is in
%%    http://www.latex-project.org/lppl/lppl-1-3c.txt
%% and the latest version of this license is in
%%    http://www.latex-project.org/lppl.txt
%% and version 1.3c or later is part of all distributions of
%% LaTeX version 2005/12/01 or later.
%% 
%% This work has the LPPL maintenance status "maintained".
%% 
%% The Current Maintainer of this work is H.-Martin Muench
%% (Martin dot Muench at Uni-Bonn dot de).
%% 
%% This work consists of the main source file papermas.dtx
%% and the derived files
%%    papermas.sty, papermas.pdf, papermas.ins, papermas.drv,
%%    papermas-example.tex.
%% 
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{papermas}[2011/02/01 v1.0e
            Computes paper mass of a printout (HMM)]%

%% Allows to compute the number of sheets of paper
%% needed to print a document as well as the
%% mass of that printed version of the document,
%% useful e. g. when sending it by mail to determine the postage.
%% Warning/Disclaimer: Mass of (printer's) ink has to be added
%% (and that of envelope, address sticker, stamps,...)!
%% So, this is only an estimation without guarantee -
%% do not sue me, if you have got to pay excess postage!
%% Further this package allows to compute
%%  "base to the power of exponent" inside TeX.

\RequirePackage{kvoptions}[2010/02/22]% v3.7
\RequirePackage{pagesLTS}[2011/02/01]% v1.1m
%% papermas may work with earlier versions of those packages,
%% but this was not tested. Please consider updating your packages
%% to the most recent version (if they are not already the most
%% recent version).

\SetupKeyvalOptions{family = papermas,prefix = papermas@}
\DeclareStringOption[4]{format}[4]%        paper foormat, ISO A...,
                                  %         default: (ISO A) 4
\DeclareStringOption[80]{masss}[80]%       specific mass of the paper,
                                   %        default: 80 (g/(m^2))
\DeclareStringOption[2]{pagespersheet}[2]% number of pages per sheet,
                                         %  for duplex printing this is 2.
\DeclareStringOption[.]{decimalsep}[.]%    decimal separator,
             % e. g. "." or ",": decimalsep={,} - brackets are needed!!!
             % decimalsep={,\,} does not work for screen, aux, log output!

\ProcessKeyvalOptions*

\gdef\unit#1{\mathord{\thinspace\mathrm{#1}}}%

\@ifundefined{papermasstotal}{\gdef\papermasstotal{\textbf{??}}}{}
\@ifundefined{papermasstotal}{\gdef\papermasstotal{\textbf{??}}}{}
\@ifundefined{papermasformat}{\gdef\papermasformat{\textbf{??}}}{}
\@ifundefined{papermasmasss}{\gdef\papermasmasss{\textbf{??}}}{}
\@ifundefined{papermaspagespersheet}{\gdef\papermaspagespersheet{\textbf{??}}}{}
\@ifundefined{papermassheets}{\gdef\papermassheets{\textbf{??}}}{}

\newcounter{papermas@rerun}
\newcounter{papermas@base}
\newcounter{papermas@exp}
\newcounter{papermas@result}
\newcounter{papermas@ini}
\setcounter{papermas@ini}{1}

\newcommand\papermas@powerof[2]{%
  \setcounter{papermas@base}{#1}
  \setcounter{papermas@exp}{#2}
  \ifnum \value{papermas@ini}=1
    \setcounter{papermas@result}{\value{papermas@base}}
    \setcounter{papermas@ini}{0}
    \ifnum \value{papermas@exp}=0%
      \setcounter{papermas@result}{1}
    \else
      \addtocounter{papermas@exp}{-1}
    \fi
  \fi
  \ifnum \value{papermas@exp}=0%
    \setcounter{papermas@ini}{1}
  \else
    \multiply \value{papermas@result} \value{papermas@base}
    \addtocounter{papermas@exp}{-1}
    \papermas@powerof{#1}{\value{papermas@exp}}
  \fi%
  }

\newcommand\papermas@totmass{%
  \newcounter{papermasA}% paper mass for ISO A...
  \setcounter{papermasA}{\papermas@format}% e. g. 4
  \ifnum \value{papermasA}<0%
    \PackageError{papermas}{Option format has no valid value}%
     {The format option of the papermas package\MessageBreak%
      only takes whole, non-negative numbers (0, 1, 2, 3,...),\MessageBreak%
      because this should be the paper format\MessageBreak%
      ISO A 0, 1, 2, 3,...\MessageBreak%
      Found instead: \papermas@format \MessageBreak%
     }
  \else%
    \newcounter{papermasmasss}% always 0
    \setcounter{papermasmasss}{\papermas@masss}% default: 80
    \multiply \value{papermasmasss} 100 % default: 8000
    \ifnum \value{papermasmasss}<1%
      \PackageError{pagesLTS}{Option masss has no valid value}%
       {The masss option of the papermas package\MessageBreak%
        only takes positive numbers,\MessageBreak%
        because this should be the mass per square meter\MessageBreak%
        of a single sheet of your paper.\MessageBreak%
        Found instead: \papermas@masss \MessageBreak%
       }
    \else
      \newcounter{papermasPPS}% is 0
      \setcounter{papermasPPS}{\papermas@pagespersheet}% default 2
      \ifnum \value{papermasPPS} < 1%
        \PackageError{papermas}{%
          The number of pages per sheet must be positive.}{%
          You cannot print less than one TeX page per sheet of paper.\MessageBreak%
          The value found was \papermas@pagespersheet .\MessageBreak%
          }
      \else
        \newcounter{papermas@sheets}
        \setcounter{papermas@sheets}{\arabic{pagesLTS.pagenr}}%
        \divide \value{papermas@sheets} by \value{papermasPPS}%
        \newcounter{papermas@tmpn}
        \setcounter{papermas@tmpn}{\arabic{papermas@sheets}}%
        \multiply \value{papermas@tmpn} \value{papermasPPS}%
        \ifnum \value{papermas@tmpn}=\value{pagesLTS.pagenr}
          \relax
        \else
          \addtocounter{papermas@sheets}{1}%
        \fi
        \multiply \value{papermasmasss} \value{papermas@sheets}
  % default:                  8000       (no default for this)
        \papermas@powerof{2}{\value{papermasA}}
        \divide \value{papermasmasss} by \value{papermas@result}
  % default:               16000      /   2^(\value{papermasA})
        % for the example 297 is used
        \newcounter{papermas@tmpm}
        \setcounter{papermas@tmpm}{\arabic{papermasmasss}}%   m:297 n:    o:  p:  q:
        \setcounter{papermas@tmpn}{\arabic{papermasmasss}}%   m:291 n:291 o:  p:  q:
        \divide \value{papermas@tmpn} by 100%                 m:297 n:2   o:  p:  q:
        \newcounter{papermas@tmpo}
        \setcounter{papermas@tmpo}{\arabic{papermas@tmpn}}%   m:291 n:2   o:2 p:  q:
        \multiply \value{papermas@tmpn} 10%                   m:297 n:20  o:2 p:  q:
        \divide \value{papermas@tmpm} by 10%                  m:29  n:20  o:2 p:  q:
        \newcounter{papermas@tmpp}
        \setcounter{papermas@tmpp}{\arabic{papermas@tmpm}}
        \addtocounter{papermas@tmpp}{-\arabic{papermas@tmpn}}%m:29  n:20  o:2 p:9 q:
        %        29              - 20 = 9
        \multiply \value{papermas@tmpm} 10%                   m:290 n:20  o:2 p:9 q:
        \newcounter{papermas@tmpq}
        \setcounter{papermas@tmpq}{\arabic{papermasmasss}}
        \addtocounter{papermas@tmpq}{-\arabic{papermas@tmpm}}%m:290 n:20  o:2 p:9 q:7
        %       297              - 290 = 7
        \ifnum\value{papermas@tmpq}>4
          \addtocounter{papermas@tmpp}{1}%                    m:290 n:20 o:2 p:10 q:7
          \ifnum\value{papermas@tmpp}>9%                      m:290 n:20 o:2 p:10 q:7
            \addtocounter{papermas@tmpo}{1}%                  m:290 n:20 o:3 p:10 q:7
            \setcounter{papermas@tmpp}{0}%                    m:290 n:20 o:3 p:0  q:7
          \fi
        \fi
        \edef\papermastmpr{\arabic{papermas@tmpo}\papermas@decimalsep\arabic{papermas@tmpp}}%
        \edef\papermastmpformat{\papermas@format}%
        \edef\papermastmpmasss{\papermas@masss}%
        \edef\papermastmppagespersheet{\papermas@pagespersheet}%
        \edef\papermastmpt{\arabic{papermas@sheets}}%
        \pagesLTS@ifcounter{papermassttl}
        \ifnum\value{papermassttl}=\value{papermasmasss}
          \relax
        \else
          \PackageWarningNoLine{papermas}{%
            Number of pages may have changed.\MessageBreak%
            Rerun to get it right.\MessageBreak%
            }%
        \fi
        \setcounter{papermassttl}{\arabic{papermasmasss}}
        \edef\papermastmps{\arabic{papermasmasss}}%
        \if@filesw%
          \immediate\write\@auxout{\string
            \pagesLTS@ifcounter{papermassttl}}%
          \immediate\write\@auxout{\string
            \setcounter{papermassttl}{\papermastmps}}%
          \immediate\write\@auxout{\string
            \gdef\string\papermasstotal{\papermastmpr}}%
          \immediate\write\@auxout{\string
            \gdef\string\papermasformat{\papermastmpformat}}%
          \immediate\write\@auxout{\string
            \gdef\string\papermasmasss{\papermastmpmasss}}%
          \immediate\write\@auxout{\string
            \gdef\string\papermaspagespersheet{\papermastmppagespersheet}}%
          \immediate\write\@auxout{\string
            \gdef\string\papermassheets{\papermastmpt}}%
        \fi%
      \fi%
    \fi%
  \fi%
  }

\AtBeginDocument{%
  \def\papermas@undefined{\textbf{??}}
  \setcounter{papermas@rerun}{0}
  \ifx\papermasstotal\papermas@undefined        \addtocounter{papermas@rerun}{000001} \fi
  \ifx\papermasstotal\papermas@undefined        \addtocounter{papermas@rerun}{000010} \fi
  \ifx\papermasformat\papermas@undefined        \addtocounter{papermas@rerun}{000100} \fi
  \ifx\papermasmasss\papermas@undefined         \addtocounter{papermas@rerun}{001000} \fi
  \ifx\papermaspagespersheet\papermas@undefined \addtocounter{papermas@rerun}{010000} \fi
  \ifx\papermassheets\papermas@undefined        \addtocounter{papermas@rerun}{100000} \fi
  }

\AfterLastShipout{%
  \papermas@totmass%
  }%

\AtVeryEndDocument{%
  \ifnum\value{papermas@rerun}>0
    \PackageWarningNoLine{papermas}{!\MessageBreak%
      Variable(s) still undefined.\MessageBreak%
      (Error code \arabic{papermas@rerun}.)\MessageBreak%
      Rerun to get the variable(s) right.\MessageBreak%
    }%
  \else
    \message{papermas: ***************************************************}
    \message{papermas: * This document consists of \arabic{pagesLTS.pagenr} pages. *}
    \message{papermas: * When printing \papermaspagespersheet\space pages on one sheet of paper, *}
    \message{papermas: * \papermassheets\space sheets will be needed. *}
    \message{papermas: * For ISO A \papermasformat\space paper of \papermasmasss\space g/m^2 specific mass, *}
    \message{papermas: * the printout will have a mass of about \papermasstotal\space g. *}
    \message{papermas: ***************************************************}
    \PackageInfo{papermas}{***************************************************\MessageBreak%
      * This document consists of \arabic{pagesLTS.pagenr} pages. *\MessageBreak%
      * When printing \papermaspagespersheet\space pages on one sheet of paper, *\MessageBreak%
      * \papermassheets\space sheets will be needed. *\MessageBreak%
      * For ISO A \papermasformat\space paper of \papermasmasss\space g/m^2 specific mass, *\MessageBreak%
      * the printout will have a mass of about \papermasstotal\space g. *\MessageBreak%
      ***************************************************\MessageBreak%
    }%
  \fi%
  }

\endinput
%%
%% End of file `papermas.sty'.
