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
|
% Copyright 2019 by Mark Wibrow
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.
% Declare a function for the parser.
%
% #1 - the name of the function.
% #2 - arity (or ... for variable arguments).
% #3 - the code for the function.
%
% Use \pgfmathdeclarefunction* to force a redefinition if the function exists already.
\def\pgfmathdeclarefunction{\pgfmath@ifnextchar*\pgfmathdeclarefunction@force\pgfmathdeclarefunction@check}
\def\pgfmathdeclarefunction@check#1{%
\expandafter\ifx\csname pgfmath@function@#1\endcsname\relax\else%
\pgfmath@error{The function `#1' already exists}{}%
\fi%
\pgfmath@declarefunction{#1}%
}
\def\pgfmathdeclarefunction@force*{\pgfmath@declarefunction}%
\def\pgfmathredeclarefunction#1#2{%
\expandafter\ifx\csname pgfmath@function@#1\endcsname\relax%
\pgfmath@error{The function `#1' does not exist}{}%
\else%
% was: \pgfmath@namedef{pgfmath#1@}{#2}% TT
\edef\pgf@marshal{\noexpand\pgfmathdeclarefunction*{#1}{\csname pgfmath@operation@#1@arity\endcsname}}%
\pgf@marshal{#2}%
\fi%
}
\def\pgfmath@declarefunction#1#2{%
\pgfmath@namedef{pgfmath@function@#1}{#1}%
\pgfmath@namedef{pgfmath@operation@#1@type}{function}%
\pgfmath@namedef{pgfmath@operation@#1@arity}{#2}%
\pgfmath@namedef{pgfmath@operation@#1@infix}{#1}%
\pgfmath@namedef{pgfmath@operation@#1@precedence}{1000}%
\expandafter\ifx\csname pgfmath@operation@#1@arity\endcsname\pgfmath@dots%
\pgfmath@namedef{pgfmath@operation@#1@arity}{1}%
\else%
\ifnum\csname pgfmath@operation@#1@arity\endcsname>9\relax%
\pgfmath@namedef{pgfmath@operation@#1@arity}{1}%
\fi%
\fi%
\edef\pgfmath@marshal{%
\noexpand\pgfmath@@declarefunction%
{\expandafter\noexpand\csname pgfmath#1\endcsname}%
{\expandafter\noexpand\csname pgfmath#1@\endcsname}%
{\csname pgfmath@operation@#1@arity\endcsname}%
}%
\pgfkeysvalueof{/pgf/declare function/execute at begin function}%
\pgfmath@marshal%
}
% #1 - \pgfmath<name>.
% #2 - \pgfmath<name>@.
% #3 - arity.
% #4 - code for function <name>.
%
\def\pgfmath@@declarefunction#1#2#3#4{% Function of arity 0 - 9.
\pgfkeysvalueof{/pgf/declare function/execute at end function}%
\ifcase#3\relax%
\def#1{#2}%
\def\pgfmath@@head{\def#2}%
\or
\def#1##1{\pgfmathparse{##1}\expandafter#2\expandafter{\pgfmathresult}}%
\def\pgfmath@@head{\def#2####1}%
\else%
\pgfmath@toks={}%
\pgfmathloop%
\ifnum\pgfmathcounter>#3\relax%
\else%
\expandafter\pgfmath@addto@toks\expandafter{\pgfmath@char@hash}%
\expandafter\pgfmath@addto@toks\expandafter{\pgfmathcounter}%
\repeatpgfmathloop%
\edef\pgfmath@head{\noexpand\def\noexpand#1\the\pgfmath@toks}%
\edef\pgfmath@@head{\noexpand\def\noexpand#2\the\pgfmath@toks}%
\pgfmath@toks={}%
\def\pgfmath@arguments{}%
\pgfmathloop%
\ifnum\pgfmathcounter>#3\relax%
\else%
\pgfmath@addto@toks{\pgfmathparse}%
\expandafter\expandafter\expandafter\pgfmath@addto@toks%
\expandafter\expandafter\expandafter{\expandafter\expandafter\expandafter%
{\expandafter\pgfmath@char@hash\pgfmathcounter}}%
\pgfmath@addto@toks{\let}%
\expandafter\pgfmath@addto@toks\expandafter{%
\csname pgfmath@argument@\pgfmathcounter\endcsname=\pgfmathresult}%
\expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter%
\pgfmath@arguments\expandafter\expandafter\expandafter{\expandafter%
\pgfmath@arguments\expandafter{\csname pgfmath@argument@\pgfmathcounter\endcsname}}%
\repeatpgfmathloop%
\expandafter\pgfmath@addto@toks\expandafter{\expandafter#2\pgfmath@arguments}%
\edef\pgfmath@body{{\the\pgfmath@toks}}%
\expandafter\pgfmath@head\pgfmath@body%
\fi%
\pgfmath@@head{#4}%
}
\def\pgfmath@addto@toks#1{%
\edef\pgfmath@toks@expanded{\the\pgfmath@toks}%
\expandafter\pgfmath@toks\expandafter{\pgfmath@toks@expanded#1}}
% Key for declaring local functions.
%
\pgfkeys{%
/pgf/declare function/.code={%
\pgfmath@local@functions#1@=@;%
},
/pgf/declare function/execute at begin function/.initial={},
/pgf/declare function/execute at end function/.initial={},
/pgf/declare function/ignore spaces/.is choice,
/pgf/declare function/ignore spaces/.default=true,
/pgf/declare function/ignore spaces/true/.style={%
/pgf/declare function/execute at begin function={%
\begingroup
\catcode`\^^I=9\relax
\catcode`\ =9\relax
\catcode`\~=10\relax
\endlinechar=`\ \relax
},
/pgf/declare function/execute at end function={%
\endgroup
},
},
/pgf/declare function/ignore spaces/false/.style={%
/pgf/declare function/execute at begin function={},
/pgf/declare function/execute at end function={},
},
}
\def\pgfmath@local@gobbleone#1{}
\def\pgfmath@local@at{@}%
\def\pgfmath@local@functions{%
\pgfutil@ifnextchar x{\pgfmath@local@@functions}{\pgfmath@local@@functions}%
}
\def\pgfmath@local@@functions#1=#2;{%
\def\pgfmath@local@temp{#1}%
\ifx\pgfmath@local@temp\pgfmath@local@at%
\let\pgfmath@local@next=\relax%
\else%
\pgfmath@local@function#1=#2;%
\let\pgfmath@local@next=\pgfmath@local@functions%
\fi%
\pgfmath@local@next%
}
\def\pgfmath@local@function{%
\let\pgfmath@local@name=\pgfutil@empty%
\let\pgfmath@local@args=\pgfutil@empty%
\let\pgfmath@local@body=\pgfutil@empty%
\pgfmath@local@@function}
\def\pgfmath@local@@function#1{%
\if#1=%
\let\pgfmath@local@next=\pgfmath@local@function@body%
\else%
\if#1(%
\let\pgfmath@local@next=\pgfmath@local@function@args%
\else%
\expandafter\def\expandafter\pgfmath@local@name\expandafter{\pgfmath@local@name#1}%
\let\pgfmath@local@next=\pgfmath@local@@function%
\fi%
\fi%
\pgfmath@local@next%
}
\def\pgfmath@local@function@args#1){%
\def\pgfmath@local@args{#1}%
\pgfmath@local@@function}
\def\pgfmath@local@function@body#1;{%
\def\pgfmath@local@body{#1}%
\begingroup%
\c@pgf@counta=0\relax%
\ifx\pgfmath@local@args\pgfmath@empty%
\expandafter\pgfmath@toks\expandafter=\expandafter{\pgfmath@local@body}%
\else%
\pgfmath@toks={}%
\expandafter\pgfmath@local@function@@body\pgfmath@local@args,,%
\fi%
\xdef\pgfmath@local@temp{%
\noexpand\pgfmathnotifynewdeclarefunction{\pgfmath@local@name}{\the\c@pgf@counta}%
{\the\pgfmath@toks}%
}%
\endgroup%
\pgfmath@local@temp%
}
% #1: the name
% #2: the number of arguments <n>
% #3: a math expression containing '#1', '#2', ... ,'#'<n>
% This callback is overwritten by pgflibraryluamath.
\def\pgfmathnotifynewdeclarefunction#1#2#3{%
\pgfmathdeclarefunction{#1}{#2}{\pgfmathparse{#3}}%
}%
\def\pgfmath@local@function@@body#1,{%
\def\pgfmath@local@test{#1}%
\ifx\pgfmath@local@test\pgfutil@empty%
\let\pgfmath@local@next=\relax%
\else%
\edef\pgfmath@local@var@string{@\expandafter\pgfmath@local@gobbleone\string#1}%
\advance\c@pgf@counta by1\relax%
\expandafter\expandafter\expandafter\pgfmath@toks\expandafter\expandafter\expandafter=%
\expandafter\expandafter\expandafter{\expandafter\pgfmath@char@hash\the\c@pgf@counta}%
\edef#1{\the\pgfmath@toks}%
\pgfmath@toks={}%
\expandafter\pgfmath@local@function@@@body\pgfmath@local@body @%
\edef\pgfmath@local@body{\the\pgfmath@toks}%
\let\pgfmath@local@next=\pgfmath@local@function@@body%
\fi%
\pgfmath@local@next%
}
\def\pgfmath@local@function@@@body{%
\pgfutil@ifnextchar\bgroup{\pgfmath@local@function@@@body@bgroup}%
{\pgfmath@local@function@@@body@nobgroup}}%
\def\pgfmath@local@function@@@body@bgroup#1{%
\begingroup%
\pgfmath@toks={}%
\pgfmath@local@function@@@body#1 @%
\xdef\pgfmath@local@temp{{\the\pgfmath@toks}}%
\endgroup%
\expandafter\pgfmath@addto@toks\expandafter{\pgfmath@local@temp}%
\pgfmath@local@function@@@body%
}
\def\pgfmath@local@function@@@body@nobgroup#1{%
\ifx#1@%
\let\pgfmath@local@next=\relax%
\else%
\let\pgfmath@local@next=\pgfmath@local@function@@@body%
\edef\pgfmath@local@var@@string{@\expandafter\pgfmath@local@gobbleone\string#1}%
\ifx\pgfmath@local@var@@string\pgfmath@local@var@string%
\expandafter\pgfmath@addto@toks\expandafter{#1}%
\else%
\edef\pgfmath@local@temp{\the\pgfmath@toks}%
\pgfmath@toks={#1}%
\edef\pgfmath@local@@temp{\the\pgfmath@toks}%
\expandafter\pgfmath@toks\expandafter=\expandafter{\pgfmath@local@temp}%
\expandafter\pgfmath@addto@toks\expandafter{\pgfmath@local@@temp}%
\fi%
\fi%
\pgfmath@local@next%
}
% Allows to define 'x' as some \TeX command '#2'.
\def\pgfmathdeclarepseudoconstant#1#2{%
\pgfmathdeclarefunction*{#1}{0}{#2}%
}%
% Redefines a pseudo constant '#1'.
\def\pgfmathredeclarepseudoconstant#1#2{%
\pgfmathredeclarefunction{#1}{#2}%
}%
\input pgfmathfunctions.basic.code.tex
\input pgfmathfunctions.trigonometric.code.tex
\input pgfmathfunctions.random.code.tex
\input pgfmathfunctions.comparison.code.tex
\input pgfmathfunctions.base.code.tex
\input pgfmathfunctions.round.code.tex
\input pgfmathfunctions.misc.code.tex
\input pgfmathfunctions.integerarithmetics.code.tex
% Local Variables:
% tab-width: 2
% End:
|