%%
%% 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.
%% 
%% Project: papermas
%% Version: 2011/08/22 v1.0h
%% 
%% Copyright (C) 2010, 2011 by
%%     H.-Martin M"unch <Martin dot Muench at Uni-Bonn dot de>
%% 
%% The usual disclaimer applys:
%% 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.
%% 
%% 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}[2009/09/24]
\ProvidesPackage{papermas}[2011/08/22 v1.0h
            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!

\RequirePackage{kvoptions}[2010/12/23]% v3.10
\RequirePackage{pageslts}[2011/08/08]% v1.2a
\RequirePackage{intcalc}[2007/09/27]%  v1.1; for intcalcPow
%% papermas may work with earlier versions of LaTeX and those
%% packages, but this was not tested. Please consider updating
%% your LaTeX and 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{??}}}{}

\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{papermas}{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)
        \divide \value{papermasmasss} by \intcalcPow{2}{\value{papermasA}}
  % 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}}%
        \xdef\papermas@mbs{\arabic{papermas@tmpo}}%
        \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
          \AtEndAfterFileList{%
            \PackageWarningNoLine{papermas}{%
              Number of pages may have changed.\MessageBreak%
              Rerun to get it right%
             }%
            }%
        \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{%
  \gdef\papermas@undefined{\textbf{??}}
  \gdef\papermas@rerun{0}
  \ifx\papermasstotal\papermas@undefined        \gdef\papermas@rerun{1} \fi
  \ifx\papermasformat\papermas@undefined        \gdef\papermas@rerun{1} \fi
  \ifx\papermasmasss\papermas@undefined         \gdef\papermas@rerun{1} \fi
  \ifx\papermaspagespersheet\papermas@undefined \gdef\papermas@rerun{1} \fi
  \ifx\papermassheets\papermas@undefined        \gdef\papermas@rerun{1} \fi
  \ifx\papermas@rerun\pagesLTS@one
    \AtEndAfterFileList{
      \PackageWarningNoLine{papermas}{%
        Variable(s) still undefined!\MessageBreak%
        Rerun to get the variable(s) right%
       }
     }
  \fi
  }

\AfterLastShipout{%
  \papermas@totmass%
  }%

\newcommand{\papermas@log}[1]{%
  \ifnum#1>9%
    \addtocounter{pagesLTS.pnc.0}{1}%
    \papermas@log{\intcalcDiv{#1}{10}}%
  \fi%
  }

\newcommand{\papermas@spaces}[2]{%
  \edef\papermas@remember{\arabic{pagesLTS.pnc.0}}%
  \setcounter{pagesLTS.pnc.0}{1}%
  \papermas@log{#1}%
  \addtocounter{pagesLTS.pnc.0}{-#2}%
  \multiply \value{pagesLTS.pnc.0} -1%
  \papermas@space{\arabic{pagesLTS.pnc.0}}%
  \message{*^^J}%
  \setcounter{pagesLTS.pnc.0}{\papermas@remember}%
  }

\newcommand{\papermas@space}[1]{%
  \ifnum \value{pagesLTS.pnc.0}>0%
    \message{}%
  \fi%
  \setcounter{pagesLTS.pnc.0}{#1}%
  \addtocounter{pagesLTS.pnc.0}{-1}%
  \ifnum \value{pagesLTS.pnc.0}>0%
    \papermas@space{\arabic{pagesLTS.pnc.0}}%
  \fi%
  }

\AtEndAfterFileList{%
  \ifx\papermas@rerun\pagesLTS@zero%
    \message{^^J}%
    \message{papermas: ******************** Paper mass ********************^^J}%
    \message{papermas: * This document consists of \arabic{pagesLTS.pagenr} pages.}
    \papermas@spaces{\arabic{pagesLTS.pagenr}}{16}%
    \message{papermas: * When printing \papermaspagespersheet\space pages on one sheet of paper,}
    \papermas@spaces{\papermaspagespersheet}{6}%
    \message{papermas: * \papermassheets\space sheets will be needed.}
    \papermas@spaces{\papermassheets}{26}%
    \message{papermas: * For ISO A \papermasformat\space paper of \papermasmasss\space g/m^2 specific mass,}
    \papermas@spaces{\papermasmasss}{7}%
    \message{papermas: * the printout will have a mass of about \papermasstotal\space g.}
    \papermas@spaces{\papermas@mbs}{5}%
    \message{papermas: ****************************************************^^J}
    \message{^^J}
  \fi%
  }

\newcommand\papermas@powerof[2]{%
  \PackageError{papermas}{Obsolete command \string\papermas@powerof\space used}{%
    The command \string\papermas@powerof\space has been removed from the papermas package.\MessageBreak%
    Please use e.g. \string\intcalcPow\space from the intcalc package instead.\MessageBreak%
    You can now just type Return to continue, but this error message will be\MessageBreak%
    issued again when using \string\papermas@powerof,\space and the command might be\MessageBreak%
    removed completely from future versions of the papermas package.\MessageBreak%
   }%
  \AtEndAfterFileList{%
    \message{^^J%
      papermas: Please remember to replace the \string\papermas@powerof\space command!^^J^^J%
     }%
   }%
  \pagesLTS@ifcounter{papermas@result}%
  \setcounter{papermas@result}{\intcalcPow{#1}{#2}}%
  }

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