summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/totcount/totcount.dtx
blob: 21433e0e00247b81fbddc513f9865282595bc1d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
\def\totcfileversion{1.1}
\def\totcfiledate{2009/07/09}
\def\totcdocdate{9 Jul 2009}
% ^^A**********************************************************************
% \iffalse
\NeedsTeXFormat{LaTeX2e}
%<*drv>
\ProvidesFile{totcount.drv}
      [\totcfiledate \space v\totcfileversion \space Driver for the TotCount package]
\documentclass{ltxdoc}
\RequirePackage{hypdoc}
\RequirePackage{tocloft}
% use leader dots with section headers (by default, only the subsections had them
\renewcommand{\cftsecleader}{\cftdotfill{\cftdotsep}}
\RequirePackage[toc]{multitoc}
\RequirePackage{setspace}
\RequirePackage{totcount}
\EnableCrossrefs
\CodelineIndex
\setcounter{IndexColumns}{2}
\RecordChanges
%\OnlyDescription  % comment out for hiding implementation details
\newcommand*{\CTAN}[1]{%
  \href{http://www.ctan.org/tex-archive/#1}{\nolinkurl{CTAN:#1}}}
\urldef\totcounturl\nolinkurl{CTAN:macros/latex/contrib/totcount/}
\def\CTANtotcount{\href{http://www.ctan.org/tex-archive/macros/latex/contrib/totcount/}{\totcounturl}}
\newcommand*{\xpackage}[1]{\textsf{#1}}
\begin{document}
  \DocInput{totcount.dtx} \PrintIndex \PrintChanges
\end{document}
%</drv>
%
% ^^A**********************************************************************
%
% The TotCount package, version \totcfileversion, \totcfiledate 
%
% Copyright (c) [2009] Vasileios Koutavas (Vasileios.Koutavas@cs.tcd.ie)
%
% This program may be re-distributed and/or modified under the terms of the
% LaTeX Project Public License version 1.3c, or any later version.
% The latest version of this license is in
%   CTAN:/macros/latex/base/lppl.txt
%
% \fi
%
% \changes{v1.1}{2009/07/09}{Fixes bug that mistakenly sets the date of any document using totcount}
% \changes{v1.0}{2009/04/10}{First release}
%
% \MakeShortVerb{\|}
%
%
% \DoNotIndex{\@ifnextchar,\@mainaux,\csname,\newcommand}
% \DoNotIndex{\def,\else,\endcsname,\expandafter}
% \DoNotIndex{\fi,\gdef,\ifnum,\ifx,\immediate,\documentclass}
% \DoNotIndex{\newwrite,\number,\begin,\section,\tt}
% \DoNotIndex{\relax,\sp,\space,\string,\end,\let}
%
%
% \title{The \xpackage{totcount} package^^A
%              \thanks{Available at \CTANtotcount.}}
% \author{Vasileios Koutavas\\\normalsize\texttt{Vasileios.Koutavas@cs.tcd.ie}}
% \date{\totcdocdate}
%
% \maketitle
%
% \begin{abstract}
% This is the documentation for the \xpackage{totcount} package, a package for
% computing and displaying the value that was assigned last to counters inside
% a document.  With this package one may count the total number of elements
% (e.g. sections, pages, citations, etc.) in a \LaTeX\ document by simply using
% standard \LaTeX\ counters.
% \end{abstract}
%
% \addtolength{\columnsep}{15pt}
% \tableofcontents
% \addtolength{\columnsep}{-15pt}
%
% \newtotcounter{citnum}
% \def\oldbibitem{}
% \let\oldbibitem=\bibitem
% \def\bibitem{\stepcounter{citnum}\oldbibitem}
%
% \newtotcounter{citesnum}
% \def\oldcite{}
% \let\oldcite=\cite
% \def\cite{\stepcounter{citesnum}\oldcite}
%
% \section{Introduction}
%
% Referring to the total number of sections, pages, citations, list items, or
% anything else in a document can be difficult to achieve. The difficulty comes
% when the reference is \emph{before} the definition of all the elements that
% need counting. In this case, abandoning the manual way  requires the use of
% auxiliary files in much the same way that \LaTeX\ uses for references and
% citations.
%
% Special packages like \xpackage{totpages} \cite{totpages} and
% \xpackage{lastpage} \cite{lastpage} handle this issue for the total number of
% pages, but, to the best of my knowledge, there is no package that makes it easy
% to print the total number of arbitrary elements.
%
% The \xpackage{TotCount} package hopefully fills this gap. It enables the
% computation and display of the number that was assigned last to a counter
% inside a document (usually the maximum value of the counter).  The user just
% has to include the \xpackage{TotCount} package, and \emph{register} a counter
% as a ``total counter''. Then, getting the maximum value of that counter is as
% easy as calling the macro |\total|\marg{counter} at the desired place, and
% running \LaTeX\ twice.
%
% As an early example, the commands
%    \begin{macrocode}
%<*doc>
\regtotcounter{section}\total{section}
%</doc>
%    \end{macrocode}
% inform the reader that there are \regtotcounter{section}\total{section}
% sections in this document. With only slightly more work (since there is no
% convenient counter already defined) we can write that this document has
% \total{citnum} citations, which it mentions \total{citesnum} times:
%    \begin{macrocode}
%<*doc>
\newtotcounter{citnum}
\def\oldbibitem{} \let\oldbibitem=\bibitem
\def\bibitem{\stepcounter{citnum}\oldbibitem}
\total{citnum}

\newtotcounter{citesnum}
\def\oldcite{} \let\oldcite=\cite
\def\cite{\stepcounter{citesnum}\oldcite}
\total{citesnum}
%</doc>
%    \end{macrocode}
%
%
% \section{User Interface}
%
% \DescribeMacro{\regtotcounter}
% To display the maximum value of a counter we first need to \emph{register}
% that counter as a ``total counter''. We can do this by calling the macro
% |\regtotcounter|\marg{counter} on an existing counter. We can register a
% counter in the preamble or inside the document, but in any case before
% calling |\total| or |\totvalue| on that counter.
%
% This macro will use the main auxiliary file to get the total number of the
% counter at the second time we run \LaTeX\ on the document.
%
% To use a file other than the main auxiliary file we need to pass an option to
% the macro: |\regtotcounter[auxfile=|\meta{file}|]|\marg{counter}. This way
% the last value of \meta{counter} will be stored in \meta{file}. This macro
% will also input \meta{file} the second time that \LaTeX\ runs (and every time
% after that) to get the right total value of the counter.  The same auxiliary
% file can be used for many total counters and multiple auxiliary files can be
% used within the same document.
%
% \DescribeMacro{\newtotcounter}
% The macro |\newtotcounter| is a shorthand for creating a new counter and
% registering it as a total counter. Just like |\regtotcounter|, we can pass an
% |auxfile| option to this macro to make it use an alternative auxiliary file:
% |\newtotcounter[auxfile=|\meta{file}|]|\marg{counter}.
%
% \DescribeMacro{\total}
% To print the maximum value of \meta{counter} we can call the macro
% |\total|\marg{counter}. The first time \LaTeX\ runs on the document this
% macro will display the symbols ``$??$'' and write the message
% ``|Rerun to get correct total counts|'' in the terminal. The second time
% \LaTeX\ runs on the same document (and every time after that) the macro will
% display the correct total count of \meta{counter}.
%
% \DescribeMacro{\totvalue}
% We can obtain the numeric total value of \meta{counter} (in contrast to
% printing it using |\total|) by calling the macro |\totvalue|\marg{counter}.
% This is useful, for example, when we want to test the total value of the
% counter (see example in Section~\ref{sec:example}). The first time that
% \LaTeX\ runs on a document, where the total counts are not computed yet, this
% command returns |-1|.
%
% \DescribeMacro{\usetotcountfile}
% Sometimes we might want to use the total counters of an auxiliary file
% without recomputing (and overriding) their value. This is especially useful
% when the total counters have been computed by running \LaTeX\ on a
% different document. In this case, instead of registering a total
% counter with one of the commands |\regtotcounter| and |\newtotcounter| we can
% simply use the auxiliary file that contains the values of the  desired total
% counters by calling the macro |\usetotcountfile|\marg{file}. This
% functionality is the main reason why total counters can be stored in
% alternative auxiliary files.
%
% \DescribeMacro{\newcounter}
% \DescribeMacro{\addtocounter}
% \DescribeMacro{\stepcounter}
% \DescribeMacro{\setcounter}
% \DescribeMacro{\value}
% Since this package uses regular \LaTeX\ counters, we can use all the usual
% commands to define, set, increment, and get the current (\emph{not the
% total}) value of counters. See The \LaTeX\ Companion \cite{latexcompanion}
% for an explanation of how to use \LaTeX\ counters, and
% Section~\ref{sec:example} for sample uses of these macros.
%
%
% \section{\label{sec:installation}Installation}
%
% To get the package file run
% \begin{verbatim}
%latex totcount.ins \end{verbatim}
% Then copy the |totcount.sty| file in a directory accessible by (pdf)\LaTeX\
% and re-hash the latex tree.
% 
% To compile the documentation run
% \begin{verbatim}
%pdflatex totcount.drv
%makeindex -s gind.ist totcount
%makeindex -s gglo.ist -o totcount.gls totcount.glo
%pdflatex totcount.drv  \end{verbatim}
%
% To run the example do
% \begin{verbatim}
%pdflatex totcount-ex.tex
%pdflatex totcount-ex.tex  \end{verbatim}
%
%
% \section{Required Packages}
% The \xpackage{TotCount} package requires the \xpackage{keyval} package
% \cite{keyval}.
%
% ^^A**********************************************************************
%
% \StopEventually{}
%
% \section{Implementation}
%
% \setlength{\parindent}{0pt}
%
% This section contains the source code of |totcount.sty|.
%
%    \begin{macrocode}
%<*sty>
\ProvidesPackage{totcount}
  [\totcfiledate \space v\totcfileversion \space package for getting%
   the total value of LaTeX counters]
%    \end{macrocode}
%%
%%   Import of the \xpackage{keyval} package \cite{keyval}:
%    \begin{macrocode}
\RequirePackage{keyval}
%    \end{macrocode}
%%
%    \begin{macro}{\newtotcounter}
%%   Creates a new counter and registers it as a total counter. This is the
%%   top-level dispatch of the macro, depending on whether there is an optional
%%   argument or not.
%    \begin{macrocode}
\def\newtotcounter{%
  \@ifnextchar[\newtotcounter@newaux\newtotcounter@mainaux}
%    \end{macrocode}
%    \end{macro}
%%
%    \begin{macro}{\newtotcounter@newaux}
%%   This is the version of the |\newtotcounter| macro that uses a separate
%%   auxiliary file. It first creates the counter (second argument) and then
%%   calls the macro |\regtotcounter|.
%    \begin{macrocode}
\def\newtotcounter@newaux[#1]#2{%
  \newcounter{#2}%
  \regtotcounter[#1]{#2}%
}
%    \end{macrocode}
%    \end{macro}
%%
%    \begin{macro}{\newtotcounter@mainaux}
%%   This is the version of the |\newtotcounter| macro that uses the main
%%   auxiliary file.  It first creates the counter (argument) and then calls
%%   the macro |\regtotcounter|.
%    \begin{macrocode}
\def\newtotcounter@mainaux#1{%
  \newcounter{#1}%
%    \end{macrocode}
%% Register the counter:
%    \begin{macrocode}
  \regtotcounter{#1}%
}
%    \end{macrocode}
%    \end{macro}
%%
%    \begin{macro}{\regtotcounter}
%%   Registers a counter as a total counter. This is the top-level dispatch of
%%   the macro, depending on whether there is an optional argument or not.
%    \begin{macrocode}
\def\regtotcounter{%
  \@ifnextchar[\regtotcounter@newaux\regtotcounter@mainaux}
%    \end{macrocode}
%    \end{macro}
%%
%%   The following is the definition of the |auxfile| key for specifying an
%%   alternative auxiliary file when calling the macro |\regtotcounter| (see
%%   \cite{keyval}).
%    \begin{macrocode}
\define@key{totcounter}{auxfile}{\def\this@auxfile{#1}}
%    \end{macrocode}
%%
%    \begin{macro}{\regtotcounter@newaux}
%%   This is the version of the |\regtotcounter| macro that uses a separate
%%   auxiliary file. The auxiliary file is passed as a first argument in the
%%   form of a key--value pair |[auxfile=|\meta{file}|]|, and the counter to be
%%   registered is passed as the second argument.
%%   \begin{macrocode}
\def\regtotcounter@newaux[#1]#2{%
  \setkeys{totcounter}{#1}%
%    \end{macrocode}
%% Try to load the contents of the file:
%    \begin{macrocode}
  \InputIfFileExists{\this@auxfile}{}{}%
%    \end{macrocode}
%% Make sure that the auxiliary file is open; \LaTeX\ will close it at the end:
%    \begin{macrocode}
  \expandafter\ifx\csname \this@auxfile @open\endcsname\relax%
    \expandafter\gdef\csname \this@auxfile @open\endcsname{}%
    \expandafter\newwrite\csname \this@auxfile @stream\endcsname%
    \immediate\expandafter\openout%
      \csname \this@auxfile @stream\endcsname=\this@auxfile%
  \fi%
%    \end{macrocode}
%% Create a new counter holding the total number of the actual counter:
%    \begin{macrocode}
  \expandafter\ifx\csname c@#2@totc\endcsname\relax%
      \newcounter{#2@totc}%
      \setcounter{#2@totc}{-1}%
  \fi%
%    \end{macrocode}
%% At the end of the document write code in the auxiliary file to update the
%% total counter with the value of the actual counter:
%    \begin{macrocode}
  \AtEndDocument{%
     \def\sp{ }%
     \immediate\expandafter\write%
     \csname \this@auxfile @stream\endcsname{%
       \string\expandafter\string\ifx%
         \string\csname\sp c@#2@totc\string\endcsname\string\relax%
       \string\newcounter{#2@totc}%
       \string\fi%
       \string\setcounter{#2@totc}{\number\value{#2}}%
     }%
  }%
}
%    \end{macrocode}
%    \end{macro}
%%
%    \begin{macro}{\regtotcounter@mainaux}
%%   This is the version of the |\regtotcounter| macro that uses the main
%%   auxiliary file. The counter to be registered is passed as the second
%%   argument.
%    \begin{macrocode}
\def\regtotcounter@mainaux#1{%
%    \end{macrocode}
%% Create a new counter holding the total number of the actual counter:
%    \begin{macrocode}
  \expandafter\ifx\csname c@#1@totc\endcsname\relax%
      \newcounter{#1@totc}%
      \setcounter{#1@totc}{-1}%
  \fi%
%    \end{macrocode}
%% At the end of the document write code in the auxiliary file to update the
%% total counter with the value of the actual counter:
%    \begin{macrocode}
  \AtEndDocument{%
    \def\sp{ }%
    \immediate\write\@mainaux{%
      \string\expandafter\string\ifx%
        \string\csname\sp c@#1@totc\string\endcsname\string\relax%
      \string\newcounter{\string #1@totc}%
      \string\fi%
      \string\setcounter{\string #1@totc}{\number\value{#1}}%
    }%
  }%
}
%    \end{macrocode}
%    \end{macro}
%%
%    \begin{macro}{\total}
%%   Prints the total value of a registered total counter that is passed as
%%   argument. If the total value is yet to be computed (at the first time
%%   \LaTeX\ runs on the document) then its value is |-1| and the output of the
%%   command is $??$.
%    \begin{macrocode}
\newcommand\total[1]{%
  \def\tmp@val{\value{#1@totc}}%
  \ifnum\tmp@val=-1%
    \message{Rerun to get correct total counts}%
    $??$%
  \else%
    \number\value{#1@totc}%
  \fi%
}
%    \end{macrocode}
%    \end{macro}
%%
%    \begin{macro}{\totvalue}
%%   Returns the numeric total value of a registered total counter that is
%%   passed as argument. Note that if the counter's value is not yet computed
%%   (at the first time \LaTeX\ runs on the document) this macro returns |-1|.
%    \begin{macrocode}
\newcommand\totvalue[1]{\value{#1@totc}}
%    \end{macrocode}
%    \end{macro}
%%
%    \begin{macro}{\usetotcountfile}
%%   Inputs an auxiliary file that should contain total-counter
%%   definitions. It outputs a warning message in the terminal if the file
%%   doesn't exist. This command should be used \emph{instead} of registering a
%%   counter.
%    \begin{macrocode}
\newcommand\usetotcountfile[1]{%
  \InputIfFileExists{#1}{%
    \message{TotCount inputs file '#1'}%
  }{%
    \message{TotCount Warning: File '#1' does not exist!}%
    \message{\space\space\space\space\space\space\space\space\space%
             \space\space\space\space\space\space\space\space\space%
             Ignoring \string\usetotcountfile{#1}.}%
  }%
}
%</sty>
%    \end{macrocode}
%    \end{macro}
%
% ^^A**********************************************************************
%
% \section{\label{sec:example}Simple Example}
%
% The following is a simple example of how to use the functionality of the
% \xpackage{TotCount} package. This example can be found in the file
% |totcount-ex.tex|, after running \LaTeX\ on the |totcount.ins| file (see
% Section~\ref{sec:installation}).
%
%    \begin{macrocode}
%<*ex>
\ProvidesFile{totcount-ex.tex}
      [\totcfiledate \space v\totcfileversion \space%
       test file for the TotCount package]
\documentclass{article}
%    \end{macrocode}
%%
%% Import the package
%    \begin{macrocode}
\RequirePackage{totcount}
%    \end{macrocode}
%%
%%  Comment out the following line to use the previously stored total counts in
%%  'mycount.aux' (make sure it exists!) instead of computing them again:
%    \begin{macrocode}
\def\computemycount{} 
\expandafter\ifx\csname computemycount\endcsname\relax
  \usetotcountfile{mycount.aux}
  \newcounter{mycount} % so that increments of this counter don't break
\else
  \newtotcounter[auxfile=mycount.aux]{mycount}
\fi
%    \end{macrocode}
%%
%% Register the section and page counters as a total counters:
%    \begin{macrocode}
\regtotcounter{section}
\regtotcounter{page}

\begin{document}
%    \end{macrocode}
%%
%% A total counter can be declared/registered anywhere:
%    \begin{macrocode}
\newtotcounter{yourcount}

\section{Total Counts}
%    \end{macrocode}
%%
%% Increment counters as usual:
%    \begin{macrocode}
\addtocounter{mycount}{5}
\addtocounter{yourcount}{5}
%    \end{macrocode}
%%
%% Print the value of a total counter by using |\total|:
%    \begin{macrocode}
The total value of the counter {\tt mycount} is \total{mycount} (should
be 10), and the total value of the counter {\tt yourcount} is
\total{yourcount} (should be 17).

%    \end{macrocode}
%%
%% Get the numeric value of a total counter by using |\totvalue|:
%    \begin{macrocode}
This document has \total{section}
\ifnum\totvalue{section}=1 section \else sections \fi
and \total{page}
\ifnum\totvalue{page}=1 page. \else pages. \fi

%    \end{macrocode}
%%
%% The effect of incrementing the counters will be visible the second
%% time LaTeX runs:
%    \begin{macrocode}
\addtocounter{yourcount}{5}
\addtocounter{mycount}{5}
\addtocounter{yourcount}{7}

\section{A Section}
\section{Another Section}
\section{Yet Another Section}

\end{document}
%</ex>
%    \end{macrocode}
% \Finale
%
% ^^A**********************************************************************
%
% \section{Acknowledgments}
%
% Many thanks to Christoforos Moutafis for pointing me to the right direction
% when I first started looking for a way to display the maximum value of
% counters. Thanks also to everyone who submitted bug reports.
%
% \begin{thebibliography}{H\,1}
%   \raggedright
%     \bibitem[1]{keyval}
%        David Carlisle,
%        \newblock \emph{The \xpackage{keyval} package},
%        \newblock \LaTeXe{} package.
%        \newblock \CTAN{macros/latex/required/graphics/keyval.dtx}
%     \bibitem[2]{lastpage}
%        Jeff Goldberg,
%        \newblock \emph{The \xpackage{lastpage} package},
%        \newblock \LaTeXe{} package.
%        \newblock \CTAN{macros/latex/contrib/lastpage/}
%     \bibitem[3]{latexcompanion}
%        Michel Goossens, Frank Mittelbach and Alexander Samarin,
%        \newblock \emph{The \LaTeX\ Companion}, 
%        \newblock Addison-Wesley, 1994.
%     \bibitem[4]{totpages} 
%        Wilhelm M\"uller,
%        \newblock \emph{The \xpackage{totpages} package},
%        \newblock \LaTeXe{} package.
%        \newblock \CTAN{macros/latex/contrib/totpages/}
% \end{thebibliography}
%
\endinput