% \iffalse
\NeedsTeXFormat{LaTeX2e}[1995/12/01]
%<*driver>
\ProvidesFile{totpages.drv}
      [2003/02/05 v1.10 Driver for Totpages Package (muewi)]
\documentclass{ltxdoc}
\usepackage{url}
\usepackage{totpages}
\usepackage[toc]{multitoc}
\GetFileInfo{totpages.sty}
\EnableCrossrefs
\CodelineIndex    % Index code by line number
\RecordChanges    % Changelog go .gls
\OnlyDescription  % comment out for implementation details
\setcounter{IndexColumns}{2}
%%\OldMakeIndex    % use if your MakeIndex is pre-v2.9
\begin{document}
   \setcounter{page}{1234}
   \DocInput{totpages.dtx}
\end{document}
%</driver>
%
% Copyright (c) [1999] by Wilhelm M\"uller.  All rights reserved.
%
% This program may be redistributed and/or modified under the terms
% of the LaTeX Project Public License, either version 1.1 of this
% license, or (at your option) any later version.
% The latest version of this license is in
%   CTAN:macros/latex/base/lppl.txt.
%
% For error reports in case of UNCHANGED versions see totpages.ins
%
% \fi
%
% \CheckSum{109}
%
%% \CharacterTable
%% {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%%  Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%%  Digits        \0\1\2\3\4\5\6\7\8\9
%%  Exclamation   \!     Double quote  \"     Hash (number) \#
%%  Dollar        \$     Percent       \%     Ampersand     \&
%%  Acute accent  \'     Left paren    \(     Right paren   \)
%%  Asterisk      \*     Plus          \+     Comma         \,
%%  Minus         \-     Point         \.     Solidus       \/
%%  Colon         \:     Semicolon     \;     Less than     \<
%%  Equals        \=     Greater than  \>     Question mark \?
%%  Commercial at \@     Left bracket  \[     Backslash     \\
%%  Right bracket \]     Circumflex    \^     Underscore    \_
%%  Grave accent  \`     Left brace    \{     Vertical bar  \|
%%  Right brace   \}     Tilde         \~}
%%
%% \iffalse meta-comment
%% ===================================================================
%%  @LaTeX-style-file{
%%     author          = {Wilhelm M\"uller},
%%     version         = "1.00",
%%     date            = "14 July 1999",
%%     filename        = "totpages.sty",
%%     address         = {Wilhelm M\"uller
%%                        Am Wall 139
%%                        D-28195 Bremen
%%     telephone       = "+49-421-1692525",
%%     email           = "muewi@acm.org",
%      codetable       = "ISO/ASCII",
%      keywords        = "LaTeX, pages",
%      supported       = "yes",
%%     docstring       = "LaTeX package which sets count1 to the total
%%                        number of pages in the document.
%%                        Uses the everyshi package."
%
%%  }
%% ===================================================================
%% \fi
%
%  \changes{v1.00}{1999/07/14}{Initial Version}
%  \changes{v1.10}{2003/02/05}{Fixed hook into hyperref}
%
%  \DoNotIndex{\@empty,\@firstofone,\addtocounter,\arabic}
%  \DoNotIndex{\begingroup,\countdef,\def,\edef,\else,\endgroup}
%  \DoNotIndex{\fi,\ifx,\immediate,\let,\MessageBreak}
%  \DoNotIndex{\newcommand,\newcounter,\newif}
%  \DoNotIndex{\ref,\relax,\stepcounter,\string,\textlatin}
%  \DoNotIndex{\the,\thepage,\undefined,\write}
%  \DoNotIndex{\ifHy@pageanchor,\if@filesw,\ifHy@hypertexnames}
%  \DoNotIndex{\ifHy@plainpages,\Hy@temp,\Hy@pagecounter}
%
%  \newcommand*{\Count}[1]{\texttt{\bslash{}count#1}}
%  \newcommand*{\package}[1]{\textsf{#1}}
%  \newcommand*{\ftype}[1]{\textsl{#1}}
%  \newcommand*{\NEWfeature}[1]{% stolen frm ltxguide.cls
%     \hskip 1sp \marginpar{\small\sffamily\raggedright
%     New feature\\#1}}
%
%  ^^A^^A We need to document a label -- we do this by using the macros for 
%  ^^A^^A environments and changing the macros for the index entries.
%  \makeatletter
%  \renewcommand*{\SpecialEnvIndex}[1]{^^A
%         \@bsphack\index{\string#1^^A
%         \actualchar\string\verb\quotechar*\verbatimchar
%         \string#1\verbatimchar
%         \encapchar usage}\@esphack}
%  \renewcommand*{\SpecialMainEnvIndex}[1]{^^A
%         \@bsphack\special@index{\string#1\actualchar
%         \string\verb\quotechar*\verbatimchar
%         \string#1\verbatimchar\encapchar main}%
%         \@esphack}
%  \makeatother
%
% ^^A -----------------------------
%
%  \title{\unskip
%         The \package{totpages} package^^A
%              \thanks{^^A
%                 The version number of this file is \fileversion,
%                 last revised \filedate.}
%        }
%  \author{Wilhelm M\"uller\\[0.5ex]
%          \normalsize  Am Wall 139\\
%          \normalsize  D-28195 Bremen\\
%          \normalsize  muewi@acm.org
%         }
%  \date{\filedate}
%  \maketitle
%
% ^^A -----------------------------
%
%
%  \begin{abstract}
%     \noindent This package counts the total number of pages shipped
%     out to the \ftype{dvi} file. The counter itself may be shipped out, too,
%     allowing page access by absolute page numbers, or it may be just
%     a regular \LaTeX\ counter.
%
%     The value that counter had at the end of  the previous \LaTeX\ run
%     may be referenced as |\ref{TotPages}|, the (\LaTeX) page number it appears
%     on as |\pageref{TotPages}|. This allows sentences like
%     ``This document contains \ref{TotPages} pages,
%     the last one being no.~\pageref{TotPages}.'' to be produced automatically.
%
%     This package can be used with with the package \package{hyperref}, producing
%     a hyperlink to the last page---though this is not of much use as most hypertext
%     readers allow direct access to the last page anyway.
%  \end{abstract}
%
%  \pagestyle{headings}
%
% ^^A -----------------------------
%
%  \tableofcontents
%
% ^^A -----------------------------
%
%  \section{Introduction}
%  ^^A
%  Printing only part of a \ftype{dvi} file can be difficult or impossible if pages in different 
%  parts of the document have the same number---e.\,g. in the 
%  frontmatter and the first text pages (iii vs. 3).
%
%  \TeX{} provides an easy solution to this problem: whenever a page
%  has been completed by the output routine and is being shipped out to
%  the \ftype{dvi} file,
%  it displayes the values of \Count{0} to \Count{9},
%  with trailing zeroes suppressed,
%  (e.\,g., \texttt{[1002.0.3]}) and writes them to the \ftype{log} and
%  \ftype{dvi} files, cf.\ \cite[p.~119]{KnuthTeXa}.
%  So,
%  if you have a \ftype{dvi} driver which allows page selection by other counters
%  than \Count{0}, you will be able to refer to absolute page numbers
%  when you make use of this package.
%
% \section{Using \package{totpages}}
%  ^^A
%  \DescribeEnv{TotPages}
%  Keeping a count of the absolute page number makes it
%  possible to provide the \emph{total} number of pages of the 
%  document (as opposed to the ``number'' on the last page provided
%  by the \package{lastpage} package, cf.\ \cite{package:lastpage}).
%
%  References to the counter itself won't make much sense since it is updated
%  asynchronously whenever an actual shipout occurs.
%  But by referencing the label |TotPages| (with |\ref{TotPages}|)
%  you get the total number of pages the document had at the end
%  of the previous run 
%  of \LaTeX. If, for some reason, you want to know what page number \LaTeX\ 
%  actually (would have) printed, you may use |\pageref{TotPages}|.
%
%  \DescribeMacro{\theTotPages}
%  What is actually printed by |\ref{TotPages}| is the result of the
%  macro |\theTotPages|, which normally is simply |\arabic{TotPages}|, but
%  may be redefined to anything (sensible)
%  somewhere before |\end{document}|.
%
%  If you want to use this package, you should load \package{totpages}
%  as the \emph{last} package in your document because it
%  executes some code at |\end{document}| and has to be sure that its 
%  code is the last code to be executed there.\footnote{See
%  \cite{package:lastpage} for a discussion of problems.}
%
%  You \emph{must not} use \package{count1to} or \package{lastpage}
%  together with this package. Package \package{count1to} provides additional
%  functionality, but will not co-operate with \package{hyperref}, whereas
%  any use of \package{lastpage} is covered by \package{totpages}.
%  If you try to use one (or both) of these packages together with
%  \package{totpages}, you will receive an error message at
%  |\begin{document}|. I don't think that something really bad will happen
%  if you ignore it (unless you use \package{count1to} together with
%  \package{hyperref}), but I won't guarantee anything.
%
%
%
% ^^A -----------------------------
%
%  \section{Options}
%  \DescribeEnv{dvi}
%  \DescribeEnv{nodvi}
%  With Option |dvi| (the default), \Count{1} is used for the absolute
%  page counter. This value is put into the \ftype{dvi} file and may be
%  used by the \ftype{dvi} driver.
%
%  Option |nodvi| uses a regular \LaTeX\ counter which will not
%  show up elsewhere.
%
%  No matter what counter is used, its name will always be |TotPages|,
%  e.\,g. if you want to change its format to roman (why would you want
%  to do this???).
%  
%  ^^A
%  
%
%
% ^^A -----------------------------
%
%  \section{Required packages}
%  ^^A
%  This package needs the \package{everyshi} 
%  package \cite{package:everyshi}.
%
%
% ^^A -----------------------------
%
%  \StopEventually{^^A
%
% ^^A -----------------------------
%
%  \section{Acknowledgements}
%  ^^A
%  This packages uses ideas inspired by
%  Martin Schr\"oder's \package{count1to}
%  package \cite{package:count1to}, Jeff Goldberg's \package{lastpage}
%  package \cite{package:lastpage}, and the \package{lastpage} support
%  contained in Sebastian Rahtz's \package{hyperref}
%  package \cite{package:hyperref}.
%
%
% ^^A -----------------------------
%
%  \begin{thebibliography}{HHHHHHHH\,1}
%     \raggedright
%     \bibitem[Goldberg]{package:lastpage}
%        Jeff Goldberg.
%        \newblock The \package{lastpage}-package.
%        \newblock \url{CTAN: tex-archive/macros/latex/contrib/other/lastpage/}.
%        \newblock \LaTeXe{} package.
%     \bibitem[Knuth]{KnuthTeXa}
%        Donald~E.\ Knuth.
%        \newblock \emph{The {\TeX}Book}, volume~A of \emph{Computers
%           and Typesetting}.
%        \newblock Addison-Wes\-ley, 1986.
%        \newblock Tenth printing, revised, June 1990.
%     \bibitem[Rahtz]{package:hyperref}
%        Sebastian Rahtz.
%        \newblock Hypertext marks in \LaTeX.
%        \newblock \url{CTAN: tex-archive/macros/latex/contrib/supported/hyperref/}.
%        \newblock \LaTeXe{} package.
%     \bibitem[Schr\"oder\,1]{package:count1to}
%        Martin Schr\"oder.
%        \newblock The \package{count1to} package.
%        \newblock \url{CTAN: tex-archive/macros/latex/contrib/supported/ms/count1to.dtx}.
%        \newblock \LaTeXe{} package.
%     \bibitem[Schr\"oder\,2]{package:everyshi}
%        Martin Schr\"oder.
%        \newblock The \package{everyshi} package.
%        \newblock \url{CTAN: tex-archive/macros/latex/contrib/supported/ms/everyshi.dtx}.
%        \newblock \LaTeXe{} package.
%  \end{thebibliography}
%
%     }
%
%
% ^^A -----------------------------
%
%  \section{The implementation}
%  ^^A
%  \setlength{\parindent}{0pt}
%  We need the \package{everyshi} package.
%    \begin{macrocode}
%<*package>
\ProvidesPackage{totpages}
        [1999/07/13 v1.00 Totpages Package (muewi)]
\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\RequirePackage{everyshi}[1994/12/09]
%    \end{macrocode}
%
%  The user can decide if the absolute page number should
%  go to the \ftype{dvi} file or not, the default being yes:
%    \begin{macrocode}
\newif\ifTotPagesToDvi\TotPagesToDvitrue
\DeclareOption{dvi}{\TotPagesToDvitrue}
\DeclareOption{nodvi}{\TotPagesToDvifalse}
\ProcessOptions*\relax
%    \end{macrocode}
%
%  \begin{environment}{TotPages}
%  If the counter is to go to the \ftype{dvi} file, we have to use a
%  fixed count register: \Count{1}.  (For this to work, we have to copy
%  a few internals from latex.ltx.)  Otherwise, we'll just use a
%  conventional \LaTeX\ counter.
%    \begin{macrocode}
\ifTotPagesToDvi
  \countdef\c@TotPages=1 \c@TotPages=0
  \let\cl@TotPages\@empty
  \newcommand\theTotPages{\arabic{TotPages}}
\else
  \newcounter{TotPages}
\fi
%    \end{macrocode}
%  To save a bit of space,
%  the control sequences for the options are removed.
%    \begin{macrocode}
\let\ifTotPagesToDvi\undefined
\let\TotPagesToDvitrue\undefined
\let\TotPagesToDvifalse\undefined
%    \end{macrocode}
%  When shipout occurs, we step the counter.
%    \begin{macrocode}
\EveryShipout{\stepcounter{TotPages}}
%    \end{macrocode}
%  Nothing outside this package should mess with that counter, of course.---
%  At the beginning of the document, we should be able to find out
%  what packages are used and warn about some we aren't sure we can
%  co-operate with:
%
%    \begin{macrocode}
\AtBeginDocument{%
  \@ifpackageloaded{count1to}{%
    \PackageError{totpages}{%
      Can't use both, count1to and totpages}{%
      You should read the documentation of these packages and\MessageBreak
      decide which one is more appropriate for your intentions.}
  }{}%
  \@ifpackageloaded{lastpage}{%
    \PackageError{totpages}{%
      Can't use both, lastpage and totpages}{%
      You should read the documentation of these packages and\MessageBreak
      decide which one is more appropriate for your intentions.}
  }{}%
%    \end{macrocode}
%  We define a command which will be executed at the end of the document,
%  but if anybody did it already, we simply hope they did it right.
%    \begin{macrocode}
  \ifx\undefined\TotPages@putlabel
%    \end{macrocode}
%  If the package \package{hyperref} is used,
%  we think we know what to do to produce a correct hyperlink label.
%  (This code was obtained from the \package{lastpage} support of
%  \package{hyperref} itself.)
%    \begin{macrocode}
    \@ifpackageloaded{hyperref}{%
      \ifx\undefined\Hy@WarningNoLine
        \let\Hy@WarningNoLine\hyper@warn
      \fi
      \ifHy@pageanchor
      \else
        \Hy@WarningNoLine{%
          The \string\ref{TotPages} link doesn't work\MessageBreak
          with disabled option `pageanchor'%
      }%
      \fi
      \def\TotPages@putlabel{%
        \addtocounter{page}{-1}%
        \if@filesw
          \begingroup
            \let\@number\@firstofone
            \ifHy@pageanchor
              \ifHy@hypertexnames
                \ifHy@plainpages
                  \def\Hy@temp{\arabic{page}}%
                \else
                  \let\textlatin\@firstofone
                  \edef\Hy@temp{\thepage}%
                \fi
              \else
                \def\Hy@temp{\the\Hy@pagecounter}%
              \fi
            \fi
            \immediate\write\@mainaux{%
              \string\newlabel
                {TotPages}{{\theTotPages}{\thepage}{}{%
                  \ifHy@pageanchor page.\Hy@temp\fi}{}}%
            }%
          \endgroup
        \fi
        \addtocounter{page}{1}%
      }%
%    \end{macrocode}
%  Otherwise, we just put a label here, but |\label| may refer
%  to some random counter just stepped some time ago, so we have
%  produce it ourselves.
%    \begin{macrocode}
    }{%
      \def\TotPages@putlabel{%
        \addtocounter{page}{-1}%
        \immediate\write\@mainaux{%
          \string\newlabel{TotPages}{{\theTotPages}{\thepage}}%
        }%
        \addtocounter{page}{1}%
      }%
    }%
  \fi
}
%    \end{macrocode}
%  Before we write the value to the auxfile we call a \cs{clearpage}
%  to force all pending floats to be output.
%  To do this we have to be sure to be the \emph{last} macro called by
%  \cs{AtEndDocument}---but this is nearly impossible to assure,
%  so we try to do our very best and set our 
%  entry for \cs{AtEndDocument} at |\begin{document}| by using 
%  \cs{AtBeginDocument}.
%    \begin{macrocode}
\AtBeginDocument{%
  \AtEndDocument{%
%    \end{macrocode}
%  The label \texttt{TotPages} is put onto the last page
%  of the document and holds the total number of pages of the 
%  document, i.\,e., the value of |TotPages|.
%    \begin{macrocode}
     \clearpage
     \TotPages@putlabel
  }%
}
%    \end{macrocode}
%  \end{environment}
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%
% ^^A -----------------------------
%
%  \Finale
%
%   \PrintChanges\PrintIndex
%   ^^A Make sure that the index is not printed twice
%   ^^A (ltxdoc.cfg might have a second \PrintIndex command)
%   \let\PrintChanges\relax
%   \let\PrintIndex\relax
