Debian Codegraph @

texmf-dist/tex/generic/pgf/math/pgfmathutil.code.tex

raw
1 % Copyright 2019 Mark Wibrow
2 %
3 % but subject to the LaTeX Project Public License
4 % (http://www.latex-project.org/lppl.txt)
5 %
6 % and the GNU Public License
7 % (http://www.gnu.org/licenses/gpl.txt)
8 %
9
10 % This file defines utilities common to the \pgfmath files.
11 %
12 % Version 1.414213 29/9/2007
13
14 \def\pgfmathincluded{}
15
16 % Helper macros for \pgfmath@ifregister
17 %
18 \def\pgfmath@grabv#1#2#3#4#5#6#7\pgfmath@grabv{#2#3#4#5\ifx#6!!\fi}
19 \def\pgfmath@ifskip@extract#1{\expandafter\pgfmath@grabv\meaning#1!!!!!!\pgfmath@grabv}
20 \def\pgfmath@iftoks@extract#1{\expandafter\pgfmath@grabv\meaning#1!!!!!!\pgfmath@grabv}
21
22 \def\pgfmath@grabvi#1#2#3#4#5#6#7#8\pgfmath@grabvi{#2#3#4#5#6\ifx#7!!\fi}
23 \def\pgfmath@ifdimen@extract#1{\expandafter\pgfmath@grabvi\meaning#1!!!!!!\pgfmath@grabvi}
24 \def\pgfmath@ifcount@extract#1{\expandafter\pgfmath@grabvi\meaning#1!!!!!!\pgfmath@grabvi}
25
26 \def\pgfmath@grabvii#1#2#3#4#5#6#7#8#9\pgfmath@grabvii{#2#3#4#5#6#7\ifx#8!!\fi}
27 \def\pgfmath@ifmuskip@extract#1{\expandafter\pgfmath@grabvii\meaning#1!!!!!!\pgfmath@grabvii}
28
29 % \pgfmath@ifdimen@unguarded
30 %
31 % Check if #2 is a register of type #1.
32 % Credits to Frank: https://tex.stackexchange.com/a/56905
33 %
34 % #1 register type (count, dimen, skip, muskip, toks)
35 % #2 single token
36 %
37 \def\pgfmath@ifregister@unguarded#1#2{%
38 \expandafter\ifx\csname\csname pgfmath@if#1@extract\endcsname{#2}\expandafter\endcsname\csname #1\endcsname
39 \expandafter\pgfutil@firstoftwo
40 \else
41 \expandafter\pgfutil@secondoftwo
42 \fi}
43
44 % \pgfmath@ifregister
45 %
46 % Checks if a token is a a certain type of register
47 %
48 % #1 register type (count, dimen, skip, muskip, toks)
49 % #2 single token
50 %
51 \def\pgfmath@ifregister#1#2{%
52 \pgfutil@IfUndefined{pgfmath@if#1@extract}%
53 {\errmessage{Package PGF Math Error: Cannot test for #1 register}}%
54 {\pgfutil@IfUndefined{\csname pgfmath@if#1@extract\endcsname{#2}}%
55 {\pgfutil@secondoftwo}}%
56 {\pgfmath@ifregister@unguarded{#1}{#2}}}
57
58 % \pgfmath@ensureregister
59 %
60 % Ensure a control sequence exists as a TeX count/dimen register.
61 %
62 % #1 - count or dimen.
63 % #2 - a control sequence.
64 %
65 \def\pgfmath@ensureregister#1#2{%
66 \pgfmath@ifregister{#1}{#2}%
67 {}%
68 {\csname new#1\endcsname#2}%
69 }
70
71 % Check registers exist. If not, create them.
72 %
73 \pgfmath@ensureregister{dimen}{\pgf@x}
74 \pgfmath@ensureregister{dimen}{\pgf@xa}
75 \pgfmath@ensureregister{dimen}{\pgf@xb}
76 \pgfmath@ensureregister{dimen}{\pgf@xc}
77
78 \pgfmath@ensureregister{dimen}{\pgf@y}
79 \pgfmath@ensureregister{dimen}{\pgf@ya}
80 \pgfmath@ensureregister{dimen}{\pgf@yb}
81 \pgfmath@ensureregister{dimen}{\pgf@yc}
82
83 \pgfmath@ensureregister{dimen}{\pgfutil@tempdima}
84 \pgfmath@ensureregister{dimen}{\pgfutil@tempdimb}
85
86 \pgfmath@ensureregister{count}{\c@pgf@counta}
87 \pgfmath@ensureregister{count}{\c@pgf@countb}
88 \pgfmath@ensureregister{count}{\c@pgf@countc}
89 \pgfmath@ensureregister{count}{\c@pgf@countd}
90
91 \pgfmath@ensureregister{count}{\pgfutil@tempcnta}
92 \pgfmath@ensureregister{count}{\pgfutil@tempcntb}
93
94 % Alias for \pgfmath.
95 %
96 \let\pgfmath@x\pgf@x
97 \let\pgfmath@xa\pgf@xa
98 \let\pgfmath@xb\pgf@xb
99 \let\pgfmath@xc\pgf@xc
100
101 \let\pgfmath@y\pgf@y
102 \let\pgfmath@ya\pgf@ya
103 \let\pgfmath@yb\pgf@yb
104 \let\pgfmath@yc\pgf@yc
105
106 \let\pgfmath@tempdima\pgfutil@tempdima
107 \let\pgfmath@tempdimb\pgfutil@tempdimb
108
109 \let\c@pgfmath@counta\c@pgf@counta
110 \let\c@pgfmath@countb\c@pgf@countb
111 \let\c@pgfmath@countc\c@pgf@countc
112 \let\c@pgfmath@countd\c@pgf@countd
113
114 \let\pgfmath@tempcnta\pgfutil@tempcnta
115 \let\pgfmath@tempcntb\pgfutil@tempcntb
116
117 % Now check if pgfutil and pgf@sys stuff exists. If it does, fine.
118 % Just \let pgfmath versions. If not, define pgfmath versions (Arghhh).
119 %
120
121 % \...ifnextchar
122 %
123 \ifx\pgfutil@ifnextchar\pgfmath@undefined%
124 \long\def\pgfmath@ifnextchar#1#2#3{%
125 \let\pgfmath@reserved@d#1%
126 \def\pgfmath@reserved@a{#2}%
127 \def\pgfmath@reserved@b{#3}%
128 \futurelet\pgfmath@let@token\pgfmath@ifnch}
129 \def\pgfmath@ifnch{%
130 \ifx\pgfmath@let@token\pgfmath@sptoken%
131 \let\pgfmath@reserved@c\pgfmath@xifnch%
132 \else%
133 \ifx\pgfmath@let@token\pgfmath@reserved@d%
134 \let\pgfmath@reserved@c\pgfmath@reserved@a%
135 \else%
136 \let\pgfmath@reserved@c\pgfmath@reserved@b%
137 \fi%
138 \fi%
139 \pgfmath@reserved@c}
140 {%
141 \def\:{\global\let\pgfmath@sptoken= } \:
142 \def\:{\pgfmath@xifnch} \expandafter\gdef\: {\futurelet\pgfmath@let@token\pgfmath@ifnch}
143 }%
144 \else%
145 \let\pgfmath@ifnextchar\pgfutil@ifnextchar%
146 % Still need to define \pgfmath@sptoken (cannot \let).
147 {%
148 \def\:{\global\let\pgfmath@sptoken= } \:
149 }%
150 \fi%
151
152 % \...ifundefined
153 %
154 \ifx\pgfutil@ifundefined\pgfmath@undefined%
155 \long\def\pgfmath@ifundefined#1{%
156 \expandafter\ifx\csname#1\endcsname\relax%
157 \expandafter\pgfmath@firstoftwo%
158 \else%
159 \expandafter\pgfmath@secondoftwo%
160 \fi}%
161 \else%
162 \let\pgfmath@ifundefined\pgfutil@ifundefined%
163 \fi%
164
165 % \...selectfont
166 %
167 \ifx\pgfutil@selectfont\pgfmath@undefined%
168 \ifx\selectfont\pgfmath@undefined%
169 \let\pgfmath@selectfont\rm% Plain TeX and ConTeXt.
170 \else%
171 \let\pgfmath@selectfont\selectfont% LaTeX.
172 \fi%
173 \else%
174 \let\pgfmath@selectfont\pgfutil@selectfont%
175 \fi%
176
177 % New definition easier than fussing around with let.
178 %
179 \newif\ifpgfmath@in@
180 \def\pgfmath@in@#1#2{%
181 \def\pgfmath@in@@##1#1##2##3\pgfmath@in@@{%
182 \ifx\pgfmath@in@##2\pgfmath@in@false\else\pgfmath@in@true\fi}%
183 \pgfmath@in@@#2#1\pgfmath@in@\pgfmath@in@@}
184
185
186 % \...tonumber
187 %
188 \ifx\pgf@sys@tonumber\pgfmath@undefined%
189 {\catcode`\p=12\catcode`\t=12\gdef\Pgf@geT#1pt{#1}}
190 \def\pgfmath@tonumber#1{\expandafter\Pgf@geT\the#1}
191 \else%
192 \let\pgfmath@tonumber\pgf@sys@tonumber%
193 \fi%
194
195
196 % \PackageWarning and \PackageError
197 %
198 \ifx\PackageWarning\pgfmath@undefined
199 \def\pgfmath@PackageWarning#1#2{\immediate\write-1{Package #1: Warning! #2.}}%
200 \else%
201 \let\pgfmath@PackageWarning\PackageWarning%
202 \fi%
203 \ifx\PackageError\pgfmath@undefined
204 \def\pgfmath@PackageError#1#2#3{\errmessage{Package #1 Error: #2.}}%
205 \else%
206 \let\pgfmath@PackageError\PackageError%
207 \fi%
208
209 \def\pgfmath@error#1#2{%
210 \pgfmath@PackageError{PGF Math}{%
211 #1%
212 \pgfutil@ifundefined{pgfmath@expression}{}{ (in '\pgfmath@expression')}%
213 }{#2}%
214 }
215 \def\pgfmath@warning#1{%
216 \pgfmath@PackageWarning{PGF Math}{%
217 #1%
218 \pgfutil@ifundefined{pgfmath@expression}{}{ (in '\pgfmath@expression')}%
219 }%
220 }
221
222 % \pgfmath@pt
223 %
224 % Needed to test for 'pt' resulting from expansion using \the.
225 %
226 {\catcode`\p=12\catcode`\t=12\gdef\PgfmaTh@PT{pt}}
227 \edef\pgfmath@pt{\PgfmaTh@PT}%
228
229 % \pgfmath@empty
230 %
231 % A handy macro.
232 %
233 \def\pgfmath@empty{}
234
235 \def\pgfmath@firstoftwo#1#2{#1}
236 \def\pgfmath@secondoftwo#1#2{#2}
237
238 % \pgfmath@gobbletilpgfmath@
239 %
240 % Gobble stream until \pgfmath@ {which is undefined}.
241 %
242 \def\pgfmath@gobbletilpgfmath@#1\pgfmath@{}
243 \def\pgfmath@gobbleone#1{}%
244
245
246 \def\pgfmath@namedef#1{\expandafter\def\csname#1\endcsname}
247 \def\pgfmath@nameedef#1{\expandafter\edef\csname#1\endcsname}
248
249 % \pgfmathloop
250 %
251 % A version of the standard TeX and LaTeX
252 % \loop, with an additional macro \pgfmathcounter
253 % (which is *not* a TeX counter) which keeps
254 % track of the iterations.
255 %
256 \newif\ifpgfmathcontinueloop
257 \def\pgfmathloop#1\repeatpgfmathloop{%
258 \def\pgfmathcounter{1}%
259 \def\pgfmath@iterate{%
260 #1\relax%
261 {% Do this inside a group, just in case...
262 \c@pgfmath@counta\pgfmathcounter%
263 \advance\c@pgfmath@counta1\relax%
264 \xdef\pgfmathloop@temp{\the\c@pgfmath@counta}%
265 }%
266 \edef\pgfmathcounter{\pgfmathloop@temp}%
267 \expandafter\pgfmath@iterate\fi}%
268 \pgfmath@iterate\let\pgfmath@iterate\relax}
269 \let\repeatpgfmathloop\fi
270 \def\pgfmathbreakloop{\let\pgfmath@iterate\relax}%
271
272 % \pgfmath@returnone
273 %
274 % #1 - a dimension representing a number or dimension.
275 %
276 % Changed by TT: made faster and simpler since it is used
277 % *heavily*. #1 must be something that can be assigned to a
278 % dimension.
279 %
280 \def\pgfmath@returnone#1\endgroup{%
281 \pgfmath@x#1%
282 \edef\pgfmath@temp{\pgfmath@tonumber{\pgfmath@x}}%
283 \expandafter\endgroup\expandafter\def\expandafter\pgfmathresult\expandafter{\pgfmath@temp}%
284 }
285
286 \let\pgfmathreturn=\pgfmath@returnone
287
288 % \pgfmath@smuggleone
289 %
290 % Smuggle a macro outside a group.
291 %
292 % Changed by TT: Speedup by insisting, that smuggleone is directly
293 % followed by \endgroup
294 %
295 \def\pgfmath@smuggleone#1\endgroup{%
296 \expandafter\endgroup\expandafter\def\expandafter#1\expandafter{#1}}
297
298 \let\pgfmathsmuggle=\pgfmath@smuggleone