blob: 76fb5559846631bfccf68cb55f3ea532d57d09d4 (
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
|
%%
%% This is file `totcount.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% totcount.dtx (with options: `sty')
%%
%% The TotCount package, version 1.2, 2011/01/25
%%
%% Copyright (c) [2009] Vasileios Koutavas (Vasileios.Koutavas@scss.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
%%
\def\totcfileversion{1.2}
\def\totcfiledate{2011/01/25}
\def\totcdocdate{25 Jan 2011}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{totcount}
[\totcfiledate \space v\totcfileversion \space package for getting%
the total value of LaTeX counters]
%%
%% Import of the \xpackage{keyval} package \cite{keyval}:
\RequirePackage{keyval}
%%
%% 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.
\def\newtotcounter{%
\@ifnextchar[\newtotcounter@newaux\newtotcounter@mainaux}
%%
%% 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|.
\def\newtotcounter@newaux[#1]#2{%
\newcounter{#2}%
\regtotcounter[#1]{#2}%
}
%%
%% 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|.
\def\newtotcounter@mainaux#1{%
\newcounter{#1}%
%% Register the counter:
\regtotcounter{#1}%
}
%%
%% 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.
\def\regtotcounter{%
\@ifnextchar[\regtotcounter@newaux\regtotcounter@mainaux}
%%
%% The following is the definition of the |auxfile| key for specifying an
%% alternative auxiliary file when calling the macro |\regtotcounter| (see
%% \cite{keyval}).
\define@key{totcounter}{auxfile}{\def\this@auxfile{#1}}
%%
%% 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}%
%% Try to load the contents of the file:
\InputIfFileExists{\this@auxfile}{}{}%
%% Make sure that the auxiliary file is open; \LaTeX\ will close it at the end:
\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%
%% Create a new counter holding the total number of the actual counter:
\expandafter\ifx\csname c@#2@totc\endcsname\relax%
\newcounter{#2@totc}%
\setcounter{#2@totc}{-1}%
\fi%
%% At the end of the document write code in the auxiliary file to update the
%% total counter with the value of the actual counter:
\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}}%
}%
}%
}
%%
%% 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.
\def\regtotcounter@mainaux#1{%
%% Create a new counter holding the total number of the actual counter:
\expandafter\ifx\csname c@#1@totc\endcsname\relax%
\newcounter{#1@totc}%
\setcounter{#1@totc}{-1}%
\fi%
%% At the end of the document write code in the auxiliary file to update the
%% total counter with the value of the actual counter:
\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}}%
}%
}%
}
%%
%% 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 $??$.
\newcommand\total[1]{%
\def\tmp@val{\value{#1@totc}}%
\ifnum\tmp@val=-1%
\PackageWarning{totcount}{Rerun to get correct total counts}%
$??$%
\else%
\number\value{#1@totc}%
\fi%
}
%%
%% 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|.
\newcommand\totvalue[1]{\value{#1@totc}}
%%
%% 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.
\newcommand\usetotcountfile[1]{%
\InputIfFileExists{#1}{%
\message{TotCount inputs file '#1'}%
}{%
\PackageWarning{totcount}{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}.}%
}%
}
\endinput
%%
%% End of file `totcount.sty'.
|