summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/assoccnt/assoccnt.sty
blob: 08697e37332dbafc6b08223db66a46429691d834 (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
\NeedsTeXFormat{LaTeX2e}%
\ProvidesPackage{assoccnt}[2014/08/21 v0.3 -- Associate counters stepping]%
%%%
%% License: LaTeX Project Public License
%% Copyright (2014) Dr. Christian Hupfer 
%% Author: Christian Hupfer christian.hupfer@siebenfelsen.de
%%
%%%%
%%% Some options later on
%%%%

\RequirePackage{etoolbox}[2011/01/03 2.2]%
\RequirePackage{xkeyval}[2012/10/14 v2.6b]%


\def\@@assoccnt@@keys{@@assoccnt@@keyfamily}%



\newcounter{@@assoccnt@@internalcounter}%

\let\@@assoccnt@@standardstepcounter\stepcounter%
\let\@@assoccnt@@standardrefstepcounter\refstepcounter%


% Just for a quick suffix
\newcommand{\@@assoccnt@@associatedcounterslistsuffix}{%
AssociatedCountersList%
}%



%%% Internal macro to generate the name of the list of associated counters
\newcommand{\@@assoccnt@@generatelistname}[1]{%
% #1 Name of the driver counter
@@#1@@\@@assoccnt@@associatedcounterslistsuffix
}%

\newcommand{\@@assoccnt@@drivercounterlist}{%
@@assoccnt@@DriverCountersList%
}%

%%
%% Adds a driver counter to an internal administration list
\newcommand{\AddDriverCounter}[2][]{%
% #1 -> options
% #2 -> driver counter name
  \setkeys{\@@assoccnt@@keys}{#1}%
  \ifcsdef{\@@assoccnt@@drivercounterlist}{%
  }{%
    \csgdef{\@@assoccnt@@drivercounterlist}{}%
  }%
  \listcsgadd{\@@assoccnt@@drivercounterlist}{#2}%
}%


%%
%% Removes the complete list of driver counters
\newcommand{\ClearDriverCountersList}{%
  \ifcsdef{\@@assoccnt@@drivercounterlist}%
  {%
    % Clear the possible driver counter entry in the list, the trailing associated counters list 
    % is cleared as well
    \forlistcsloop{\ClearAssociatedCountersList}{\@@assoccnt@@drivercounterlist}%
  }{%
    % Nothing to be done
  }%
}%

\newcommand{\IsDriverCounter}[3]{%
  \ifcsdef{\@@assoccnt@@drivercounterlist}{%
    \ifinlistcs{#1}{\@@assoccnt@@drivercounterlist}{% True branch, it's in the list
      #2%
    }{% False branch, not in the list
      #3% 
    }%
  }{%  List does not exist at all --> expand false branch
    #3%
  }%
}% 

\newcommand{\@@assoccnt@@generateboundtocounterslist}[1]{%
#1@@boundtocounterslist%
}%


\newcommand{\@@assoccnt@@addassociatedcounter}[3][]{%
  \ifcsdef{\@@assoccnt@@generateboundtocounterslist{#3}}{%
    % The counter is already to some other counter, it should not be multiply associated!
  }{%
    \csgdef{\@@assoccnt@@generateboundtocounterslist{#3}}{}%
    \listcsgadd{\@@assoccnt@@generateboundtocounterslist{#3}}{#1}
    \ifcsdef{#2}{%
      \ifinlistcs{#3}{#2}{%
        % Do nothing, since counter is already in the list, prevent multiple list insertion
      }{%
        \listcsgadd{#2}{#3}%
      }%
    }{%
      % Nothing in here
    }%
  }%
}%



\newcommand{\AddAssociatedCounters}[3][]{%
% #1 --> options
% #2 --> driver counter 
% #3 --> CSV list of counters, that should be stepped, if the driver counter is stepped
\setkeys{\@@assoccnt@@keys}{#1}%
  \ifcsdef{\@@assoccnt@@generatelistname{#2}}{%
    %  % Nothing to be done --> List already exists
    \forcsvlist{\@@assoccnt@@addassociatedcounter[#2]{\@@assoccnt@@generatelistname{#2}}}{#3}%
    % Prevent accidental self - association
    \RemoveAssociatedCounter{#2}{#2}% 
  }{%
    \GenericError{}{Error: List for driver counter #2 does not exist -- Usage on line \the\inputlineno}{}{}
  }%
}%

% This command defines a list of counters, that should be stepped also if the driver counter
% is `\stepcounter`ed. 
% A self - association is not possible, as this would lead to inconsistent counting
\newcommand{\DeclareAssociatedCounters}[3][]{%
% #1 --> options
% #2 --> driver counter 
% #3 --> CSV list of other counters, that should be stepped, if the driver counter is stepped
\setkeys{\@@assoccnt@@keys}{#1}%
  \ifcsdef{\@@assoccnt@@generatelistname{#2}}{%
    %  % Nothing to be done --> List already exists
    \GenericWarning{}{Warning: List of associated counters for driver counter #2 already exists}%
  }{%
    \AddDriverCounter{#2}%
    \csgdef{\@@assoccnt@@generatelistname{#2}}{}%   Define some global list 
  }%
  % Now add the counter names from #3 to the list 
  % Note: Currently, it is not checked whether a counter is already added!
  \forcsvlist{\@@assoccnt@@addassociatedcounter[#2]{\@@assoccnt@@generatelistname{#2}}}{#3}%
  % Now remove an accidental self-association
  \RemoveAssociatedCounter{#2}{#2}% 
}%

%\@onlypreamble{\DeclareAssociatedCounters}%


%%



%%% A generic macro, that removes a list entry from the list by
%%% defining a temporary list and omitting the 
\newcommand{\@@assoccnt@@removefromlist}[2]{%
% #1 list name
% #2 element to be removed
  \ifcsdef{#1}{%
    \gdef\@@assoccnt@@mytemplist{}%
    \renewcommand*{\do}[1]{%
      \ifstrequal{##1}{#2}{
        % Later one some success routine etc. ???
      }{%
        \listgadd{\@@assoccnt@@mytemplist}{##1}%
      }%
    }%
    \dolistcsloop{#1}%
    \csxdef{#1}{\@@assoccnt@@mytemplist}%
  }{%
    % The list is not defined at all, can't remove something from something not existing...
  }%
}%



%% Remove a particular counter from the list 
\newcommand{\RemoveAssociatedCounter}[2]{%
% #1 arg: driver counter
% #2 arg: counter name to be removed
  \@@assoccnt@@removefromlist{\@@assoccnt@@generatelistname{#1}}{#2}%
}%

%% Remove a CSV list of counters from the list of associated counters 

\newcommand{\RemoveAssociatedCounters}[2]{%
% #1 arg: driver counter
% #2 arg: CSV list of counters to be removed
  \forcsvlist{\RemoveAssociatedCounter{#1}}{#2}%
}%

%% Remove all associated counters from the list 
\newcommand{\ClearAssociatedCountersList}[1]{%
% #1 arg: driver counter
  \csundef{\@@assoccnt@@generatelistname{#1}}%
  \@@assoccnt@@removefromlist{\@@assoccnt@@drivercounterlist}{#1}%
}%


%% Test if a counter is an associated counter of driver counter 
\newcommand{\IsAssociatedToCounter}[4]{%
% #1 arg: driver counter
% #2 arg: (possibly) associated counter
% #3 arg: Code for execution on true branch
% #4 arg: Code for execution on false branch
  \ifcsdef{\@@assoccnt@@generatelistname{#1}}{%
    \ifinlistcs{#2}{\@@assoccnt@@generatelistname{#1}}{%
      #3%
    }{%
      #4%
    }%
  }{% List does not exist, so it's not associated
    #4%
  }%
}%


\newcommand{\IsAssociatedCounter}[3]{%
  \ifcsdef{\@@assoccnt@@generateboundtocounterslist{#1}}{% true branch
    \setcounter{@@assoccnt@@internalcounter}{0}%
    \renewcommand*{\do}[1]{%
      \@@assoccnt@@standardstepcounter{@@assoccnt@@internalcounter}%
    }%
    \dolistcsloop{\@@assoccnt@@generateboundtocounterslist{#1}}%
    \ifnumgreater{\number\value{@@assoccnt@@internalcounter}}{0}{%
      #2%
    }{%
      #3%
    }%
  }{%
    #3% 
  }% End of \ifcsdef false branch
}%


\newcommand{\GetDriverCounter}[1]{%
  \setcounter{@@assoccnt@@internalcounter}{0}%
  \renewcommand*{\do}[1]{%
    ##1%
    \ifnumequal{\number\value{@@assoccnt@@internalcounter}}{1}{%
      \listbreak}{%
      \@@assoccnt@@standardstepcounter{@@assoccnt@@internalcounter}%
    }%
  }%
  \dolistcsloop{\@@assoccnt@@generateboundtocounterslist{#1}}%
}%

\newcommand{\@@assoccnt@@showassociatedcounterlist}[1]{%
  \textcolor{red}{\textbf{#1{ }}}%
}%

\newcommand{\@@assoccnt@@showdrivercounterlist}[1]{%
  \textcolor{blue}{\textbf{#1}}
  \ifcsdef{\@@assoccnt@@generatelistname{#1}}{%
    \forlistcsloop{\@@assoccnt@@showassociatedcounterlist}{\@@assoccnt@@generatelistname{#1}}%
  }{}%
  \par%
}%

\newcommand{\AssociationStatistics}[1][]{%
  \forlistcsloop{\@@assoccnt@@showdrivercounterlist}{\@@assoccnt@@drivercounterlist}%
}%



%%% The stepcounter wrapper for the standard stepcounter command
%%% This is just for convenience, if the command as to be improved/extended later on in future
\newcommand{\@@assoccnt@@stepcounter}[1]{%  
  \@@assoccnt@@standardstepcounter{#1}%
}%

%%% Not needed so far
\newcommand{\@@assoccnt@refstepcounter}[1]{%  
  \@@assoccnt@@standardrefstepcounter{#1}%
}%

%%%% Redefinition of the \stepcounter command 
\renewcommand{\stepcounter}[1]{%
  \@@assoccnt@@standardstepcounter{#1}%   Traditional behaviour, since this is expected!
  \ifcsdef{\@@assoccnt@@generatelistname{#1}}{%    Check first, whether the list exists at all, 
    \forlistcsloop{\@@assoccnt@@stepcounter}{\@@assoccnt@@generatelistname{#1}}%  March through the list
  }{% 
    %No list -> do nothing at all
  }%
}%





%%%% Redefinition of the \stepcounter command 
\renewcommand{\refstepcounter}[1]{%
  \@@assoccnt@@standardstepcounter{#1}%   Traditional behaviour, since this is expected!
  \protected@edef\@currentlabel%
  {\csname p@#1\endcsname\csname the#1\endcsname}%
  \ifcsdef{\@@assoccnt@@generatelistname{#1}}{%    Check first, whether the list exists at all, 
    \forlistcsloop{\@@assoccnt@@stepcounter}{\@@assoccnt@@generatelistname{#1}}%  March through the list
  }{% 
    %No list -> do nothing at all
  }%
}%


\endinput%