% \iffalse meta-comment
% 
% This is file `totalcount.dtx'.
% 
% Copyright (C) 2010-2018 Axel Sommerfeldt (axel.sommerfeldt@f-m.fm)
% 
% --------------------------------------------------------------------------
% 
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2003/12/01 or later.
% 
% This work has the LPPL maintenance status "maintained".
% 
% This Current Maintainer of this work is Axel Sommerfeldt.
% 
% This work consists of the file totalcount.dtx,
% and the derived file totalcount.sty.
% 
% \fi
% \CheckSum{126}
%
% \iffalse
%<*driver>
\NeedsTeXFormat{LaTeX2e}[1994/12/01]
\ProvidesFile{totalcount.drv}[2013/05/12 v1.0 Total values of counters]
\hbadness=9999 \newcount\hbadness \hfuzz=74pt % Make TeX shut up.
%\errorcontextlines=3
%
\documentclass{ltxdoc}
\setlength\parindent{0pt}
\setlength\parskip{\smallskipamount}
%
\usepackage{ifpdf}
\ifpdf
  \usepackage{mathptmx,courier}
  \usepackage[scaled=0.90]{helvet}
% \addtolength\marginparwidth{15pt}
\fi
%
\usepackage[bottom]{footmisc}
\usepackage{hypdoc}
\ifpdf\usepackage{hypdestopt}\fi
\hypersetup{pdfkeywords={LaTeX, package, totalcount},pdfstartpage={},pdfstartview={}}
%
\usepackage{totalcount}[2008/02/16]
%
\newcommand*\purerm[1]{{\upshape\mdseries\rmfamily #1}}
\newcommand*\puresf[1]{{\upshape\mdseries\sffamily #1}}
\newcommand*\purett[1]{{\upshape\mdseries\ttfamily #1}}
\let\package\puresf
\let\env\purett \let\opt\purett
%
\newcommand*\csmarg[1]{\texttt{\char`\{#1\char`\}}}
\newcommand*\csoarg[1]{\texttt{\char`\[#1\char`\]}}
\newcommand*\version[2][]{$v#2$}
%
\usepackage{marvosym}
\makeatletter
\newcommand*\INFO{\@ifstar{\@INFO{}}{\@INFO{\vbox to \ht\strutbox}}}
\newcommand*\@INFO[1]{\MARGINSYM{#1{\LARGE\Info}}}
\makeatother
%
\begin{document}
  \DocInput{totalcount.dtx}
\end{document}
%</driver>
% \fi
%
% \def\thispackage{the \package{totalcount} package}
% \def\Thispackage{The \package{totalcount} package}
%
% \GetFileInfo{totalcount.drv}
% \let\docdate\filedate
% \GetFileInfo{totalcount.sty}
%
% \title{\texorpdfstring{\Thispackage\thanks{%^^A
%          This package has version number \fileversion, last revised \filedate.}}%^^A
%        {The totalcount package}}
% \author{Axel Sommerfeldt\\
%         \url{https://gitlab.com/axelsommerfeldt/caption}}
% \date{\docdate}
% \maketitle
% 
% \begin{abstract}
% This package offers commands for typesetting total values of counters.
% \end{abstract}
%
% \iffalse\clearpage\fi
% \setcounter{tocdepth}{2}
% \tableofcontents
%
% \iffalse\clearpage\fi
% \pagebreak[3]
% \section{Loading the package}
%
% Load this package using
% \begin{quote}
%   |\usepackage|\oarg{options}|{totalcount}|\quad,
% \end{quote}
% where \meta{options} is a list of names of \LaTeX\ counters, e.g.:
% \begin{quote}
%   |\usepackage[figure,table]{totalcount}|\quad.
% \end{quote}
% \pagebreak[3]
% This way the commands |\total|\-|figures| and |\total|\-|tables| will be defined which
% are typesetting the total number of figures resp.~tables, e.g.:
% \begin{quote}
%   |This document contains \totalfigures\ figures.|
% \end{quote}
% \pagebreak[3]
% Furthermore the commands |\if|\-|total|\-|figures| and |\if|\-|total|\-|tables| are offered
% for typesetting text only if the document contains figures resp.~tables at all, e.g.:
% \begin{quote}
%   |\iftotalfigures|\\
%   |  \listoffigures|\\
%   |\fi|
% \end{quote}
% \pagebreak[3]
% This should work with other counters as well (for example |equation|,
% or |lst|\-|listing| offered by the \package{listings} package~\cite{listings}),
% but please note that using the |page| counter here is not supported.
%
% \pagebreak[3]
% \section{\cs{DeclareTotalCounter}}
% \begin{quote}
%   |\DeclareTotalCounter|\marg{counter}
% \end{quote}
% actually defines the commands |\total|\meta{counter}|s| and |\if|\-|total|\meta{counter}|s|.
%
% \pagebreak[3]
% So in fact
% \begin{quote}
%   |\usepackage[figure,table]{totalcount}|
% \end{quote}
% is identical to
% \begin{quote}
%   |\usepackage{totalcount}|\\
%   |\DeclareTotalCounter{figure}|\\
%   |\DeclareTotalCounter{table}|
% \end{quote}
%
% \pagebreak[3]
% \section{Spacing}
% The space after all \LaTeX\ commands without (optional or mandatory) arguments
% like |\total|\-|figures| will not be typeset,
% so these commands needs to be finished with an additional backslash or an empty
% pair of curly braces right before the space character,
% or to be finished with a tilde (swung dash),
% so \TeX\ will be prevented from omitting the following space character.
%
% \pagebreak[3]
% This could be avoided by defining new commands with the help of
% the \package{xspace} package~\cite{xspace}.
% When using \thispackage\ with the package option |xspace| this will be actually done,
% so after
% \begin{quote}
%   |\usepackage[figure,table,xspace]{totalcount}|
% \end{quote}
% code like
% \begin{quote}
%   |This document contains \totalfigures figures.|
% \end{quote}
% will give correct results without a missing space between the total number of figures
% and the word ``figures''.
%
% \pagebreak[3]
% \section{Thanks}
% I would like to thank Herbert Vo\ss\ since the implementation of this package is
% based on an idea from him which could be found at
% \url{http://texnik.dante.de/cgi-bin/mainFAQ.cgi?file=counter/counter#number}.
%
% \iffalse
% --------------------------------------------------------------------------- %
% \fi
%
% \StopEventually{%^^A
% \begin{thebibliography}{9}
%   \bibitem{listings}
%   Carsten Heinz \& Brooks Moses:\\
%   \href{http://www.ctan.org/pkg/listings}%
%        {\emph{The Listings Package}},\\
%  2007/02/22
%   \bibitem{xspace}
%   David Carlisle and Morten H\o gholm:\\
%   \href{http://www.ctan.org/pkg/xspace}%
%        {\emph{The xspace package}},
%   2009/10/20
% \end{thebibliography}
% }
%
% \iffalse
% --------------------------------------------------------------------------- %
% \fi
%
% \DoNotIndex{\\,\_,\ ,\@@par}
% \DoNotIndex{\@bsphack}
% \DoNotIndex{\@car,\@cdr,\@classoptionslist,\@cons,\@currext,\@currname}
% \DoNotIndex{\@ehc,\@ehd,\@empty,\@esphack,\@expandtwoargs}
% \DoNotIndex{\@for,\@firstofone,\@firstoftwo}
% \DoNotIndex{\@gobble,\@gobblefour,\@gobbletwo,\@hangfrom}
% \DoNotIndex{\@ifnextchar,\@ifpackagelater,\@ifpackageloaded}
% \DoNotIndex{\@ifstar,\@ifundefined,\@latex@error,\@namedef,\@nameuse}
% \DoNotIndex{\@onlypreamble,\@parboxrestore,\@plus,\@ptionlist}
% \DoNotIndex{\@removeelement,\@restorepar,\@secondoftwo,\@setpar}
% \DoNotIndex{\@tempa,\@tempboxa,\@tempdima,\@tempdimb,\@tempdimc,\@tempb,\@tempc}
% \DoNotIndex{\@testopt}
% \DoNotIndex{\@undefined,\@unprocessedoptions,\@unusedoptionlist}
% \DoNotIndex{\p@,\z@}
% \DoNotIndex{\active,\addtocounter,\addtolength,\advance,\aftergroup}
% \DoNotIndex{\baselineskip,\begin,\begingroup,\bfseries,\box}
% \DoNotIndex{\catcode,\centering,\changes,\csname,\def,\divide,\do,\downarrow}
% \DoNotIndex{\edef,\else,\empty,\end,\endcsname,\endgraf,\endgroup,\expandafter}
% \DoNotIndex{\fi,\footnotesize,\global}
% \DoNotIndex{\hangindent,\hbox,\hfil,\hsize,\hskip,\hspace,\hss}
% \DoNotIndex{\ifcase,\ifdim,\ifnum,\ifodd,\ifvoid,\ifvmode}
% \DoNotIndex{\ifx,\ignorespaces,\itshape}
% \DoNotIndex{\Large,\large,\leavevmode,\leftmargini,\leftskip,\let,\linewidth}
% \DoNotIndex{\llap,\long,\m@ne,\margin,\mdseries,\message}
% \DoNotIndex{\newcommand,\newdimen,\newlength,\newline,\newif,\newsavebox}
% \DoNotIndex{\next,\nobreak,\nobreakspace,\noexpand,\noindent,\numberline}
% \DoNotIndex{\normalcolor,\normalfont,\normalsize,\or,\par,\parbox,\parfillskip}
% \DoNotIndex{\parindent,\parskip,\prevdepth,\protect,\protected@edef,\protected@write}
% \DoNotIndex{\providecommand,\quad}
% \DoNotIndex{\raggedleft,\raggedright,\relax,\renewcommand,\RequirePackage}
% \DoNotIndex{\rightskip,\rmfamily}
% \DoNotIndex{\sbox,\scriptsize,\scshape,\setbox,\setlength,\sffamily,\slshape}
% \DoNotIndex{\small,\string,\space,\strut}
% \DoNotIndex{\textheight,\the,\toks@,\typeout,\ttfamily}
% \DoNotIndex{\unvbox,\uparrow,\upshape,\usebox,\usepackage}
% \DoNotIndex{\value,\vbox,\vsize,\vskip,\wd,\width,\z@skip}
% \DoNotIndex{\AtBeginDocument,\AtEndOfPackage,\CurrentOption,\DeclareOption}
% \DoNotIndex{\ExecuteOptions,\GenericWarning,\IfFileExists,\InputIfFileExists}
% \DoNotIndex{\NeedsTeXFormat,\MessageBreak}
% \DoNotIndex{\PackageError,\PackageInfo,\PackageWarning,\PackageWarningNoLine}
% \DoNotIndex{\PassOptionsToPackage,\ProcessOptions,\ProvidesPackage}
%
% \iffalse
% --------------------------------------------------------------------------- %
% \fi
%
% \setlength{\parskip}{0pt plus 1pt}
% \changes{v1.0}{2008/02/16}{First version}
%
% \iffalse
% --------------------------------------------------------------------------- %
% \fi
%
% \clearpage
% \section{The implementation}
% \iffalse
%<*package>
% \fi
%
% \subsection{Identification}
%
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}[1994/12/01]
\ProvidesPackage{totalcount}[2018/01/21 v1.0a Total values of counters (AR)]
%    \end{macrocode}
%
% \subsection{Initial code}
%
% \begin{macro}{\DeclareTotalCounter}
% \changes{v1.0}{2013/05/12}{Usage of \cs{totalcount@counters} delayed}
% |\DeclareTotalCounter|\marg{counter name}
%    \begin{macrocode}
\newcommand*\DeclareTotalCounter[1]{%
  \newcounter{totalcount@#1}%
  \@namedef{total#1s}{\nfss@text{\reset@font\bfseries ??}}%
  \@namedef{total#1es}{\@nameuse{total#1s}}%
  \expandafter\newif\csname iftotal#1s\endcsname
  \@namedef{iftotal#1es}{\@nameuse{iftotal#1s}}%
% \@nameuse{totalcount@set@#1}%
  \AtBeginDocument{\@ifundefined{c@#1}%
    {\PackageError{totalcount}{Undefined counter `#1'}\@ehc}%
    {\@cons\totalcount@counters{{#1}}}}}
\@onlypreamble\DeclareTotalCounter
%    \end{macrocode}
%    \begin{macrocode}
\let\totalcount@counters\@empty
%    \end{macrocode}
% \end{macro}
%
% \subsection{Declaration of options}
%
%    \begin{macrocode}
\newcommand*\totalcount@xspace{}
\DeclareOption{xspace}{\renewcommand*\totalcount@xspace{\xspace}}
%    \end{macrocode}
%    \begin{macrocode}
\DeclareOption*{\expandafter\DeclareTotalCounter\expandafter{\CurrentOption}}
%    \end{macrocode}
%
% \subsection{Execution of options}
%
%    \begin{macrocode}
\ProcessOptions\relax
\ifx\totalcount@xspace\@empty\else
  \RequirePackage{xspace}
\fi
%    \end{macrocode}
%
% \subsection{Main code}
%
% \begin{macro}{\totalcount@addtocounter}
% |\totalcount@addtocounter|\marg{counter name}
%    \begin{macrocode}
\newcommand*\totalcount@addtocounter[1]{%
  \@ifundefined{c@totalcount@#1}{}{%
    \addtocounter{totalcount@#1}{\value{#1}}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@stpelt}
% |\@stpelt|\marg{counter name}
%    \begin{macrocode}
\let\totalcount@stpelt@ORI\@stpelt
\renewcommand*\@stpelt[1]{%
  \totalcount@addtocounter{#1}%
  \totalcount@stpelt@ORI{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\totalcount@set}
% \changes{v1.0a}{2018/01/21}{A negative counter value will be handled correctly now}
% |\totalcount@set|\marg{counter name}\marg{value}
%    \begin{macrocode}
\newcommand*\totalcount@set[2]{%
  \@ifundefined{c@totalcount@#1}%
%   {\global\@namedef{totalcount@set@#1}{\totalcount@set{#1}{#2}}}%
    {}%
    {\global\@namedef{total#1s}{#2\totalcount@xspace}%
     \ifnum#2<1\relax \else
       \global\@nameuse{total#1strue}%
     \fi}}
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
\AtBeginDocument{\AtEndDocument{%
  \begingroup
    \@tempswafalse
    \if@filesw
      \immediate\write\@mainaux{%
        \string\providecommand\string\totalcount@set[2]{}}%
    \fi
    \def\@elt#1{%
      \totalcount@addtocounter{#1}%
      \edef\totalcount@tempa{\the\value{totalcount@#1}}%
      \expandafter\let\expandafter\totalcount@tempb\csname total#1s\endcsname
      \@expandtwoargs\totalcount@set{#1}{\totalcount@tempa}%
      \expandafter\ifx\csname total#1s\endcsname\totalcount@tempb \else
        \@tempswatrue
      \fi
      \if@filesw
        \immediate\write\@mainaux{%
          \string\totalcount@set{#1}{\totalcount@tempa}}%
      \fi}%
    \totalcount@counters
    \if@tempswa
      \PackageWarningNoLine{totalcount}{%
        Total counter(s) have changed.\MessageBreak
        Rerun to get them right}%
    \fi
  \endgroup}}%
%    \end{macrocode}
%
% \iffalse
%</package>
% \fi
%
% \iffalse
% --------------------------------------------------------------------------- %
% \fi
%
% \Finale
%
\endinput
