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
|
\documentclass[12pt,paper=a4]{ltxdoc}
\usepackage[lmargin=2cm,rmargin=2cm]{geometry}
\usepackage{totcount}%
\usepackage{assoccnt}%
\usepackage{blindtext}%
\usepackage{xcolor}
\usepackage{imakeidx}%
\usepackage{tcolorbox}
\usepackage{datetime}%
\usepackage{scrpage2}%
\tcbuselibrary{listings}%
\tcbuselibrary{documentation}%
\usepackage{array}%
\usepackage{amsmath}%
\usepackage{amsthm}%
\newcommand{\MarkupPackageName}[2][lightgray]{%
\colorbox{#1}{\textcolor{black}{#2}}%
}%
\newcommand{\MarkupMacroName}[2][cyan]{%
\colorbox{#1}{\(\backslash\)\textcolor{black}{#2}}%
}%
\newcommand{\MarkupCounterName}[2][yellow]{%
\colorbox{#1}{\textcolor{black}{\texttt{#2}}}%
}%
\def\packageversion{0.2}%
\makeindex
\newcommand{\PackageDocName}{assoccnt.sty}%
\begin{document}
\mmddyyyydate
\setlength{\parindent}{0pt}
\newtotcounter{totalpages}%
\newtotcounter{anothertotalpages}%
\newtotcounter{totalproofs}%
\newtotcounter{totalsections}%
\DeclareAssociatedCounters{section}{totalsections}%
\DeclareAssociatedCounters{proof}{totalproofs}
\DeclareAssociatedCounters{page}{anothertotalpages,totalpages}
\thispagestyle{empty}%
\begin{center}
\begin{tcolorbox}[colback=yellow!30!white,width=0.8\textwidth]
\large \bfseries%
\begin{center}%
\begin{tabular}{c}%
\textsc{\PackageDocName} \tabularnewline
\tabularnewline
Associate counters stepping simultaneously \tabularnewline
\tabularnewline
Version \packageversion \tabularnewline
\tabularnewline
\today \tabularnewline
\tabularnewline
Author: Christian Hupfer\footnote{\makeatletter christian.hupfer@siebenfelsen.de}
\tabularnewline
\end{tabular}
\end{center}
\end{tcolorbox}
\end{center}
\tableofcontents
\clearpage
\pagestyle{scrheadings}%
\setheadsepline{2pt}[\color{blue}]
\section{Introduction}
The aim of this package is to provide some additional support for example for a package like \MarkupPackageName{totcount}.
For example, the total number of pages in a document could be achieved by using
\begin{dispExample}%
\regtotcounter{page}
...
The number of pages in the document is \number\totvalue{page} page(s).
\end{dispExample}%
This will work, as long there is no reset of the page counter, as it might happen in the case of \cs{setcounter} or \cs{pagenumbering} being applied in the document. The result is a false page counter total value.
This package provides associate counters, i.e. counters that are increased simultaneously with a driver counter and are not influenced by a a resetting of the driver counter, as long as not being added to the reset list by definition of the counter or explicitly by \cs{@addtoreset}.
This package defines some macros to handle associated counters. The only interception to the standard behaviour is within the redefined commands \cs{stepcounter} and \cs{refstepcounter}. The usual commands still work, as there is code added to their definition.
Internally, the associated counters are stored in one list per counter -- it is not recommended to operate on those lists directly.
\section{Documentation of Macros}
\tcbset{color command={blue}}
All macros have the general rule, that the driver counter is specified as 1st argument to the macro.
\begin{docCommand}{DeclareAssociatedCounters}{\marg{driver counter}\marg{associated counters list}}
This command is the main macro of the package. It declares the counter names being specified in comma - separated - list (CSV) which should be stepped simultaneously when the driver counter is increased by \cs{stepcounter}.
Note, that this command should be used as early as possible, i.e. in the preamble of the document, since the driven counters are not increased as long as they are not associated to the driver counter. On the hand, it is possible, to control the starting point of the association at any position in the body of the document, when the association should start later on.
\begin{dispExample}
%%%% The association of anothertotalpages in this example just takes place here, so the stepping of the counter will start from here and providing a 'wrong' value.
%%%%
\DeclareAssociatedCounters{page}{totalpages,anothertotalpages}
This document has \number\totvalue{totalpages} (note: \number\totvalue{anothertotalpages}) pages.
\end{dispExample}
\begin{itemize}
\item Current version state:
\begin{itemize}
\item No checking whether the 1st and 2nd arguments hold counter names is applied.
\item No check is done whether two (or more) associated counters are mutually associated.
\end{itemize}
\item A self-association of the driver counter to itself is ignored internally as this would lead to inconsistent counter values.
\item The order of the specification of associated counters in the 2nd arguments is of no importance.
\item Specifing an associated counter name multiple times has no effect, only the first occurence of the name will be used.
\end{itemize}
\end{docCommand}
\begin{docCommand}{RemoveAssociatedCounter}{\marg{driver counter}\marg{associated counter}}
This command removes a counter from the existing list for a driver counter, i.e. the counter will not be increased any longer by \cs{stepcounter}. It can be increased however manually, of course.
\end{docCommand}
\begin{dispExample}
\RemoveAssociatedCounter{page}{anothertotalpages}
This document has \number\totvalue{totalpages} (beware: \number\totvalue{anothertotalpages}) pages.
\end{dispExample}
\begin{docCommand}{RemoveAssociatedCounters}{\marg{driver counter}\marg{list of associated counters}}
This command removes the comma-separated-value list of counters from the existing list for a driver counter, i.e. the counters will not be increased any longer by \cs{stepcounter}. They can be increased however manually, of course.
Take care not to confuse the commands \refCom{RemoveAssociatedCounters}
and{}\linebreak \refCom{RemoveAssociatedCounter}
\end{docCommand}
\begin{docCommand}{ClearAssociatedCounters}{\marg{driver counter}}
This command clears the internal list for all counters associated to the \marg{driver counter}. The counters will not be increased automatically.
\end{docCommand}
\begin{docCommand}{IfIsAssociatedCounter}{\marg{driver counter}\marg{associated counter}\marg{True branch}\marg{False branch}}
This macro checks, whether a counter is associated to the driver counter and expands the corresponding branch. If the internal driver counter list does not exist, the false branch will be used, since this also means, that the possibly associated counter is not associated at all.
\begin{dispExample}
% Remove associated counter first for demonstration purposes
\RemoveAssociatedCounter{page}{anothertotalpages}
\IfIsAssociatedCounter{page}{totalpages}{Yes, totalpages is associated}{No, totalpages is not associated}
\IfIsAssociatedCounter{page}{anothertotalpages}{Yes, anothertotalpages is associated}{No, anotherpages is not associated}
\end{dispExample}
\end{docCommand}
\clearpage
\section{To do list}
\begin{itemize}
\item Check the existence of driver and associated counters
\item Improve documentation
\end{itemize}
\clearpage
\section{Acknowledgments}
I would like to express my gratitudes to the developpers of fine \LaTeX{} packages and of course
to the users at tex.stackexchange.com, especially to
\begin{itemize}
\item Enrico Gregorio
\item Joseph Wright
\item David Carlisle
\item Werner
\end{itemize}
for their invaluable help on many questions on macros.
\vspace{2\baselineskip}
A special gratitude goes to Prof. Dr. Dr. Thomas Sturm for providing the wonderful \MarkupPackageName{tcolorbox} package which was used to
write this documentation.
\clearpage
\appendix
\section{Example: Total number of sections}
In this example, all sections of this document are counted, i.e. the current one as well as all following ones.
\begin{dispExample}
\DeclareAssociatedCounters{section}{totalsections}%
This document has \number\totvalue{totalsections} section(s)%
\end{dispExample}
\section{Example: Total number of subsections with suspension}
In this example, the subsections of this document are counted but later on, the associatedcounter is removed from the list, so it is frozen.
\begin{dispExample}
\newtotcounter{totalsubsections}%
\DeclareAssociatedCounters{subsection}{totalsubsections}%
\subsection{First dummy subsection}
SubSection counter: \thesubsection~-- \number\totvalue{totalsubsections}
\subsection{Second dummy subsection}
SubSection counter: \thesubsection~-- \number\totvalue{totalsubsections}
\RemoveAssociatedCounter{subsection}{totalsubsections}
\subsection{Third dummy subsection after removing the associated counter}
SubSection counter: \thesubsection~-- \number\totvalue{totalsubsections}
\end{dispExample}
\section{Example: Total number of proofs}
\begin{dispExample}
% \usepackage{amsthm} is needed
\newcounter{proof}%
\newtheorem{Proof}[proof]{Proof}%
\DeclareAssociatedCounters{proof}{totalproofs}
\begin{Proof}
It's trivial.
\end{Proof}
\begin{Proof}[On Brontosaurs]
See excavations!
\end{Proof}
%%% Manipulate the counter
\setcounter{proof}{17}
\begin{Proof}[Yet another proof]
\begin{align*}
E &= m c^2
\end{align*}
\end{Proof}
This document has \number\totvalue{totalproofs} proofs %
\end{dispExample}
\clearpage
%%%% Index of commands
\printindex
\end{document}
|