summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/assoccnt/assoccnt.sty
blob: b6406ff56a42df1a7f242e5997e9d36c9131fa34 (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
\NeedsTeXFormat{LaTeX2e}%
\ProvidesPackage{assoccnt}[2014/08/13 v0.2 -- 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]%



\let\@@assoccnt@standardstepcounter\stepcounter%

\let\@@assoccnt@standardrefstepcounter\refstepcounter%


% Just for a quick suffix
\newcommand{\@@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\@@associatedcounterslistsuffix
}%


\newcommand{\@@assoccnt@@addassociatedcounter}[2]{%
  \ifcsdef{#1}{%
    \ifinlistcs{#2}{#1}{%
      % Do nothing, since counter is already in the list
    }{%
      \listcsadd{#1}{#2}%
    }%
  }{%
    % Nothing in here
  }%
}%




% 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}[2]{%
% #1 --> driver counter 
% #2 --> CSV list of other counters, that should be stepped, if the driver counter is stepped
  \ifcsdef{\@@assoccnt@@generatelistname{#1}}{%
    %  % Nothing to be done --> List already exists
    \GenericWarning{}{Warning: List of associated counters for counter #1 already exists}%
  }{%
    \csgdef{\@@assoccnt@@generatelistname{#1}}{}%   Define some global list 
  }%
  % Now add the counter names from #2 to the list 
  % Note: Currently, it is not checked whether a counter is already added!
  \forcsvlist{\@@assoccnt@@addassociatedcounter{\@@assoccnt@@generatelistname{#1}}}{#2}%
  % Now remove an accidental self-association
  \RemoveAssociatedCounter{#1}{#1}% 
}%



%%% 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\@@mytemplist{}%
    \renewcommand*{\do}[1]{%
      \ifstrequal{##1}{#2}{
        % Later one some success routine etc. ???
      }{%
        \listgadd{\@@mytemplist}{##1}%
      }%
    }%
    \dolistcsloop{#1}%
    \csxdef{#1}{\@@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}}%
}%


%% Test if a counter is an associated counter of driver counter 
\newcommand{\IfIsAssociatedCounter}[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%
  }%
}%


%%% 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%