Debian Codegraph @

texmf-dist/tex/generic/pgf/utilities/pgfutil-common.tex

raw
1 % Copyright 2019 by Till Tantau
2 %
3 % This file may be distributed and/or modified
4 %
5 % 1. under the LaTeX Project Public License and/or
6 % 2. under the GNU Public License.
7 %
8 % See the file doc/generic/pgf/licenses/LICENSE for more details.
9
10
11 %
12 % This file provides utility commands that are used throughout pgf.
13 %
14 % For most commands, the definition of these commands is just given
15 % below. We cannot use the LaTeX definition of these commands since
16 % LaTeX may not be the current format and since LaTeX packages tend to
17 % redefine these commands.
18 %
19 % For some commands the actual definition of the format (like latex or
20 % context) is to be preferred over the generic definition below. In
21 % this case, the definition of the format is installed when the file
22 % pgfutil-XXXX.tex is read, where XXXX is the format name (latex,
23 % plain, or context).
24
25
26
27 \catcode`\@=11\relax
28
29
30 % Which format is loaded?
31 \newif\ifpgfutil@format@is@latex
32 \newif\ifpgfutil@format@is@plain
33 \newif\ifpgfutil@format@is@context
34
35 % Aliases
36
37 \let\pgfutil@exp\romannumeral
38 \chardef\pgfutil@exp@end=0
39
40 % Simple stuff
41
42 % \pgfutil@trimspaces{<token list>}
43 %
44 % Removes spaces on both sides of <token list>. Same as trimspaces.sty
45 \catcode`\Q=3
46 \def\pgfutil@trimspaces#1{%
47 \romannumeral-`0\pgfutil@trimspaces@\noexpand#1Q Q}
48 \def\pgfutil@trimspaces@#1 Q{\pgfutil@trimspaces@@#1Q}
49 \def\pgfutil@trimspaces@@#1Q#2{#1}
50 \catcode`\Q=11
51
52 \def\pgfutil@trimspaces@e#1{%
53 \expandafter\pgfutil@trimspaces\expandafter{\pgfutil@expanded{#1}}%
54 }
55
56 % \pgfutil@ifx{<token 1>}{<token 2>}{<true code>}{<false code>}
57 %
58 % This macro is expandable.
59 \long\def\pgfutil@ifx#1#2{%
60 \ifx#1#2%
61 \expandafter\pgfutil@firstoftwo
62 \else
63 \expandafter\pgfutil@secondoftwo
64 \fi}
65
66 % \pgfutil@ifstrequal{<lhs>}{<rhs>}{<true code>}{<false code>}
67 %
68 % Check if <lhs> and <rhs> are equal by string comparison
69 %
70 % This macro is expandable.
71 \long\def\pgfutil@ifstrequal#1#2{%
72 \ifnum\pgfsys@strcmp{\pgfutil@unexpanded{#1}}{\pgfutil@unexpanded{#2}}=0
73 \expandafter\pgfutil@firstoftwo
74 \else
75 \expandafter\pgfutil@secondoftwo
76 \fi
77 }
78
79 % \pgfutil@ifxempty{<token 1>}{<true code>}{<false code>}
80 %
81 % Shortcut for \pgfutil@ifx\pgfutil@empty{<token 1>}{<true code>}{<false code>}
82 %
83 % This macro is expandable.
84 \def\pgfutil@ifxempty{\pgfutil@ifx\pgfutil@empty}
85
86 % pgfutil@ifempty
87 %
88 % Check if #1 is empty. No expansion is performed on #1.
89 %
90 % This macro is expandable.
91 \long\def\pgfutil@ifempty#1{%
92 \if\relax\detokenize{#1}\relax
93 \expandafter\pgfutil@firstoftwo
94 \else
95 \expandafter\pgfutil@secondoftwo
96 \fi}
97
98 % \pgfutil@ifundefined{<macro name with backslash>}
99 % {<is undefined code>}{<is defined code>}
100 %
101 % This will let #1 to \relax if it undefined.
102 %
103 % This macro is expandable.
104 \def\pgfutil@ifundefined#1{%
105 \expandafter\pgfutil@ifx\csname#1\endcsname\relax}
106
107 % A variant of \pgfutil@ifundefined which will NOT let #1 to \relax it
108 % is undefined.
109 \def\pgfutil@IfUndefined#1{%
110 \ifcsname#1\endcsname
111 \expandafter\pgfutil@secondoftwo
112 \else
113 \expandafter\pgfutil@firstoftwo
114 \fi
115 }
116
117 \long\def\pgfutil@firstofone#1{#1}
118 \long\def\pgfutil@firstoftwo#1#2{#1}
119 \long\def\pgfutil@secondoftwo#1#2{#2}
120 \def\pgfutil@empty{}
121 \long\def\pgfutil@gobble@until@relax#1\relax{}
122 \long\def\pgfutil@gobble#1{}
123 \long\def\pgfutil@gobbletwo#1#2{}
124 \def\pgfutil@namedef#1{\expandafter\def\csname #1\endcsname}
125 \def\pgfutil@namelet#1{\expandafter\pgfutil@@namelet\csname#1\endcsname}
126 \def\pgfutil@@namelet#1#2{\expandafter\let\expandafter#1\csname#2\endcsname}
127 \long\def\pgfutil@g@addto@macro#1#2{%
128 \begingroup
129 \pgfutil@toks@\expandafter{#1#2}%
130 \xdef#1{\the\pgfutil@toks@}%
131 \endgroup}
132 \newif\ifpgfutil@tempswa
133 \newif\ifpgfutil@tempswb
134
135 % \pgfutil@pushmacro
136 % \pgfutil@popmacro
137 %
138 % Macro stack. Allows pushing a macro over \egroup. Taken from ConTeXt
139
140 \let\pgfutil@pushmacro@string\pgfutil@empty
141
142 \def\pgfutil@pushmacro#1{%
143 \xdef\pgfutil@pushmacro@string{\string#1}%
144 \ifcsname pgfutil@pushedmacro@\pgfutil@pushmacro@string\endcsname\else
145 % \newcount is \outer in Plain
146 \csname newcount\expandafter\endcsname\csname pgfutil@pushedmacro@\pgfutil@pushmacro@string\endcsname
147 \fi
148 \global\advance\csname pgfutil@pushedmacro@\pgfutil@pushmacro@string\endcsname 1\relax
149 \global\expandafter\let\csname\the\csname pgfutil@pushedmacro@\pgfutil@pushmacro@string\endcsname\pgfutil@pushmacro@string\endcsname#1%
150 }
151
152 \def\pgfutil@popmacro#1{%
153 \xdef\pgfutil@pushmacro@string{\string#1}%
154 \expandafter\let\expandafter#1\csname\the\csname pgfutil@pushedmacro@\pgfutil@pushmacro@string\endcsname\pgfutil@pushmacro@string\endcsname
155 \global\advance\csname pgfutil@pushedmacro@\pgfutil@pushmacro@string\endcsname -1\relax
156 }
157
158 % pgfutil@ifnextchar
159
160 \long\def\pgfutil@ifnextchar#1#2#3{%
161 \let\pgfutil@reserved@d=#1%
162 \def\pgfutil@reserved@a{#2}%
163 \def\pgfutil@reserved@b{#3}%
164 \futurelet\pgfutil@let@token\pgfutil@ifnch}
165 \def\pgfutil@ifnch{%
166 \ifx\pgfutil@let@token\pgfutil@sptoken
167 \let\pgfutil@reserved@c\pgfutil@xifnch
168 \else
169 \ifx\pgfutil@let@token\pgfutil@reserved@d
170 \let\pgfutil@reserved@c\pgfutil@reserved@a
171 \else
172 \let\pgfutil@reserved@c\pgfutil@reserved@b
173 \fi
174 \fi
175 \pgfutil@reserved@c}
176 {%
177 \def\:{\global\let\pgfutil@sptoken= } \:
178 \def\:{\pgfutil@xifnch} \expandafter\gdef\: {\futurelet\pgfutil@let@token\pgfutil@ifnch}
179 }
180
181 % pgfutil@ignorespaces
182
183 \def\pgfutil@ignorespaces
184 {\ifx\pgfutil@let@token\pgfutil@sptoken
185 \expandafter\pgfutil@ignorespaces@helper
186 \else
187 \expandafter\pgfutil@next
188 \fi}
189
190 {
191 \def\:{\pgfutil@ignorespaces@helper}
192 \expandafter\gdef\: {\futurelet\pgfutil@let@token\pgfutil@ignorespaces}
193 }
194
195 % pgfutil@in@
196
197 \newif\ifpgfutil@in@
198
199 % Usage:
200 % \pgfutil@in@{one}{three two one}
201 % \ifpgfutil@in@
202 % -> will be true!
203 % \else
204 % \fi
205 %
206 % \pgfutil@in@{,}{1234,456567}
207 % \ifpgfutil@in@
208 % -> will be true!
209 % \else
210 % \fi
211 \def\pgfutil@in@#1#2{%
212 \def\pgfutil@in@@##1#1##2##3\pgfutil@in@@{%
213 \ifx\pgfutil@in@##2\pgfutil@in@false\else\pgfutil@in@true\fi}%
214 \pgfutil@in@@#2#1\pgfutil@in@\pgfutil@in@@}
215
216
217 % pgfutil@for
218
219 \def\pgfutil@nnil{\pgfutil@nil}
220 \def\pgfutil@fornoop#1\@@#2#3{}
221 \long\def\pgfutil@for#1:=#2\do#3{%
222 \expandafter\def\expandafter\pgfutil@fortmp\expandafter{#2}%
223 \ifx\pgfutil@fortmp\pgfutil@empty \else
224 \expandafter\pgfutil@forloop#2,\pgfutil@nil,\pgfutil@nil\@@#1{#3}\fi}
225 \long\def\pgfutil@forloop#1,#2,#3\@@#4#5{\def#4{#1}\ifx #4\pgfutil@nnil \else
226 #5\def#4{#2}\ifx #4\pgfutil@nnil \else#5\pgfutil@iforloop #3\@@#4{#5}\fi\fi}
227 \long\def\pgfutil@iforloop#1,#2\@@#3#4{\def#3{#1}\ifx #3\pgfutil@nnil
228 \expandafter\pgfutil@fornoop \else
229 #4\relax\expandafter\pgfutil@iforloop\fi#2\@@#3{#4}}
230 \def\pgfutil@tfor#1:={\pgfutil@tf@r#1 }
231 \long\def\pgfutil@tf@r#1#2\do#3{\def\pgfutil@fortmp{#2}\ifx\pgfutil@fortmp\pgfutil@space\else
232 \pgfutil@tforloop#2\pgfutil@nil\pgfutil@nil\@@#1{#3}\fi}
233 \long\def\pgfutil@tforloop#1#2\@@#3#4{\def#3{#1}\ifx #3\pgfutil@nnil
234 \expandafter\pgfutil@fornoop \else
235 #4\relax\expandafter\pgfutil@tforloop\fi#2\@@#3{#4}}
236
237 \def\pgfutil@space{ }
238
239 % pgfutil@IfFileExists
240
241 \chardef\pgfutil@inputcheck0
242 \def\pgfutil@IfFileExists#1#2#3{%
243 \openin\pgfutil@inputcheck=#1 %
244 \ifeof\pgfutil@inputcheck
245 #3\relax
246 \else
247 #2\relax
248 \fi
249 \closein\pgfutil@inputcheck}
250
251 \def\pgfutil@InputIfFileExists#1#2#3{\pgfutil@IfFileExists{#1}{\input #1\relax#2}{#3}}%
252
253
254 % pgfutil@loop (from plain.tex)
255
256 \def\pgfutil@loop#1\pgfutil@repeat{\def\pgfutil@body{#1}\pgfutil@iterate}
257 \def\pgfutil@iterate{\pgfutil@body \let\pgfutil@next\pgfutil@iterate \else\let\pgfutil@next\relax\fi \pgfutil@next}
258 \let\pgfutil@repeat=\fi % this makes \loop...\if...\repeat skippable
259
260 % \pgfutil@switch
261 %
262 % #1: comparison function of the form
263 % \comp{<lhs>}{<rhs>}{<true code>}{<false code>}
264 % Has to be expandable!
265 % #2: lhs
266 % #3: sequence of rhs-value pairs
267 % #4: default statement in case of match
268 % #5: default statement in case of no match
269 %
270 % Example:
271 %
272 % \pgfutil@switch\pgfutil@ifx\pgf@let@token{%
273 % {(}{\tikz@movetoabs}%)
274 % {+}{\tikz@movetorel}%
275 % {-}{\tikz@lineto}%
276 % }{}{\tikz@expand}
277
278 \let\pgfutil@scan@mark\relax
279 \let\pgfutil@scan@stop\relax
280
281 \long\def\pgfutil@switch#1#2#3#4#5{%
282 \pgfutil@exp
283 \pgfutil@switch@@{#1}%
284 {#2}#3% user-defined cases
285 {#2}{}% default case
286 \pgfutil@scan@mark{#4}% true code
287 \pgfutil@scan@mark{#5}% false code
288 \pgfutil@scan@stop
289 }
290 \long\def\pgfutil@switch@@#1#2#3#4{%
291 #1{#2}{#3}%
292 {\pgfutil@switch@end{#4}}%
293 {\pgfutil@switch@@{#1}{#2}}%
294 }
295 \long\def\pgfutil@switch@end#1#2#3\pgfutil@scan@mark#4#5\pgfutil@scan@stop{\pgfutil@exp@end#1#4}
296
297 % aux-read-hook
298
299 \let\pgfutil@aux@read@hook=\relax
300
301
302 % Tokens for the end of the typesetting -- they will be added at the
303 % end of every job (hopefully...).
304
305 \newtoks\pgfutil@everybye
306
307
308 %
309 % PDF-Resource management -- might seem strange that this is here, but
310 % it turns out to be the correct place since latex and context handle
311 % this stuff quite differently
312 %
313
314 % Adding something to the pdf-resources:
315
316 \def\pgfutil@addpdfresource@extgs#1{\pgf@sys@addpdfresource@extgs@plain{#1}}
317 \def\pgfutil@addpdfresource@colorspaces#1{\pgf@sys@addpdfresource@colorspaces@plain{#1}}
318 \def\pgfutil@addpdfresource@patterns#1{\pgf@sys@addpdfresource@patterns@plain{#1}}
319 \def\pgfutil@setuppdfresources{\pgf@sys@setuppdfresources@plain}
320
321
322 % Inserts the argument at the begin of the current page (hopefully)
323
324 \let\pgfutil@insertatbegincurrentpage=\relax
325
326
327 % Ragged left and ragged right
328
329 \def\pgfutil@raggedright{\rightskip\z@ plus2em \spaceskip.3333em \xspaceskip.5em\relax}
330 \def\pgfutil@raggedleft{\leftskip\z@ plus2em \rightskip\z@ \spaceskip.3333em \xspaceskip.5em\parfillskip0pt\relax}
331
332
333 % Error Messages
334 \def\pgfutil@packageerror#1#2#3{\errhelp{#3}\errmessage{Package #1 Error: #2}}
335 \def\pgfutil@packagewarning#1#2{\immediate\write17{Package #1: Warning! #2.}}
336 \def\pgferror#1{\pgfutil@packageerror{pgf}{#1}{}}
337 \def\pgfwarning#1{\pgfutil@packagewarning{pgf}{#1}}
338
339
340 % Library files inclusion
341
342
343 % Include a library file.
344 %
345 % #1 = List of names of library file.
346 %
347 % Description:
348 %
349 % This command includes a list of library files. For each file X in the
350 % list, the file pgflibraryX.code.tex is included, provided this has
351 % not been done earlier.
352 %
353 % For the convenience of Context users, both round and square brackets
354 % are possible for the argument.
355 %
356 % Example:
357 %
358 % \usepgflibrary{arrows}
359 % \usepgflibrary[patterns,snakes]
360
361 \def\usepgflibrary{\pgfutil@ifnextchar[{\use@pgflibrary}{\use@@pgflibrary}}%}
362 \def\use@pgflibrary[#1]{\use@@pgflibrary{#1}}
363 \def\use@@pgflibrary#1{%
364 \edef\pgf@list{#1}%
365 \pgfutil@for\pgf@temp:=\pgf@list\do{%
366 \expandafter\pgfkeys@spdef\expandafter\pgf@temp\expandafter{\pgf@temp}%
367 \ifx\pgf@temp\pgfutil@empty
368 \else
369 \expandafter\ifx\csname pgf@library@\pgf@temp @loaded\endcsname\relax%
370 \expandafter\let\csname pgf@library@\pgf@temp @loaded\endcsname=\pgfutil@empty%
371 \expandafter\edef\csname pgf@library@#1@atcode\endcsname{\the\catcode`\@}
372 \expandafter\edef\csname pgf@library@#1@barcode\endcsname{\the\catcode`\|}
373 \expandafter\edef\csname pgf@library@#1@dollarcode\endcsname{\the\catcode`\$}
374 \catcode`\@=11
375 \catcode`\|=12
376 \catcode`\$=3
377 \pgfutil@InputIfFileExists{pgflibrary\[email protected]}{}{%
378 \pgferror{I did not find the pgf library
379 '\pgf@temp'. I looked for the file named
380 pgflibrary\[email protected], but could not find it in
381 the current texmf trees.}
382 }%
383 \catcode`\@=\csname pgf@library@#1@atcode\endcsname
384 \catcode`\|=\csname pgf@library@#1@barcode\endcsname
385 \catcode`\$=\csname pgf@library@#1@dollarcode\endcsname
386 \fi%
387 \fi
388 }%
389 }
390
391
392 % Include a module file.
393 %
394 % #1 = List of names of module files.
395 %
396 % Description:
397 %
398 % This command includes a list of module files. For each file X in the
399 % list, the file pgfmoduleX.code.tex is included, provided this has
400 % not been done earlier.
401 %
402 % For the convenience of Context users, both round and square brackets
403 % are possible for the argument.
404 %
405 % Example:
406 %
407 % \usepgfmodule{matrix}
408
409 \def\usepgfmodule{\pgfutil@ifnextchar[{\use@pgfmodule}{\use@@pgfmodule}}%}
410 \def\use@pgfmodule[#1]{\use@@pgfmodule{#1}}
411 \def\use@@pgfmodule#1{%
412 \edef\pgf@list{#1}%
413 \pgfutil@for\pgf@temp:=\pgf@list\do{%
414 \expandafter\ifx\csname pgf@module@\pgf@temp @loaded\endcsname\relax%
415 \expandafter\let\csname pgf@module@\pgf@temp @loaded\endcsname=\pgfutil@empty%
416 \expandafter\edef\csname pgf@module@#1@atcode\endcsname{\the\catcode`\@}
417 \expandafter\edef\csname pgf@module@#1@barcode\endcsname{\the\catcode`\|}
418 \expandafter\edef\csname pgf@module@#1@dollarcode\endcsname{\the\catcode`\$}
419 \catcode`\@=11
420 \catcode`\|=12
421 \catcode`\$=3
422 \input pgfmodule\[email protected]
423 \catcode`\@=\csname pgf@module@#1@atcode\endcsname
424 \catcode`\|=\csname pgf@module@#1@barcode\endcsname
425 \catcode`\$=\csname pgf@module@#1@dollarcode\endcsname
426 \fi%
427 }%
428 }
429
430 \def\pgfutilensuremath#1{%
431 \ifmmode#1\else$#1$\fi
432 }
433
434
435 %
436 % Guess the driver:
437 %
438
439 \begingroup
440 \catcode`\"=12
441 \edef\pgf@loc@TMPa{"}%
442 %
443 % prepares file names by checking for double colons.
444 %
445 % If '#1' is a usual file name without anything fancy, \pgfretval
446 % will simply contain it. However, if '#1' contains double colons
447 % (introduces, for example, by pdftex because there are white
448 % spaces in '#1'), the routine
449 % - removes the double colons,
450 % - re-inserts them outside of the string.
451 %
452 % \pgfutilpreparefilename{file.tex} -> file.tex
453 % \pgfutilpreparefilename{"A file name".file} -> "A file name.file"
454 %
455 % The resulting file name is returned in '\pgfretval',
456 % furthermore, a *quoted* version of the file name is returned in
457 % \pgfretvalquoted. The latter result is to have some sort of
458 % output normalisation: if the file name as such contains double
459 % quotes, we don't want to insert another set of them.
460 %
461 % This special handling has the following purposes:
462 % 1. Both, treatment of white spaces and double colons is not
463 % properly defined in TeX. Thus, we should only work with them if
464 % necessary and maintain backwards compatibility as far as
465 % possible.
466 % 2. It *should* work if there are spaces.
467 %
468 \gdef\pgfutilpreparefilename#1{%
469 \begingroup
470 \ifnum\the\catcode`\"=13
471 \pgfutilconvertdcolon
472 \fi
473 \xdef\pgf@temp{#1}%
474 \endgroup
475 \expandafter\pgfutil@in@\expandafter"\expandafter{\pgf@temp}%
476 \ifpgfutil@in@
477 \def\pgf@loc@TMPa{\pgfutilstrreplace{"}{}}%
478 \expandafter\pgf@loc@TMPa\expandafter{\pgf@temp}%
479 \edef\pgfretval{"\pgfretval"}% re-insert quotes! Otherwise, TeX can't use the file name.
480 \let\pgfretvalquoted=\pgfretval
481 \else
482 \let\pgfretval=\pgf@temp
483 \edef\pgfretvalquoted{"\pgfretval"}%
484 \fi
485 }%
486 \catcode`\"=13
487 \xdef\pgfutilconvertdcolon{%
488 \noexpand\def\noexpand"{\pgf@loc@TMPa}%
489 }%
490 \endgroup
491
492 % Converts an arbitrary command (without arguments) to a string in which all characters
493 % have category 12.
494 %
495 % #1: a macro name (which takes no arguments)
496 % #2: a macro name which will be assigned to '#1' converted to string.
497 %
498 % This uses '\meaning#1' hackery.
499 \def\pgfutil@command@to@string#1#2{%
500 \expandafter\pgfutil@command@to@string@@\meaning#1\pgfutil@EOI{#2}%
501 }%
502 \xdef\pgfutil@glob@TMPa{\meaning\pgfutil@empty}%
503 \expandafter\def\expandafter\pgfutil@command@to@string@@\pgfutil@glob@TMPa#1\pgfutil@EOI#2{%
504 \def#2{#1}%
505 }%
506 \begingroup
507 \catcode`\|=0
508 \catcode`\\=12
509 |gdef|pgfutil@backslash@as@other{\}%
510 |endgroup
511
512 % Checks if the token sequence #1 (unexpanded) contains a macro and
513 % invokes #2 if so and #3 if not.
514 \def\pgfutilifcontainsmacro#1#2#3{%
515 \def\pgf@marshal{#1}%
516 \pgfutil@command@to@string\pgf@marshal\pgf@marshal
517 \edef\pgf@marshal{\noexpand\pgfutil@in@{\pgfutil@backslash@as@other}{\pgf@marshal}}%
518 \pgf@marshal
519 \ifpgfutil@in@
520 \def\pgf@marshal{#2}%
521 \else
522 \def\pgf@marshal{#3}%
523 \fi
524 \pgf@marshal
525 }%
526
527 % Tests if string '#2' starts with pattern '#1'.
528 %
529 % If so, it executes #3 and defines \pgfretval to contain the
530 % suffix which is not equal to #1. Otherwise it executes #4.
531 \def\pgfutilifstartswith#1#2#3#4{%
532 \def\pgfutilifstartswith@ ##1#1##2\pgf@EOI{%
533 \def\pgfutil@tmp{##1}%
534 \ifx\pgfutil@tmp\pgfutil@empty
535 % Ah - a hit!
536 %
537 % define \pgfretval to be the suffix...
538 \def\pgfutil@tmp#1####1\pgf@EOI{%
539 \def\pgfretval{####1}%
540 }%
541 \pgfutil@tmp#2\pgf@EOI%
542 %
543 % ... and execute the <true> code:
544 #3\relax%
545 \else
546 % hm. No such prefix.
547 #4\relax%
548 \fi
549 }%
550 \pgfutilifstartswith@#2--#1\pgf@EOI%
551 }%
552
553 % Usage:
554 % \pgfutilstrreplace{<token>}{<replacement>}{<string>}
555 %
556 % -> will assign the modified string into \pgfretval.
557 %
558 % #1: the string to search (one or more tokens)
559 % #2: zero, one or more tokens which will be inserted instead of '#1'.
560 % #3: the string to search in
561 \long\def\pgfutilstrreplace#1#2#3{%
562 \def\pgfretval{}%
563 \long\def\pgfutil@search@and@replace@@##1#1##2\pgf@EOI{%
564 \expandafter\def\expandafter\pgfretval\expandafter{\pgfretval ##1#2}%
565 \pgfutil@search@and@replace@loop{#1}{##2}%
566 }%
567 \pgfutil@search@and@replace@loop{#1}{#3}%
568 }
569 \long\def\pgfutil@search@and@replace@loop#1#2{%
570 \pgfutil@in@{#1}{#2}%
571 \ifpgfutil@in@
572 \def\pgf@loc@TMPa{\pgfutil@search@and@replace@@ #2\pgf@EOI}%
573 \else
574 \expandafter\def\expandafter\pgfretval\expandafter{\pgfretval #2}%
575 \let\pgf@loc@TMPa=\relax
576 \fi
577 \pgf@loc@TMPa
578 }%
579
580 % Solves a linear equation system of size 2x2 using gauss elimination.
581 %
582 % It employs TeX register arithmetics to do so.
583 % #1: should contain 4 sets of braces with matrix entries,
584 % {<a11>}{<a12>}
585 % {<a21>}{<a22>}
586 % where each entry should be a number without unit.
587 % It is acceptable if '#1' is a macro which expands to the expected
588 % format.
589 % #2: should contain 2 sets of braces with the right-hand-side,
590 % {<r1>}{<r2>}
591 % where each entry should be a number without unit.
592 % It is acceptable if '#2' is a macro which expands to the expected
593 % format.
594 %
595 % It will assign \pgfmathresult to contain two sets of braces with the
596 % result.
597 %
598 % Example:
599 % \pgfutilsolvetwotwoleq{
600 % {0.24}{1}
601 % {-0.97}{0}
602 % }{
603 % {-7}
604 % {18}
605 % }
606 % -> yields \pgfmathresult={−18.55618}{−2.54642}
607 %
608 % The algorithm employs column pivotisation.
609 %
610 % If the matrix is singular, the routine will return {}.
611 \def\pgfutilsolvetwotwoleq#1#2{%
612 \begingroup
613 \dimendef\aa=0
614 \dimendef\ab=1
615 \dimendef\ba=2
616 \dimendef\bb=3
617 \dimendef\ra=4
618 \dimendef\rb=5
619 \dimendef\tmpa=6
620 \dimendef\tmpb=7
621 \edef\pgf@temp{#1}%
622 \expandafter\pgfutilsolvetwotwoleq@A\pgf@temp
623 \edef\pgf@temp{#2}%
624 \expandafter\pgfutilsolvetwotwoleq@r\pgf@temp
625 %
626 \pgfutilsolvetwotwoleq@ifislarger\aa\ba{%
627 % identity "permutation":
628 \def\Pa{a}%
629 \def\Pb{b}%
630 }{%
631 % permutation matrix: switch rows!
632 \def\Pa{b}%
633 \def\Pb{a}%
634 }%
635 % \pivot := 1/aa
636 \tmpa=\csname m\Pa a\endcsname pt %
637 \ifdim\tmpa<0pt \tmpa=-\tmpa\fi
638 \ifdim\tmpa<0.0001pt
639 % singular matrix!
640 \let\pgfmathresult=\pgfutil@empty
641 \else
642 \pgfmathreciprocal@
643 {\csname m\Pa a\endcsname}%
644 \let\pivot=\pgfmathresult
645 %
646 % \factor := 1/aa * ba
647 \csname \Pb a\endcsname=\pivot\csname \Pb a\endcsname
648 \edef\factor{\expandafter\pgf@sys@tonumber\csname \Pb a\endcsname}%
649 %
650 % bb -= ba/aa * ab
651 \tmpa=-\factor\csname \Pa b\endcsname
652 \advance\csname \Pb b\endcsname by\tmpa
653 %
654 % rb -= ba/aa * ra
655 \tmpa=-\factor\csname r\Pa\endcsname
656 \advance\csname r\Pb\endcsname by\tmpa
657 %
658 \tmpa=\csname \Pb b\endcsname%
659 \ifdim\tmpa<0pt \tmpa=-\tmpa\fi
660 \ifdim\tmpa<0.0001pt
661 % singular matrix!
662 \let\pgfmathresult=\pgfutil@empty
663 \else
664 % xb := rb / bb (the modified rb and modified bb!)
665 \pgfmathdivide@
666 {\expandafter\pgf@sys@tonumber\csname r\Pb\endcsname}
667 {\expandafter\pgf@sys@tonumber\csname \Pb b\endcsname}%
668 \expandafter\let\csname pgfmathresult\Pb\endcsname=\pgfmathresult
669 %
670 % ra := ra - xb * ab
671 \tmpa=\csname pgfmathresult\Pb\endcsname\csname \Pa b\endcsname
672 \advance\csname r\Pa\endcsname by-\tmpa
673 %
674 % xa := 1/aa * ra (the modified ra!)
675 \tmpa=\pivot\csname r\Pa\endcsname
676 \expandafter\edef\csname pgfmathresult\Pa\endcsname{\pgf@sys@tonumber\tmpa}%
677 %
678 \edef\pgfmathresult{%
679 {\csname pgfmathresult\Pa\endcsname}%
680 {\csname pgfmathresult\Pb\endcsname}%
681 }%
682 \fi
683 \fi
684 \pgfmath@smuggleone\pgfmathresult
685 \endgroup
686 }%
687
688 \def\pgfutilsolvetwotwoleq@ifislarger#1#2#3#4{%
689 \tmpa=#1
690 \ifdim\tmpa<0pt
691 \multiply\tmpa by-1
692 \fi
693 \tmpb=#2
694 \ifdim\tmpb<0pt
695 \multiply\tmpb by-1
696 \fi
697 \ifdim\tmpa>\tmpb
698 #3%
699 \else
700 #4%
701 \fi
702 }%
703
704 \def\pgfutilsolvetwotwoleqfloat@ifislarger#1#2#3#4{%
705 \pgfmathfloatabs@{#1}\let\tmpa=\pgfmathresult
706 \pgfmathfloatabs@{#2}\let\tmpb=\pgfmathresult
707 \pgfmathfloatlessthan@{\tmpb}{\tmpa}%
708 \ifpgfmathfloatcomparison
709 #3%
710 \else
711 #4%
712 \fi
713 }%
714 \def\pgfutilsolvetwotwoleq@A#1#2#3#4{%
715 \def\maa{#1}\def\mab{#2}%
716 \def\mba{#3}\def\mbb{#3}%
717 \aa=#1pt \ab=#2pt
718 \ba=#3pt \bb=#4pt
719 }
720 \def\pgfutilsolvetwotwoleq@r#1#2{%
721 \ra=#1pt \rb=#2pt
722 }%
723 \def\pgfutilsolvetwotwoleqfloat@A#1#2#3#4{%
724 \pgfmathfloatparsenumber{#1}\let\maa=\pgfmathresult
725 \pgfmathfloatparsenumber{#2}\let\mab=\pgfmathresult
726 \pgfmathfloatparsenumber{#3}\let\mba=\pgfmathresult
727 \pgfmathfloatparsenumber{#4}\let\mbb=\pgfmathresult
728 }
729 \def\pgfutilsolvetwotwoleqfloat@r#1#2{%
730 \pgfmathfloatparsenumber{#1}\let\ra=\pgfmathresult
731 \pgfmathfloatparsenumber{#2}\let\rb=\pgfmathresult
732 }%
733
734 % Same as \pgfutilsolvetwotwoleq, but using floating point
735 % arithmetic. The return value is still in fixed point.
736 \def\pgfutilsolvetwotwoleqfloat#1#2{%
737 \begingroup
738 \pgfmathfloatcreate{1}{1.0}{-4}% FIXME : use a smaller threshold for FPU?
739 \let\thresh=\pgfmathresult
740 %
741 \edef\pgf@temp{#1}%
742 \expandafter\pgfutilsolvetwotwoleqfloat@A\pgf@temp
743 \edef\pgf@temp{#2}%
744 \expandafter\pgfutilsolvetwotwoleqfloat@r\pgf@temp
745 %
746 \pgfutilsolvetwotwoleqfloat@ifislarger\maa\mba{%
747 % identity "permutation":
748 \def\Pa{a}%
749 \def\Pb{b}%
750 }{%
751 % permutation matrix: switch rows!
752 \def\Pa{b}%
753 \def\Pb{a}%
754 }%
755 % \pivot := 1/aa
756 \expandafter\pgfmathfloatabs@\expandafter{\csname m\Pa a\endcsname}%
757 \let\tmpa=\pgfmathresult
758 \pgfmathfloatlessthan@{\tmpa}{\thresh}%
759 \ifpgfmathfloatcomparison
760 % singular matrix!
761 \let\pgfmathresult=\pgfutil@empty
762 \else
763 \expandafter\pgfmathfloatreciprocal@\expandafter{\csname m\Pa a\endcsname}%
764 \let\pivot=\pgfmathresult
765 %
766 % \factor := 1/aa * ba
767 \expandafter\pgfmathfloatmultiply@\expandafter{\csname m\Pb a\endcsname}{\pivot}%
768 \let\factor=\pgfmathresult
769 \expandafter\let\csname m\Pb a\endcsname=\factor
770 %
771 % bb -= ba/aa * ab
772 \expandafter\pgfmathfloatmultiply@\expandafter{\csname m\Pa b\endcsname}{\factor}%
773 \let\tmpa=\pgfmathresult
774 \expandafter\pgfmathfloatsubtract@\expandafter{\csname m\Pb b\endcsname}{\tmpa}%
775 \expandafter\let\csname m\Pb b\endcsname=\pgfmathresult
776 %
777 % rb -= ba/aa * ra
778 \expandafter\pgfmathfloatmultiply@\expandafter{\csname r\Pa\endcsname}{\factor}%
779 \let\tmpa=\pgfmathresult
780 \expandafter\pgfmathfloatsubtract@\expandafter{\csname r\Pb\endcsname}{\tmpa}%
781 \expandafter\let\csname r\Pb\endcsname=\pgfmathresult
782 %
783 \expandafter\pgfmathfloatabs@\expandafter{\csname m\Pb b\endcsname}%
784 \let\tmpa=\pgfmathresult
785 \pgfmathfloatlessthan@{\tmpa}{\thresh}%
786 \ifpgfmathfloatcomparison
787 % singular matrix!
788 \let\pgfmathresult=\pgfutil@empty
789 \else
790 % xb := rb / bb (the modified rb and modified bb!)
791 \edef\pgf@marshal{%
792 \noexpand\pgfmathfloatdivide@
793 {\csname r\Pb\endcsname}
794 {\csname m\Pb b\endcsname}%
795 }%
796 \pgf@marshal
797 \expandafter\let\csname pgfmathresult\Pb\endcsname=\pgfmathresult
798 \let\tmpa=\pgfmathresult
799 %
800 % ra := ra - xb * ab
801 \expandafter\pgfmathfloatmultiply@\expandafter{\csname m\Pa b\endcsname}{\tmpa}%
802 \let\tmpa=\pgfmathresult
803 \expandafter\pgfmathfloatsubtract@\expandafter{\csname r\Pa\endcsname}{\tmpa}%
804 \expandafter\let\csname r\Pa\endcsname=\pgfmathresult
805 %
806 % xa := 1/aa * ra (the modified ra!)
807 \expandafter\pgfmathfloatmultiply@\expandafter{\csname r\Pa\endcsname}{\pivot}%
808 \expandafter\let\csname pgfmathresult\Pa\endcsname=\pgfmathresult
809 %
810 \edef\pgfmathresult{%
811 {\csname pgfmathresult\Pa\endcsname}%
812 {\csname pgfmathresult\Pb\endcsname}%
813 }%
814 \expandafter\pgfutilsolvetwotwoleqfloat@to@pgf@range\pgfmathresult
815 \fi
816 \fi
817 \pgfmath@smuggleone\pgfmathresult
818 \endgroup
819 }%
820
821
822 \def\pgfutilsolvetwotwoleqfloat@to@pgf@range#1#2{%
823 \pgfmathfloatcreate{1}{1.6}{4}\let\pgfutilsolvetwotwoleqfloat@to@pgf@range@max=\pgfmathresult
824 \pgfmathfloatabs@{#1}%
825 \expandafter\pgfmathfloatlessthan@\expandafter{\pgfmathresult}{\pgfutilsolvetwotwoleqfloat@to@pgf@range@max}%
826 \ifpgfmathfloatcomparison
827 \pgfmathfloatabs@{#2}%
828 \expandafter\pgfmathfloatlessthan@\expandafter{\pgfmathresult}{\pgfutilsolvetwotwoleqfloat@to@pgf@range@max}%
829 \ifpgfmathfloatcomparison
830 % ok.
831 \pgfmathfloattofixed{#1}\let\tmpa=\pgfmathresult
832 \pgfmathfloattofixed{#2}%
833 \edef\pgfmathresult{{\tmpa}{\pgfmathresult}}%
834 \else
835 % singular (because PGF cannot represent its result
836 \let\pgfmathresult=\pgfutil@empty
837 \fi
838 \else
839 % singular (because PGF cannot represent its result
840 \let\pgfmathresult=\pgfutil@empty
841 \fi
842 }%
843
844 % there are funny programs which overwrite \read and \write (like
845 % tabularx).
846 \let\pgfutil@write=\write
847 \let\pgfutil@read=\read
848
849 % courtesy for latex:
850 \let\pgfutil@protect\relax
851
852 % Used by latex for doing reruns
853 % #1 = an internal label
854 % #2 = a to-be-tested text
855 \def\pgfutil@check@rerun#1#2{}
856
857 % new tempdims:
858 \newdimen\pgfutil@tempdima
859 \newdimen\pgfutil@tempdimb
860
861
862 % luatex stuff
863 % We assume luatex version > 0.39:
864 % - \directlua <general text> will work
865 % - \directlua is the only luatex primitive that we can assume
866 % accessible without being prefixed by the format via
867 % tex.enableprimitives.
868 % Ideas taken from the ifluatex package (Heiko Oberdiek)
869 \let\pgfutil@ifluatex\iffalse
870 \begingroup\expandafter\expandafter\expandafter\endgroup
871 \expandafter\ifx\csname directlua\endcsname\relax
872 \else
873 \expandafter\let\csname pgfutil@ifluatex\expandafter\endcsname
874 \csname iftrue\endcsname
875 \fi
876
877 \pgfutil@ifluatex
878 \let\pgfutil@directlua\directlua
879 \pgfutil@directlua{%
880 tex.enableprimitives('pgfutil@',{'luaescapestring'})}
881 \else
882 \def\pgfutil@directlua#1{}
883 \def\pgfutil@luaescapestring#1{}
884 \fi
885
886 % Emulate the "runsystem(...)" logging for LuaTeX
887 % based on the emulation in l3kernel (l3sys.dtx, @@_shell_now:e), and the
888 % ending periods are added following https://github.com/latex3/latex2e/commit/90d53de44fe4a09baa4a17506abe6ce9e120a7c3.
889 \def\pgfutil@shellescape@lua@eightseven#1{%
890 \pgfutil@directlua{%
891 local write_nl = texio.write_nl
892 local cmd = "\pgfutil@luaescapestring{#1}"
893 local status, msg = os.execute(cmd)
894 if status == nil then
895 write_nl("log", "runsystem(" .. cmd .. ")...(" .. msg .. ").\string\n")
896 elseif status == 0 then
897 write_nl("log", "runsystem(" .. cmd .. ")...executed.\string\n")
898 else
899 write_nl("log", "runsystem(" .. cmd .. ")...failed. " .. (msg or "") .. "\string\n")
900 end
901 }%
902 }%
903 \def\pgfutil@shellescape#1{%
904 \immediate\write18{#1}%
905 }%
906 \pgfutil@IfUndefined{directlua}{%
907 }{%
908 \pgfutil@IfUndefined{lastsavedimageresourcepages}{%
909 % Ah - an old version of luatex. It still supports \write18
910 }{%
911 % take the new variant...
912 \let\pgfutil@shellescape=\pgfutil@shellescape@lua@eightseven
913 }%
914 }%
915
916 % End of luatex stuff
917
918 % Advances a number stored in a macro and writes the result back into
919 % the macro.
920 % #1 is a macro containing a number.
921 \def\pgfutil@advancestringcounter#1{%
922 \begingroup
923 \c@pgf@counta=#1\relax
924 \advance\c@pgf@counta by1
925 \edef#1{\the\c@pgf@counta}%
926 \pgfmath@smuggleone#1%
927 \endgroup
928 }%
929
930 \endinput