Debian Codegraph @

texmf-dist/tex/generic/pgf/frontendlayer/tikz/libraries/tikzexternalshared.code.tex

raw
1 % Copyright 2008-2020 by Christian Feuersaenger
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 % This here is the shared implementation of TeX-dialect specific files
12 % tikzlibraryexternal.code.tex
13 % ******************************
14 %
15 %
16 % This file provides a high-level automatic export feature for tikz pictures.
17 % It allows to export EACH SINGLE FIGURE into a separate PDF.
18 %
19 % The simplest way is to use the 'convert with system call' option; it simply converts every figure using the write18 method. If that is not possible, a list of figure file names is generated and you need to generate these figures manually (or with a script).
20 %
21 %
22 % It replaces \tikzpicture/ \endtikzpicture and \tikz and invokes \beginpgfgraphicnamed ... \endpgfgraphicnamed
23 % commands if necessary.
24
25 % handle with extreme care and only in small, local groups:
26 \toksdef\t@tikzexternal@tmpa=0
27 \toksdef\t@tikzexternal@tmpb=1
28
29 \newif\iftikzexternal@nestedflag
30 \newif\iftikzexternal@verboseuptodate
31 \newif\iftikzexternal@verboseio
32 \newif\iftikzexternal@genfigurelist
33 \newif\iftikzexternal@onlynamed
34 \newif\iftikzexternal@file@isuptodate
35 \newif\iftikzexternal@force@remake
36 \newif\iftikzexternal@optimize
37 \newif\iftikzexternal@export@enabled
38 \tikzexternal@export@enabledtrue
39
40 % must be numeric!
41 \gdef\c@tikzexternal@version{2}
42
43 % This 'if' can be used as part of the public user interface.
44 %
45 % It is set by the 'remake next' key.
46 %
47 % It disables any up-to-date checks for the *next* picture, forcing a
48 % remake of it.
49 %
50 % It applies only to *one* picture.
51 \newif\iftikzexternalremakenext
52 \newif\iftikzexternal@verbose@optimize
53 \tikzexternal@verbose@optimizetrue
54
55 % A global boolean which can be used to skip single figures.
56 \newif\iftikzexternalexportnext
57 \tikzexternalexportnexttrue
58
59 % Invokes '#1' if the external lib is currently externalizing
60 % something and '#2' if not.
61 %
62 % This command must be called *after* \tikzexternalize.
63 \def\tikzifexternalizing#1#2{%
64 \ifpgf@external@grabshipout #1\else #2\fi
65 }%
66 % Invokes '#1' if the external lib is currently externalizing the NEXT FOLLOWING
67 % tikzpicture. It invokes '#2' if that is not the case.
68 \def\tikzifexternalizingnext#1#2{%
69 \ifpgf@external@grabshipout
70 \tikzexternalgetnextfilename\tikzexternal@temp
71 \tikzifexternaljobnamematches\tikzexternal@temp{#1}{#2}%
72 \else
73 #2%
74 \fi
75 }%
76 % Invokes '#1' if the external lib is currently externalizing the
77 % current picture. It invokes '#2' if that is not the case.
78 %
79 % If the command is invoked outside of a picture, '#2' will be
80 % invoked.
81 \def\tikzifexternalizingcurrent#1#2{%
82 \ifpgf@external@grabshipout
83 \tikzexternalgetcurrentfilename\tikzexternal@temp
84 \ifx\tikzexternal@temp\pgfutil@empty
85 #2%
86 \else
87 \tikzifexternaljobnamematches\tikzexternal@temp{#1}{#2}%
88 \fi
89 \else
90 #2%
91 \fi
92 }%
93
94 % Invokes '#2' if \jobname equals '#1' and '#3' if not.
95 % I suppose that '#1' is a macro containing the file name.
96 %
97 \def\tikzifexternaljobnamematches#1#2#3{%
98 \edef\pgf@tempa{\expandafter\string\csname#1\endcsname}%
99 \edef\pgf@tempb{\expandafter\string\csname\pgfactualjobname\endcsname}%
100 \ifx\pgf@tempa\pgf@tempb #2\else#3 \fi%
101 }%
102
103
104 \pgfutil@IfUndefined{pdfshellescape}{%
105 % let's hope that \usepackage{pdftexcmds} has been used...
106 \pgfutil@IfUndefined{pdf@shellescape}{%
107 \def\tikzexternalcheckshellescape{0}%
108 }{%
109 \let\tikzexternalcheckshellescape=\pdf@shellescape
110 }%
111 }{%
112 \let\tikzexternalcheckshellescape=\pdfshellescape
113 }%
114 \ifnum\tikzexternalcheckshellescape=1
115 \def\tikzexternalcheckshellescape{\pgfkeysvalueof{/tikz/external/shell escape}\space}%
116 \else
117 \def\tikzexternalcheckshellescape{}%
118 \fi
119
120 \newif\iftikzexternal@auto@detect@jobname
121
122 \pgfqkeys{/tikz/external}{
123 figure list/.is if=tikzexternal@genfigurelist,
124 aux in dpth/.style={/pgf/images/aux in dpth={#1}},%
125 disable dependency files/.code={%
126 \let\tikzexternalfiledependsonfile@ACTIVE=\tikzexternalfiledependsonfile
127 },
128 % 'mode' applies only if \jobname==real job name.
129 mode/.is choice,
130 mode/only graphics/.code = {%
131 \def\tikzexternal@opmode{0}%
132 \pgfkeysalso{/pgf/images/aux in dpth=true}%
133 },
134 mode/no graphics/.code = {\def\tikzexternal@opmode{1}},
135 % an alias for 'no graphics:'
136 mode/only pictures/.code = {\def\tikzexternal@opmode{1}},
137 mode/graphics if exists/.code= {%
138 \def\tikzexternal@opmode{2}%
139 \pgfkeysalso{/pgf/images/aux in dpth=true}%
140 },
141 mode/list only/.code = {\def\tikzexternal@opmode{3}\tikzexternal@genfigurelisttrue},
142 mode/convert with system call/.code={%
143 \def\tikzexternal@opmode{4}%
144 \pgfkeysalso{/tikz/external/figure list=false,/pgf/images/aux in dpth=true}%
145 },
146 mode/list and make/.code = {%
147 \def\tikzexternal@opmode{5}%
148 \pgfkeysalso{/tikz/external/figure list=true,/pgf/images/aux in dpth=true}%
149 },
150 mode=convert with system call,
151 force remake/.is if=tikzexternal@force@remake,
152 force remake/.default=true,
153 %
154 % DEPRECATED: this here will work out-of-the box (provided that
155 % 'aux in dpth' is active):
156 failed ref warnings for/.initial={\ref,\cite,\pageref},
157 export next/.is if=tikzexternalexportnext,
158 export/.is if=tikzexternal@export@enabled,
159 remake next/.is if=tikzexternalremakenext,
160 remake next/.default=true,
161 verbose IO/.is if=tikzexternal@verboseio,
162 verbose IO/.default=true,
163 verbose IO=true,
164 verbose optimize/.is if=tikzexternal@verbose@optimize,
165 verbose up to date/.is if=tikzexternal@verboseuptodate,
166 verbose/.style={
167 verbose IO=#1,
168 verbose optimize=#1,
169 verbose up to date=#1,
170 },
171 shell escape/.initial=-shell-escape,
172 read main aux/.is if=pgfexternalreadmainaux,
173 image discarded text/.initial={%
174 [[ \textsc{Image Discarded Due To} \texttt{`/tikz/external/%
175 \ifcase\tikzexternal@opmode\relax
176 mode=only graphics%
177 \or
178 mode=no graphics%
179 \or
180 mode=graphics if exists%
181 \or
182 mode=list only%
183 \or
184 mode=convert with system call%
185 \or
186 mode=list and make%
187 \fi
188 '}~]]%
189 \pgfutil@ifundefined{tikzexternal@warning@at@end}{%
190 \pgfutil@ifundefined{AtEndDocument}{}{%
191 \AtEndDocument{%
192 \message{! Package tikz Warning: Some images are not up-to-date and need to be generated. }%
193 }%
194 }%
195 \gdef\tikzexternal@warning@at@end{1}%
196 }{}%
197 },
198 optimize/.is choice,
199 optimize/true/.code={%
200 \tikzexternal@optimizetrue
201 \iftikzexternal@optimize
202 \ifpgf@external@grabshipout
203 % we have already started the externalization! Install
204 % optimization commands to activate the new one:
205 \tikzexternal@optimize@REPLACE
206 \fi
207 \fi
208 },
209 optimize/false/.code={%
210 \tikzexternal@optimizefalse%
211 \tikzexternal@optimize@RESTORE
212 },%
213 optimize=true,
214 optimize away text/.code={[ \textsc{\string#1\ optimized away because it does not contribute to exported PDF}]},
215 optimize/install/.code={},%
216 optimize/restore/.code={},%
217 % Expects two arguments, the command name and (optionally) a count
218 % of expected arguments.
219 % Example:
220 % 'optimize command away=\includegraphics'
221 % 'optimize command away={\mycommand}{3}'
222 %
223 % It accepts commands which have '[]' arguments and whose first
224 % argument always begins with '{'.
225 %
226 % #1: the command name
227 % #2: either empty or a number of EXPECTED arguments. It will be checked
228 % for one optional argument in square brackets as well.
229 optimize command away/.code 2 args={%
230 \expandafter\global\expandafter\let\csname\string#1@ORIG\endcsname=#1%
231 \pgfkeysalso{%
232 /tikz/external/optimize/install/.append code={%
233 \def#1{\tikzexternal@optimize@away@cmd{#1}{#2}}%
234 },
235 /tikz/external/optimize/restore/.append code={%
236 \expandafter\let\expandafter#1\csname\string#1@ORIG\endcsname
237 }
238 }%
239 \iftikzexternal@optimize
240 \ifpgf@external@grabshipout
241 % we have already started the externalization! Install
242 % optimization commands to activate the new one:
243 \tikzexternal@optimize@REPLACE
244 \fi
245 \fi
246 },
247 optimize command away=\includegraphics,
248 optimize command away=\pgfincludeexternalgraphics,
249 % EXPERIMENTAL (UNTESTED):
250 optimize latex env away/.code={%
251 \expandafter\let\expandafter\pgf@tempa\csname #1\endcsname
252 \expandafter\global\expandafter\let\csname #1@ORIG\endcsname=\pgf@tempa%
253 \pgfkeysalso{%
254 /tikz/external/optimize/install/.append code={%
255 \pgfutil@namedef{#1}{\tikzexternal@optimize@away@latex@env{#1}}%
256 },
257 /tikz/external/optimize/restore/.append code={%
258 \pgfutil@namelet{#1}{#1@ORIG}%
259 }%
260 }%
261 },
262 only named/.is if=tikzexternal@onlynamed,
263 only named/.default=true,
264 figure name/.initial=\tikzexternal@realjob-figure,
265 reset counter/.code={%
266 \expandafter\gdef\csname c@tikzext@no@#1\endcsname{0}%
267 },%
268 prefix/.code={\tikzsetexternalprefix{#1}},
269 up to date check/.is choice,
270 up to date check/simple/.code={\def\tikzexternal@uptodate@mode{0}},
271 % md5 relies on \pdfmdfivesum of pdftex. It uses 'diff' as fallback if we do not have that command.
272 up to date check/md5/.code={\def\tikzexternal@uptodate@mode{1}},
273 up to date check/diff/.code={\def\tikzexternal@uptodate@mode{2}},
274 up to date check=md5,
275 }
276
277 \expandafter\def\csname tikzexternal@[email protected]\endcsname{%
278 \pgfkeyssetvalue{/tikz/external/system call}{%
279 lualatex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\texsource"%
280 }%
281 }%
282 \expandafter\def\csname tikzexternal@[email protected]\endcsname{%
283 \pgfutil@IfUndefined{directlua}{%
284 \pgfkeyssetvalue{/tikz/external/system call}{%
285 pdflatex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\texsource"%
286 }%
287 }{%
288 \pgfkeyssetvalue{/tikz/external/system call}{%
289 lualatex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\texsource"%
290 }%
291 }%
292 }%
293 %--------------------------------------------------
294 % \expandafter\def\csname tikzexternal@[email protected]\endcsname{%
295 % \pgfkeyssetvalue{/tikz/external/system call}{%
296 % latex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\texsource"%
297 % && dvipdfm "\image".dvi %
298 % }%
299 % }%
300 %--------------------------------------------------
301 \expandafter\def\csname tikzexternal@[email protected]\endcsname{%
302 \pgfkeyssetvalue{/tikz/external/system call}{%
303 xelatex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\texsource"%
304 }%
305 }%
306 \expandafter\def\csname tikzexternal@[email protected]\endcsname{%
307 \pgfkeyssetvalue{/tikz/external/system call}{%
308 latex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\texsource" %
309 && dvips -o "\image".ps "\image".dvi %
310 }%
311 }%
312
313 % Auto-select a suitable default value for 'system call':
314 \pgfutil@ifundefined{tikzexternal@driver@\pgfsysdriver}{%
315 % fallback. We do not know the driver here.
316 \csname tikzexternal@[email protected]\endcsname
317 }{%
318 \csname tikzexternal@driver@\pgfsysdriver\endcsname
319 }%
320
321
322 \def\tikzsetfigurename#1{\pgfkeyssetvalue{/tikz/external/figure name}{#1}}%
323 \def\tikzappendtofigurename#1{\pgfkeys{/tikz/external/figure name/.add={}{#1}}}
324
325 % This method sets the file name prefix used for every import/export.
326 %
327 % It is invoked by the '/tikz/external/prefix' key.
328 \def\tikzsetexternalprefix#1{\def\tikzexternal@filenameprefix{#1}}
329 \tikzsetexternalprefix{}
330
331 % This method initialises automatic externalization.
332 %
333 % The command does need any argument, but it has two optional ones:
334 %
335 % 1. \tikzexternalize[<options>]
336 %
337 % 2. \tikzexternalize{<real job's name>}
338 %
339 % 3. \tikzexternalize[<options>]{<real job's name>}
340 %
341 % The <real job's name> is the job's file name, without the suffix
342 % .tex .
343 % If the <real job's name> is omitted, it will be deduced
344 % automatically.
345 %
346 % The effect is:
347 % 1. automatically surround EVERY tikzpicture by \beginpgfgraphicnamed % sections.
348 % 2. in case that the job's real name and the current \jobname differ,
349 % it will process ONLY the matching tikzpicture.
350 \def\tikzexternalize{%
351 \pgfutil@ifnextchar[{%
352 \tikzexternalize@opt
353 }{%
354 \tikzexternalize@opt[]%
355 }%
356 }%
357 \def\tikzexternalize@opt[#1]{%
358 \pgfutil@ifnextchar\bgroup{%
359 \tikzexternal@auto@detect@jobnamefalse
360 \tikzexternalize@opt@withname[#1]%
361 }{%
362 \tikzexternal@auto@detect@jobnametrue
363 \pgfutil@ifundefined{tikzexternalrealjob}{%
364 % ok, \tikzexternalrealjob is not known.
365 % Assume we are currently *not* externalizing.
366 \let\tikzexternalrealjob=\jobname
367 }{}%
368 \def\pgf@tempa{\tikzexternalize@opt@withname[#1]}%
369 \expandafter\pgf@tempa\expandafter{\tikzexternalrealjob}%
370 }%
371 }%
372 \def\tikzexternalize@opt@withname[#1]#2{%
373 \gdef\tikzexternalrealjob{#2}%
374 \if1\tikzexternalize@hasbeencalled
375 \else
376 \pgfqkeys{/tikz/external}{#1}%
377 \def\tikzexternal@realjob{#2}%
378 \tikzexternalauxlock@init
379 \pgfrealjobname{#2}%
380 \def\tikzexternalize@hasbeencalled{1}%
381 \tikzexternalenable
382 \def\tikzexternal@determineimgextension##1:##2\relax{\gdef\tikzexternalimgextension{##1}}%
383 \xdef\pgf@tempa{\pgfsys@imagesuffixlist}%
384 \expandafter\tikzexternal@determineimgextension\pgf@tempa:\relax
385 \pgfutil@ifundefined{includegraphics}{%
386 \let\tikzexternal@orig@includegraphics=\relax
387 }{%
388 \let\tikzexternal@orig@includegraphics=\includegraphics
389 }%
390 \let\tikzexternalfiledependsonfile=\tikzexternalfiledependsonfile@ACTIVE
391 \let\tikzpicturedependsonfile=\tikzpicturedependsonfile@ACTIVE
392 \fi
393 }
394 \def\tikzexternalize@hasbeencalled{0}%
395
396 \def\tikzifexternalizehasbeencalled#1#2{%
397 \if1\tikzexternalize@hasbeencalled
398 #1%
399 \else
400 #2%
401 \fi
402 }%
403
404 % If mode=`convert with system call', the boolean \ifpgfexternalreadmainaux
405 % will be set depending on the current lock file.
406 %
407 % The idea is as follows: Suppose
408 % pdflatex -shell-escape mainjob.tex
409 % is running.
410 %
411 % While it runs,
412 % a) it modifies its mainjob.aux file,
413 % b) it issues pdflatex -jobname mainjob-figure0 mainjob.tex .
414 %
415 % The call (b) will try to read mainjob.aux in order to resolve \ref
416 % commands. This may fail since the aux
417 % file is not complete; it may be subject to output buffering. To
418 % avoid such failure, a locking mechanism is established.
419 %
420 % The locking mechanisms causes (a) to write a lock command into
421 % mainjob.auxlock
422 % just before (b) is called. Then, (b) will check it. After (b) has
423 % completed, the lock will be reset.
424 %
425 % Thus: mode=convert with system call does NOT support \ref commands
426 % inside of images. You will need to call the externalization command
427 % manually. In this case, it should work.
428 %
429 % The lock file allows to detect automatically whether an image is created by
430 % 'convert with system call' or if the user issued the required command manually.
431 %
432 % @PRECONDITION : this command must be invoked AFTER
433 % \tikzexternal@realjob has been defined but BEFORE \pgfrealjobname is
434 % called.
435 %
436 % @POSTCONDITION : If \ifpgfexternalreadmainaux=\iffalse, we won't do
437 % anything. Otherwise, it will be changed depending for 'mode=convert
438 % with system call' if necessary.
439 \def\tikzexternalauxlock@init{%
440 \if4\tikzexternal@opmode
441 % mode=convert with system call
442 \ifpgfexternalreadmainaux
443 % ohoh. This case needs care because the main.aux might
444 % not be ready at this time!
445 % check.
446 %
447 \edef\pgf@tempa{\expandafter\string\csname\tikzexternal@realjob\endcsname}%
448 \edef\pgf@tempb{\expandafter\string\csname\jobname\endcsname}%
449 \ifx\pgf@tempa\pgf@tempb
450 % we are *not* externalizing. Set lock into its
451 % initial "unlocked" state.
452 \tikzexternalauxlock@setlock{0}%
453 \else
454 % we are externalizing. Query the lock's value:
455 \tikzexternalauxlock@getlockvalue
456 \if1\tikzexternallocked
457 \pgfexternalreadmainauxfalse
458 % the main .aux file won't be read right now.
459 \else
460 \pgfexternalreadmainauxtrue
461 \fi
462 \fi
463 \else
464 % NO-OP. We won't read the main aux file anyway.
465 \def\tikzexternalauxlock@setlock##1{}%
466 \fi
467 \fi
468 }
469 % defines \tikzexternallocked to be either 0 or 1, depending on the
470 % lock file.
471 \def\tikzexternalauxlock@getlockvalue{%
472 \openin\r@pgf@reada=\[email protected]
473 \ifeof\r@pgf@reada
474 % no lock file. Unlock.
475 \def\tikzexternallocked{0}%
476 \else
477 % read first line...
478 \read\r@pgf@reada to\pgf@tempa
479 % ... and execute it.
480 \pgf@tempa
481 % it should contain a definition of \tikzexternallocked. If
482 % not, lock it anyway.
483 \pgfutil@ifundefined
484 {tikzexternallocked}
485 {\def\tikzexternallocked{1}}%
486 {}%
487 \fi
488 \closein\r@pgf@reada
489 }%
490 \def\tikzexternalauxlock@setlock#1{%
491 \immediate\openout\w@pgf@writea=\[email protected]
492 \immediate\write\w@pgf@writea{\noexpand\def\noexpand\tikzexternallocked{#1}}%
493 \immediate\closeout\w@pgf@writea
494 }
495
496
497 % this is a hook into pgfcoreexternal.code.tex:
498 \def\pgf@external@grab@refundefinedtrue@code{%
499 \gdef\noexpand\tikzexternal@systemcall@reschedule@to@end@document{1}%
500 }%
501
502 % Expands to the default image extension (it is set by
503 % \tikzexternalize).
504 %
505 % This image extension may not resemble the correct one; you may need
506 % to overwrite this macro *after* \tikzexternalize in this case.
507 %
508 % The default setting uses the first registered image extension.
509 \def\tikzexternalimgextension{}%
510
511 % can be used to (temporarily) disable the externalization.
512 \def\tikzexternaldisable{%
513 \tikzexternaldisable@nofading
514 \pgfutil@ifundefined{tikzexternal@orig@tikzfadingfrompicture}
515 {}% NOP
516 {%
517 \let\tikzfadingfrompicture=\tikzexternal@orig@tikzfadingfrompicture
518 \let\endtikzfadingfrompicture=\tikzexternal@orig@endtikzfadingfrompicture
519 \let\tikzfading=\tikzexternal@orig@tikzfading
520 }%
521 }%
522 \def\tikzexternaldisable@nofading{%
523 \let\tikz=\tikzexternal@origtikz
524 \tikzexternal@TEXDIALECT@restore@picture
525 \iftikzexternal@optimize
526 \ifpgf@external@grabshipout
527 \tikzexternal@optimize@RESTORE
528 \fi
529 \fi
530 }
531 % re-enables externalization after a \tikzexternalizedisable.
532 \def\tikzexternalenable{%
533 \if\tikzexternalize@hasbeencalled1%
534 \let\tikz=\tikzexternal@tikz@replacement
535 \tikzexternal@TEXDIALECT@installreplacement@picture
536 \pgfutil@ifundefined{tikzfadingfrompicture}%
537 {}% no special handling for 'fading lib'
538 {%
539 % oh, ok -- patch fading lib!
540 \pgfutil@ifundefined{tikzexternal@orig@tikzfadingfrompicture}{%
541 \let\tikzexternal@orig@tikzfadingfrompicture=\tikzfadingfrompicture
542 \let\tikzexternal@orig@endtikzfadingfrompicture=\endtikzfadingfrompicture
543 \let\tikzexternal@orig@tikzfading=\tikzfading
544 }{}%
545 \def\tikzfadingfrompicture{%
546 \begingroup
547 \tikzexternaldisable@nofading
548 \tikzexternal@orig@tikzfadingfrompicture}%
549 \def\endtikzfadingfrompicture{%
550 \tikzexternal@orig@endtikzfadingfrompicture
551 \endgroup}%
552 \def\tikzfading[##1]{%
553 \begingroup
554 \tikzexternaldisable@nofading
555 \tikzexternal@orig@tikzfading[##1]%
556 \endgroup}%
557 }%
558 \iftikzexternal@optimize
559 \ifpgf@external@grabshipout
560 \tikzexternal@optimize@REPLACE
561 \fi
562 \fi
563 \fi
564 }%
565
566 % Sets the filename for the next tikzpicture or \tikz shortcommand.
567 %
568 % It will *only* be used for the next picture.
569 %
570 % If you don't call \tikzsetnextfilename for a picture, a file name
571 % will be assembled automatically.
572 %
573 % Please note that the global file prefix will be prepended anyway.
574 \def\tikzsetnextfilename#1{\gdef\tikzexternal@nextfile{#1}}
575 \tikzsetnextfilename{}
576
577
578 % These are few TeX dialect-specific commands which need to be overridden when used with
579 % something different than plain TeX:
580 %
581 % 1. this command collects a complete image into a macro, up to (but not including) the
582 % final TeX-dialect specific end-image command.
583 % This is really difficult for LaTeX because \end{tikzpicture} either involves dirty
584 % catcode-hacks to parse the braces or an ugly loop which loops until it
585 % finds \end followed by {tikzpicture}.
586 %
587 % #1: a macro which will be called with the collected environment contents.
588 % all following tokens: the environment contents.
589 \long\gdef\tikzexternal@TEXDIALECT@collectpicture#1#2\endtikzpicture{#1{#2}}
590 %
591 % 2. the following commands should expand to the TeX-dialect specific begin and end image command, i.e.
592 % \tikzpicture / \endtikzpicture
593 % or
594 % \begin{tikzpicture} / \end{tikzpicture}
595 % or
596 % \starttikzpicture / \stoptikzpicture
597 \def\tikzexternal@TEXDIALECT@begpicture{\tikzpicture}
598 \def\tikzexternal@TEXDIALECT@endpicture{\endtikzpicture}%
599 %
600 % 3. This installs the replacement macros:
601 \def\tikzexternal@TEXDIALECT@installreplacement@picture{%
602 \let\tikzpicture=\tikzexternal@tikzpicture@replacement
603 }%
604 \def\tikzexternal@TEXDIALECT@restore@picture{%
605 \let\tikzpicture=\tikzexternal@origpicture
606 }%
607
608
609
610
611 \let\tikzexternal@origtikz=\tikz
612 \let\tikzexternal@origpicture=\tikzpicture
613 \let\tikzexternal@origendpicture=\endtikzpicture
614 \let\tikzexternal@curfilename=\relax
615
616 % Replacement for '\tikzpicture'.
617 %
618 % There are two different states in which this method is applied:
619 % - normal typesetting. Then, the 'mode' key controls its behavior.
620 % - externalize mode (i.e. jobname != real job name). Then, it will
621 % externalize the picture selected by 'jobname' (and only this picture).
622 \def\tikzexternal@tikzpicture@replacement{%
623 \leavevmode
624 \global\tikzexternal@file@isuptodatetrue% may be set to false during checks.
625 \tikzexternal@getnextfilename\tikzexternal@curfilename
626 \ifx\tikzexternal@curfilename\pgfutil@empty
627 \ifpgf@external@grabshipout
628 % This picture won't be externalized.
629 % But maybe we can optimize it away:
630 \iftikzexternal@optimize
631 % -> it won't be shipped out anyway, so save typesetting time!
632 \let\tikzexternal@next=\tikzexternal@skipfigure
633 \else
634 % include this graphics into the output (even if
635 % it will be discarded anyway).
636 \let\tikzexternal@next=\tikzexternal@normalpicture@nographics
637 \fi
638 \else
639 \let\tikzexternal@next=\tikzexternal@normalpicture@nographics
640 \fi
641 \else
642 \ifpgf@external@grabshipout
643 \tikzifexternaljobnamematches\tikzexternal@curfilename{%
644 % ok, this here IS the picture for which
645 % pdflatex --jobname <my name>
646 % has been invoked.
647 \let\tikzexternal@next=\tikzexternal@externalizefig
648 }{%
649 \iftikzexternal@optimize
650 % No, another picture should be exported. Simply skip this one
651 % -> it won't be shipped out anyway, so save typesetting time!
652 \let\tikzexternal@next=\tikzexternal@skipfigure
653 \else
654 % include this graphics into the output (even it
655 % it will be discarded anyway).
656 \let\tikzexternal@next=\tikzexternal@normalpicture@nographics
657 \fi
658 }%
659 \else
660 \ifcase\tikzexternal@opmode\relax
661 % 'mode=only graphics'
662 \let\tikzexternal@next=\tikzexternal@forcegraphics
663 \or
664 % 'mode=no graphics'
665 \let\tikzexternal@next=\tikzexternal@normalpicture@nographics
666 \or
667 % 'mode=graphics if exists'
668 \let\tikzexternal@next=\tikzexternal@externalizefig
669 \or
670 % 'mode=list only'
671 \let\tikzexternal@next=\tikzexternal@listmodepicture
672 \or
673 % 'mode=convert with system call'
674 \let\tikzexternal@next=\tikzexternal@externalizefig@systemcall
675 \or
676 % 'mode=list and make'
677 \let\tikzexternal@next=\tikzexternal@list@and@makefile@mode@picture
678 \fi
679 \fi
680 \fi
681 \tikzexternal@next
682 }%
683
684 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
685 % Replacement for \tikz short command:
686 \def\tikzexternal@tikz@replacement{%
687 \pgfutil@ifnextchar[{\tikzexternal@tikz@replacement@opt}{\tikzexternal@tikz@replacement@opt[]}%
688 }%
689 \def\tikzexternal@tikz@replacement@opt[#1]{%
690 \pgfutil@ifnextchar\bgroup{\tikzexternal@tikz@replacement@opt@{#1}}{\tikzexternal@tikz@replacement@opt@@{#1}}%
691 }
692 \long\def\tikzexternal@tikz@replacement@opt@#1#2{%
693 \tikzexternal@tikz@replacement@opt@process{#1}{#2}%
694 }%
695 \def\tikzexternal@tikz@replacement@opt@@#1{%
696 \def\tikz@next{\tikzexternal@tikz@replacement@collectnormalsemicolon{#1}}%
697 \ifnum\the\catcode`\;=\active\relax%
698 \def\tikz@next{\tikzexternal@tikz@replacement@collectactivesemicolon{#1}}%
699 \fi%
700 \tikz@next}
701 \long\def\tikzexternal@tikz@replacement@collectnormalsemicolon#1#2;{%
702 \tikzexternal@tikz@replacement@opt@process{#1}{#2;}%
703 }%
704 {
705 \catcode`\;=\active
706 \long\gdef\tikzexternal@tikz@replacement@collectactivesemicolon#1#2;{%
707 \tikzexternal@tikz@replacement@opt@process{#1}{#2;}%
708 }
709 }
710 \long\def\tikzexternal@tikz@replacement@opt@process#1#2{%
711 \begingroup
712 \t@tikzexternal@tmpa=\expandafter{\tikzexternal@TEXDIALECT@begpicture[#1]#2}%
713 \t@tikzexternal@tmpb=\expandafter{\tikzexternal@TEXDIALECT@endpicture}%
714 \xdef\pgf@tempa{\the\t@tikzexternal@tmpa\the\t@tikzexternal@tmpb}%
715 \endgroup
716 \pgf@tempa
717 }
718 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
719
720 \def\tikzexternal@getnextfilename@advancecount{%
721 \begingroup
722 \c@pgf@counta=\csname c@tikzext@no@\tikzexternal@figurename\endcsname\relax
723 \advance\c@pgf@counta by1
724 \expandafter\xdef\csname c@tikzext@no@\tikzexternal@figurename\endcsname{\the\c@pgf@counta}%
725 \endgroup
726 }%
727
728 % Returns the file name which will be used for the next tikzpicture (based on the currently available information only, of course).
729 \def\tikzexternalgetnextfilename#1{%
730 \begingroup
731 \let\tikzexternal@getnextfilename@advancecount\relax% NOP
732 \let\tikzexternal@getnextfilename@resetglobals=\relax% NOP
733 \def\tikzexternal@protocol@to@file##1{}% NOP
734 \tikzexternal@getnextfilename#1%
735 \pgfmath@smuggleone#1%
736 \endgroup
737 }%
738
739 % Returns the file name of the current picture.
740 %
741 % It returns the empty string in case the command is invoked outside
742 % of a picture.
743 % #1 the macro which will contain the file name.
744 % @see \tikzexternalgetnextfilename
745 \def\tikzexternalgetcurrentfilename#1{%
746 \ifx\tikzexternal@curfilename\relax
747 % we are outside of a picture.
748 \let#1=\pgfutil@empty
749 \else
750 % we are inside of a picture.
751 \let#1=\tikzexternal@curfilename
752 \fi
753 }
754
755 % Fills #1 with a filename for the current picture.
756 %
757 % The filename will be generated automatically by appending '-figure<number>' to the real jobname.
758 %
759 % This method deals with
760 % - \tikzsetnextfilename
761 % - \tikzexternalexportnextfalse
762 %
763 % It returns #1={} if the current figure shall NOT be exported.
764 %
765 % Please note that both, \tikzsetnextfilename and \tikzexternalexportnextfalse
766 % affect only ONE picture. They will be reset afterwards.
767 \def\tikzexternal@getnextfilename#1{%
768 \let#1=\pgfutil@empty
769 % determine next file name:
770 \iftikzexternal@export@enabled
771 \iftikzexternalexportnext
772 \begingroup
773 \t@tikzexternal@tmpa=\expandafter{\tikzexternal@filenameprefix}%
774 \ifx\tikzexternal@nextfile\pgfutil@empty
775 \iftikzexternal@onlynamed
776 \xdef\pgf@tempa{}%
777 \else
778 \pgfkeysgetvalue{/tikz/external/figure name}\tikzexternal@figurename
779 \pgfutil@ifundefined{c@tikzext@no@\tikzexternal@figurename}{%
780 % initialise on first usage:
781 \expandafter\gdef\csname c@tikzext@no@\tikzexternal@figurename\endcsname{0}%
782 }{}%
783 \t@tikzexternal@tmpb=\expandafter{\tikzexternal@figurename}%
784 \xdef\pgf@tempa{\the\t@tikzexternal@tmpa\the\t@tikzexternal@tmpb\csname c@tikzext@no@\tikzexternal@figurename\endcsname}%
785 % advance the counter for 'figure name':
786 \tikzexternal@getnextfilename@advancecount
787 \fi
788 \else
789 \t@tikzexternal@tmpb=\expandafter{\tikzexternal@nextfile}%
790 \xdef\pgf@tempa{\the\t@tikzexternal@tmpa\the\t@tikzexternal@tmpb}%
791 \fi
792 \endgroup
793 \let#1=\pgf@tempa
794 \fi
795 \fi
796 %
797 \tikzexternal@getnextfilename@resetglobals%
798 %
799 \tikzexternal@protocol@to@file#1%
800 }%
801
802 \def\tikzexternal@getnextfilename@resetglobals{%
803 % Reset global flags:
804 \global\let\tikzexternal@nextfile=\pgfutil@empty
805 \global\tikzexternalexportnexttrue
806 }%
807
808 % #1: the image file name which should be protocolled. (can be a
809 % macro)
810 \def\tikzexternal@protocol@to@file#1{%
811 \ifpgf@external@grabshipout%
812 \else
813 \iftikzexternal@genfigurelist
814 \edef\tikzexternal@temp{#1}%
815 \ifx\tikzexternal@temp\pgfutil@empty
816 \else
817 \pgfutil@ifundefined{tikzexternal@listmode@openedfile}{%
818 \message{Opening '\[email protected]' for writing.}%
819 \begingroup
820 \globaldefs=1
821 % this gets round '\outer\newwrite' in plain TeX:
822 \csname newwrite\endcsname\tikzexternal@outfile
823 \endgroup
824 \immediate\openout\tikzexternal@outfile=\[email protected]\relax
825 \gdef\tikzexternal@listmode@openedfile{1}%
826 \if\tikzexternal@opmode5% mode='list and make'
827 \tikzexternal@list@and@make@prepare
828 \fi
829 }{}%
830 \iftikzexternal@verboseio
831 \pgf@typeout{Writing '#1' to '\[email protected]'.}%
832 \fi
833 \immediate\write\tikzexternal@outfile{#1}%
834 \if\tikzexternal@opmode5% mode='list and make'
835 \tikzexternal@list@and@make@gentarget{#1}%
836 \fi
837 \fi
838 \fi
839 \fi
840 }%
841
842 \def\tikzexternal@list@and@make@gentarget#1{%
843 \tikzexternal@assemble@systemcall{#1}{\pgf@tempa}%
844 \def\tikzexternal@tempb{\pgfutilstrreplace{^^J}{^^J\tikzexternal@TABchar}}%
845 \expandafter\tikzexternal@tempb\expandafter{\pgf@tempa}%
846 \let\pgf@tempa=\pgfretval
847 \iftikzexternal@verboseio
848 \pgf@typeout{Writing '#1' to '\[email protected]'.}%
849 \fi
850 \global\tikzexternal@file@isuptodatetrue% only check for force remake:
851 \tikzexternal@checkforceremake%
852 \immediate\write\tikzexternal@outmakefile{#1\tikzexternalimgextension: \iftikzexternal@file@isuptodate\else FORCEREMAKE\fi}%
853 \immediate\write\tikzexternal@outmakefile{\tikzexternal@TABchar\pgf@tempa}%
854 \immediate\write\tikzexternal@outmakefile{}%
855 }
856
857 \def\tikzexternal@list@and@make@prepare{%
858 \iftikzexternal@verboseio
859 \pgf@typeout{Opening '\[email protected]' for writing.}%
860 \fi
861 \begingroup
862 % this makes \tikzexternal@outmakefile global:
863 \globaldefs=1
864 % this gets round '\outer\newwrite' in plain TeX:
865 \csname newwrite\endcsname\tikzexternal@outmakefile
866 \endgroup
867 \immediate\openout\tikzexternal@outmakefile=\[email protected]\relax
868 \immediate\write\tikzexternal@outmakefile{ALL_FIGURE_NAMES=\tikzexternal@DOLLARchar(shell cat \[email protected])}%
869 \immediate\write\tikzexternal@outmakefile{ALL_FIGURES=\tikzexternal@DOLLARchar(ALL_FIGURE_NAMES:\tikzexternal@PERCENTchar=\tikzexternal@PERCENTchar\tikzexternalimgextension)}%
870 \immediate\write\tikzexternal@outmakefile{}%
871 \immediate\write\tikzexternal@outmakefile{allimages: \tikzexternal@DOLLARchar(ALL_FIGURES)}%
872 \immediate\write\tikzexternal@outmakefile{\tikzexternal@TABchar @echo All images exist now. Use make -B to re-generate them.}%
873 \immediate\write\tikzexternal@outmakefile{}%
874 \immediate\write\tikzexternal@outmakefile{FORCEREMAKE:}%
875 \immediate\write\tikzexternal@outmakefile{}%
876 %
877 % support for .dep files and auto-dependencies:
878 \immediate\write\tikzexternal@outmakefile{-include $(ALL_FIGURE_NAMES:\tikzexternal@PERCENTchar=\tikzexternal@PERCENTchar.\tikzexternaldepext)}%
879 \immediate\write\tikzexternal@outmakefile{}%
880 \tikzexternalmakefiledefaultdeprule
881 \immediate\write\tikzexternal@outmakefile{}%
882 \tikzexternal@outmakefile@pendingcommands
883 \pgfutil@ifundefined{AtEndDocument}{}{%
884 \AtEndDocument{\pgf@typeout{===== mode=`list and make': Use 'make -f \[email protected]' to generate all images. Then, re-run (pdf)latex \tikzexternal@realjob. =====}}%
885 }%
886 }%
887 \def\tikzexternaldepext{dep}
888
889 \def\tikzexternalmakefiledefaultdeprule{%
890 \immediate\write\tikzexternal@outmakefile{\tikzexternal@PERCENTchar.\tikzexternaldepext:}%
891 \immediate\write\tikzexternal@outmakefile{\tikzexternal@TABchar mkdir -p \tikzexternal@normal@dq $(dir $@)\tikzexternal@normal@dq}%
892 \immediate\write\tikzexternal@outmakefile{\tikzexternal@TABchar touch \tikzexternal@normal@dq $@\tikzexternal@normal@dq\space \tikzexternal@HASHchar\space will be filled later.}%
893 }%
894
895 % Invokes '#1' if a makefile is to be written and '#2' if not.
896 \def\tikzexternalifwritesmakefile#1#2{%
897 \if5\tikzexternal@opmode #1\else #2\fi
898 }%
899 \def\tikzexternal@outmakefile@pendingcommands{}%
900
901 % Will write something to the make file. If the makefile is not yet
902 % opened, #1 will be written as soon as it *is* opened.
903 \def\tikzexternalwritetomakefile#1{%
904 \if\tikzexternal@opmode5% mode='list and make'
905 \pgfutil@ifundefined{tikzexternal@outmakefile}{%
906 \expandafter\gdef\expandafter\tikzexternal@outmakefile@pendingcommands\expandafter{%
907 \tikzexternal@outmakefile@pendingcommands
908 \immediate\write\tikzexternal@outmakefile{#1}%
909 }%
910 }{%
911 \immediate\write\tikzexternal@outmakefile{#1}%
912 }%
913 \fi
914 }%
915
916 \def\tikzexternal@dep@file@name{}
917
918 % Adds a dependency for the externalized picture file name `#1',
919 % namely another file `#2'.
920 %
921 % #1: the fully qualified path name (without image extension) of the
922 % external graphics for which we are adding a dependency.
923 % #2: a file name. If this file gets changed, #1 shall be remade.
924 %
925 % This command will be activated as soon as \tikzexternalize has been
926 % called.
927 \def\tikzexternalfiledependsonfile@ACTIVE#1#2{%
928 \begingroup
929 \def\tikzpicturedependsonfile@name{#1}%
930 \ifx\tikzpicturedependsonfile@name\pgfutil@empty
931 % could be auto-generated: empty file name means "picture
932 % won't be externalized". Skip it.
933 \else
934 % if we encounter any dependencies while we externalize a
935 % picture, we have to write these things into the image's .dep
936 % file.
937 \tikzifexternalizingcurrent
938 {\tikzexternalfiledependsonfile@append@to@dep@file{#1}{#2}}%
939 {\tikzexternalfiledependsonfile@append@to@makefile{#1}{#2}}%
940 \fi
941 \endgroup
942 }%
943 \def\tikzexternalfiledependsonfile#1#2{}% NO-OP until \tikzexternalize has been called.
944
945 % sub-routine of \tikzexternalfiledependsonfile which appends stuff to
946 % the pictures .dep file.
947 %
948 % The file #1.dep is generated during the externalization of #1. If
949 % you change and/or overwrite it in any other context, its information
950 % might get lost (because #1 won't be regenerated).
951 %
952 % Consequently, this here has to be called if and only if #1 is about
953 % to be externalized.
954 \def\tikzexternalfiledependsonfile@append@to@dep@file#1#2{%
955 % write every dependency which is defined *INSIDE* of the current
956 % picture into the .dep file of the current picture:
957 \tikzexternalgetcurrentfilename\tikzpicturedependsonfile@name
958 \edef\tikzpicturedependsonfile@name{\tikzpicturedependsonfile@name.\tikzexternaldepext}%
959 %\edef\tikzpicturedependsonfile@name{#1.\tikzexternaldepext}%
960 \ifx\tikzexternal@dep@file@name\tikzpicturedependsonfile@name
961 % file is open and ready.
962 \else
963 % create new output file.
964 \pgfutil@ifundefined{tikzexternal@dep@file}{%
965 \begingroup
966 % this makes \tikzexternal@dep@file global:
967 \globaldefs=1
968 % this gets round '\outer\newwrite' in plain TeX:
969 \csname newwrite\endcsname\tikzexternal@dep@file
970 \endgroup
971 }{}%
972 \ifx\tikzexternal@dep@file@name\pgfutil@empty
973 \else
974 % it is already open. Close it.
975 \immediate\closeout\tikzexternal@dep@file
976 \fi
977 \global\let\tikzexternal@dep@file@name=\tikzpicturedependsonfile@name
978 \immediate\openout\tikzexternal@dep@file=\tikzexternal@dep@file@name\relax
979 \fi
980 \immediate\write\tikzexternal@dep@file{#1\tikzexternalimgextension: #2}%
981 }
982
983 % sub-routine of \tikzexternalfiledependsonfile which appends stuff to
984 % the makefile of the current \jobname .
985 %
986 % This is used for dependencies which are recomputed by every run of
987 % tex; we must not write them into #1.dep (for reasons explained in
988 % the documentation of
989 % \tikzexternalfiledependsonfile@append@to@dep@file)
990 \def\tikzexternalfiledependsonfile@append@to@makefile#1#2{%
991 \edef\tikzexternal@temp{#1\tikzexternalimgextension: #2}%
992 \expandafter\tikzexternalwritetomakefile\expandafter{\tikzexternal@temp}%
993 }
994
995 % Adds a dependency for the NEXT picture which is about to be
996 % externalized. If we are currently inside of a picture, the
997 % dependency is added for this current picture.
998 %
999 % #1: a file name. If this file gets changed, the tikzpicture's
1000 % externalized graphics shall be remade.
1001 %
1002 % This command will be activated as soon as \tikzexternalize has been
1003 % called.
1004 \def\tikzpicturedependsonfile@ACTIVE#1{%
1005 \begingroup
1006 \tikzexternalgetcurrentfilename\tikzpicturedependsonfile@name
1007 \ifx\tikzpicturedependsonfile@name\pgfutil@empty
1008 \tikzexternalgetnextfilename\tikzpicturedependsonfile@name
1009 \fi
1010 \expandafter\tikzexternalfiledependsonfile\expandafter{\tikzpicturedependsonfile@name}{#1}%
1011 \endgroup
1012 }%
1013 \def\tikzpicturedependsonfile#1{}% NO-OP until \tikzexternalize has been called.
1014
1015
1016 % This command is invoked
1017 % if and only if ( (grab mode && --jobname matches) || 'mode=graphics if exists' )
1018 %
1019 % It converts the current tikzpicture into an image in grab mode
1020 % or processes the 'graphics if exist' mode.
1021 \def\tikzexternal@externalizefig{%
1022 \ifpgf@external@grabshipout%
1023 % In this case, we already KNOW that the filename matches.
1024 \expandafter\tikzexternal@externalizefig@GRAB%
1025 \else
1026 \expandafter\tikzexternal@externalizefig@mode@graphics@if@exists
1027 \fi%
1028 }
1029 \def\tikzexternal@externalizefig@mode@graphics@if@exists{%
1030 % check if there is already a file.
1031 % In that case, use it. If not, typeset the picture normally.
1032 \gdef\pgf@filename{}%
1033 \xdef\pgf@tempa{\noexpand\pgf@findfile\pgfsys@imagesuffixlist:+{\tikzexternal@curfilename}}%
1034 \pgf@tempa
1035 \ifx\pgf@filename\pgfutil@empty%
1036 % Note: since we have no 'GRAB' mode, we do not have to deal with optimization.
1037 % there is no graphics.
1038 \expandafter\tikzexternal@normalpicture@nographics
1039 \else
1040 \expandafter\tikzexternal@forcegraphics
1041 \fi
1042 }%
1043
1044
1045 % Used by the optimization code.
1046 % It will be called if GRAB mode is on.
1047 % See the 'optimize command away' key.
1048 \def\tikzexternal@optimize@away@cmd#1#2{%
1049 \pgfutil@ifnextchar[{%
1050 \tikzexternal@optimize@away@cmd@{#1}{#2}%
1051 }{%
1052 \tikzexternal@optimize@away@cmd@{#1}{#2}[]%
1053 }%
1054 }%
1055 \def\tikzexternal@optimize@away@cmd@#1#2[#3]{%
1056 \def\tikz@temp{#2}%
1057 \ifx\tikz@temp\pgfutil@empty
1058 \def\tikz@temp{\tikzexternal@optimize@away@cmd@auto{#1}{#3}}%
1059 \else
1060 \ifcase#2\relax
1061 \def\tikzexternal@optimize@away@cmd@manual{%
1062 \tikzexternal@optimize@away@cmd@@@{#1}{#3}{}%
1063 }%
1064 \or
1065 \def\tikzexternal@optimize@away@cmd@manual##1{%
1066 \tikzexternal@optimize@away@cmd@@@{#1}{#3}{{##1}}%
1067 }%
1068 \or
1069 \def\tikzexternal@optimize@away@cmd@manual##1##2{%
1070 \tikzexternal@optimize@away@cmd@@@{#1}{#3}{{##1}{##2}}%
1071 }%
1072 \or
1073 \def\tikzexternal@optimize@away@cmd@manual##1##2##3{%
1074 \tikzexternal@optimize@away@cmd@@@{#1}{#3}{{##1}{##2}{##3}}%
1075 }%
1076 \or
1077 \def\tikzexternal@optimize@away@cmd@manual##1##2##3##4{%
1078 \tikzexternal@optimize@away@cmd@@@{#1}{#3}{{##1}{##2}{##3}{##4}}%
1079 }%
1080 \or
1081 \def\tikzexternal@optimize@away@cmd@manual##1##2##3##4##5{%
1082 \tikzexternal@optimize@away@cmd@@@{#1}{#3}{{##1}{##2}{##3}{##4}{##5}}%
1083 }%
1084 \or
1085 \def\tikzexternal@optimize@away@cmd@manual##1##2##3##4##5##6{%
1086 \tikzexternal@optimize@away@cmd@@@{#1}{#3}{{##1}{##2}{##3}{##4}{##5}{##6}}%
1087 }%
1088 \or
1089 \def\tikzexternal@optimize@away@cmd@manual##1##2##3##4##5##6##7{%
1090 \tikzexternal@optimize@away@cmd@@@{#1}{#3}{{##1}{##2}{##3}{##4}{##5}{##6}{##7}}%
1091 }%
1092 \or
1093 \def\tikzexternal@optimize@away@cmd@manual##1##2##3##4##5##6##7##8{%
1094 \tikzexternal@optimize@away@cmd@@@{#1}{#3}{{##1}{##2}{##3}{##4}{##5}{##6}{##7}{##8}}%
1095 }%
1096 \or
1097 \def\tikzexternal@optimize@away@cmd@manual##1##2##3##4##5##6##7##8##9{%
1098 \tikzexternal@optimize@away@cmd@@@{#1}{#3}{{##1}{##2}{##3}{##4}{##5}{##6}{##7}{##8}{##9}}%
1099 }%
1100 \fi
1101 \def\tikz@temp{\tikzexternal@optimize@away@cmd@manual}%
1102 \fi
1103 \tikz@temp
1104 }%
1105 \def\tikzexternal@optimize@away@cmd@auto#1#2{%
1106 \pgfutil@ifnextchar\bgroup{%
1107 \tikzexternal@optimize@away@cmd@auto@{#1}{#2}%
1108 }{%
1109 \tikzexternal@optimize@away@cmd@auto@{#1}{#2}{<no argument in curly braces>}% give empty argument.
1110 }
1111 }%
1112
1113 \def\tikzexternal@optimize@away@cmd@auto@#1#2#3{%
1114 \tikzexternal@optimize@away@cmd@@@{#1}{#2}{{#3}}% <-- provide braces
1115 }%
1116 \def\tikzexternal@optimize@away@cmd@@@#1#2#3{%
1117 \begingroup
1118 \toks0={#1[#2]#3}%
1119 \iftikzexternal@verbose@optimize
1120 \pgf@typeout{The command '\the\toks0' has been optimized away. Use '/tikz/external/optimize=false' to disable this.}%
1121 \fi
1122 \endgroup
1123 \begingroup
1124 \pgfkeysvalueof{/tikz/external/optimize away text/.@cmd}#1\pgfeov%
1125 \endgroup
1126 }%
1127
1128 \def\tikzexternal@optimize@away@latex@env#1{%
1129 \def\tikzexternal@optimize@away@latex@env@{#1}%
1130 \begingroup
1131 \def\tikzexternal@laTeX@collect@until@end@tikzpicturetikzpicturestring{#1}%
1132 \tikzexternal@TEXDIALECT@collectpicture\tikzexternal@optimize@away@latex@env@close
1133 }%
1134 \long\def\tikzexternal@optimize@away@latex@env@close#1{%
1135 \iftikzexternal@verbose@optimize
1136 \pgf@typeout{The complete contents of \string\begin{tikzexternal@optimize@away@latex@env@} up to the next \end{tikzexternal@optimize@away@latex@env@} has been optimized away because it does not contribute to the exported PDF. Use '/tikz/external/optimize=false' to disable this.}%
1137 \fi
1138 \endgroup
1139 % we still need to invoke \end{<name>} in latex because \begin{<name>}
1140 % starts a local group - that must be closed properly. Make sure then \end<name> does nothing:
1141 \expandafter\let\csname end\tikzexternal@optimize@away@latex@env@\endcsname=\relax
1142 \expandafter\end\expandafter{\tikzexternal@optimize@away@latex@env@}%
1143 }%
1144
1145 \def\tikzexternal@optimize@REPLACE{%
1146 \pgfkeysvalueof{/tikz/external/optimize/install/.@cmd}\pgfeov
1147 }%
1148
1149 \def\tikzexternal@optimize@RESTORE{%
1150 \pgfkeysvalueof{/tikz/external/optimize/restore/.@cmd}\pgfeov
1151 }%
1152
1153 % Closes one 'tikzpicture' environment. This is only used for LaTeX,
1154 % because the '\end{tikzpicture}' command would raise an exception otherwise.
1155 % It does *not* call \endtikzpicture.
1156 \def\tikzexternal@closeenvironments{%
1157 \let\endtikzpicture=\relax
1158 \tikzexternal@TEXDIALECT@endpicture
1159 \let\endtikzpicture=\tikzexternal@origendpicture
1160 \let\tikzexternal@curfilename=\relax
1161 }
1162
1163 % Throws a tikzpicture away - without further notice.
1164 % Used if we are currently converting *another* picture. No need to waste time
1165 % with expensive pictures if they are not shipped out anyway.
1166 %
1167 % See the 'optimize' key to disable this.
1168 \def\tikzexternal@skipfigure{%
1169 \tikzexternal@TEXDIALECT@collectpicture\tikzexternal@skipfigure@@
1170 }
1171 \long\def\tikzexternal@skipfigure@@#1{%
1172 \iftikzexternal@verbose@optimize
1173 \pgf@typeout{A tikzpicture has been optimized away. Use '/tikz/external/optimize=false' to disable this.}%
1174 \fi
1175 \tikzexternal@closeenvironments
1176 \pgfkeysvalueof{/tikz/external/optimize away text/.@cmd}tikzpicture\pgfeov%
1177 }
1178
1179 % Processes tikzpicture normally; without any externalization.
1180 %
1181 % We need to do further work here to deal with NESTED tikzpicture environments
1182 % because all of them shall also be typeset normally.
1183 %
1184 % Idea:
1185 % 1. restore the original \tikzpicture macro
1186 % 2. install the replacement \tikzpicture in \end{tikzpicture}.
1187 % and keep track of nesting.
1188 %
1189 % FIXME: could it be possible that nested tikzpictures use other code anyway?
1190 \def\tikzexternal@normalpicture@nographics{%
1191 \tikzexternal@nestedflagfalse
1192 \let\tikzpicture=\tikzexternal@normalpicture@begreplace
1193 \let\endtikzpicture=\tikzexternal@normalpicture@endreplace
1194 \tikzpicture
1195 }
1196 \def\tikzexternal@normalpicture@begreplace{%
1197 \begingroup
1198 \tikzexternal@nestedflagtrue
1199 \tikzexternal@origpicture
1200 }
1201 \def\tikzexternal@normalpicture@endreplace{%
1202 \tikzexternal@origendpicture
1203 \endgroup
1204 \iftikzexternal@nestedflag
1205 \else
1206 \let\tikzpicture=\tikzexternal@tikzpicture@replacement
1207 \let\endtikzpicture=\tikzexternal@origendpicture
1208 \fi
1209 }%
1210
1211 % Assumes there is an image on disk and reads it. The tikzpicture is thrown away.
1212 \def\tikzexternal@forcegraphics{%
1213 \tikzexternal@TEXDIALECT@collectpicture\tikzexternal@forcegraphics@@
1214 }
1215 \long\def\tikzexternal@forcegraphics@@#1{\tikzexternal@forcegraphics@@@}%
1216 \def\tikzexternal@forcegraphics@@@{%
1217 \if5\tikzexternal@opmode
1218 \let\tikz@refundefinedtrue@@=\G@refundefinedtrue
1219 \gdef\G@refundefinedtrue{%
1220 \tikz@refundefinedtrue@@
1221 \tikzexternal@forceremake@undefined@reference@handler
1222 }%
1223 \fi
1224 \expandafter\pgfincludeexternalgraphics\expandafter{\tikzexternal@curfilename}%
1225 \if5\tikzexternal@opmode
1226 \global\let\G@refundefinedtrue=\tikz@refundefinedtrue@@
1227 \fi
1228 \tikzexternal@closeenvironments
1229 }
1230
1231 \def\tikzexternal@forceremake@undefined@reference@handler{%
1232 \pgf@typeout{===== 'mode=list and make': encountered undefined reference in current picture. Adding dependency to FORCEREMAKE. Rerun make to update the picture.' ========^^J}%
1233 \tikzpicturedependsonfile{FORCEREMAKE}%
1234 }%
1235
1236 % Simply replaces the complete picture by some placeholder text.
1237 % It is used by 'mode=list only' to reduce runtime.
1238 \def\tikzexternal@listmodepicture{%
1239 \tikzexternal@TEXDIALECT@collectpicture\tikzexternal@listmodepicture@@
1240 }
1241 \long\def\tikzexternal@listmodepicture@@#1{\tikzexternal@listmodepicture@@@}
1242 \def\tikzexternal@listmodepicture@@@{%
1243 \pgfkeysvalueof{/tikz/external/image discarded text}%
1244 \tikzexternal@closeenvironments
1245 }
1246
1247 % check if there is already a file.
1248 % In that case, use it. If not, skip it.
1249 \def\tikzexternal@list@and@makefile@mode@picture{%
1250 \tikzexternal@TEXDIALECT@collectpicture\tikzexternal@list@and@makefile@mode@picture@
1251 }%
1252 \long\def\tikzexternal@list@and@makefile@mode@picture@#1{%
1253 %
1254 \tikzexternal@check@uptodate@mode{#1}%
1255 %
1256 \gdef\pgf@filename{}%
1257 \xdef\pgf@tempa{\noexpand\pgf@findfile\pgfsys@imagesuffixlist:+{\tikzexternal@curfilename}}%
1258 \pgf@tempa
1259 \ifx\pgf@filename\pgfutil@empty%
1260 % Note: since we have no 'GRAB' mode, we do not have to deal with optimization.
1261 % there is no graphics.
1262 \expandafter\tikzexternal@listmodepicture@@@
1263 \else
1264 \expandafter\tikzexternal@forcegraphics@@@
1265 \fi
1266 }%
1267
1268 \def\tikzexternal@externalizefig@before@grab{%
1269 \pgfutil@IfUndefined{TP@holdbox}{%
1270 }{%
1271 % \usepackage[absolute]{textpos}
1272 % populates this box -- and would inject unwanted material into our images. Reset it:
1273 \global\setbox\TP@holdbox\vbox{}%
1274 }%
1275 }%
1276
1277 \def\tikzexternal@externalizefig@GRAB{%
1278 \ifpgf@external@grabshipout
1279 \iftikzexternal@optimize
1280 \tikzexternal@optimize@RESTORE
1281 \fi
1282 \tikzexternal@externalizefig@before@grab
1283 \fi
1284 \def\tikzpicture{%
1285 \def\tikzpicture{% make sure that nested \tikzpicture are processed normally.
1286 \begingroup
1287 \def\endtikzpicture{%
1288 \tikzexternal@origendpicture
1289 \endgroup
1290 }%
1291 \tikzexternal@origpicture
1292 }%
1293 \pgf@external@grab{\tikzexternal@curfilename}%
1294 \tikzexternal@origpicture
1295 }%
1296 \def\endtikzpicture{%
1297 \tikzexternal@origendpicture
1298 \pgf@externalend
1299 %
1300 \tikzexternal@ensure@nonempty@floats
1301 %
1302 \let\tikzpicture=\tikzexternal@tikzpicture@replacement
1303 \let\endtikzpicture=\tikzexternal@origendpicture
1304 \iftikzexternal@optimize
1305 \ifpgf@external@grabshipout
1306 \tikzexternal@optimize@REPLACE
1307 \fi
1308 \fi
1309 }%
1310 \tikzpicture
1311 }%
1312
1313 % If a sequence of floats containing JUST tikzpictures is
1314 % externalized, this results in EMPTY floats. Empty floats, in turn,
1315 % can confuse latex; it thinks it did something wrong.
1316 %
1317 % Solution: Avoid empty floats by writing junk into it.
1318 %
1319 % Note that this method is only invoked if \ifpgf@external@grabshipout
1320 % is true and if the text is OUTSIDE of the original shipout routine.
1321 % In other words: this text is being thrown away.
1322 %
1323 % see
1324 % http://tex.stackexchange.com/questions/54625/why-is-fixltx2e-incompatible-with-tikzexternalize
1325 \def\tikzexternal@ensure@nonempty@floats{%
1326 tikzexternal: picture has been externalized. This text is required to avoid empty floats.
1327 }%
1328
1329 % 1. Discards the current picture in this document.
1330 % 2. Checks whether an image exists already. If that is the case: acquire it.
1331 % 3. If no image exists: call '/tikz/external/system call'. This will
1332 % process \tikzexternal@externalizefig.
1333 % 4. Assert that finally an image exists and use it.
1334 \def\tikzexternal@externalizefig@systemcall{%
1335 \tikzexternal@TEXDIALECT@collectpicture\tikzexternal@externalizefig@systemcall@@
1336 }
1337
1338 {
1339 \catcode`\"=12
1340 \catcode`\'=12
1341 \catcode`\;=12
1342 \catcode`\&=12
1343 \catcode`\-=12
1344 \xdef\tikzexternal@normal@dq{"}
1345 \xdef\tikzexternal@normal@sq{'}
1346 \xdef\tikzexternal@normal@semic{;}
1347 \xdef\tikzexternal@normal@and{&}
1348 \xdef\tikzexternal@normal@dash{-}
1349 \catcode`\"=13
1350 \catcode`\'=13
1351 \catcode`\;=13
1352 \catcode`\&=13
1353 \catcode`\-=13
1354 \gdef\tikzexternal@activate@normal@dq{\let"=\tikzexternal@normal@dq}
1355 \gdef\tikzexternal@activate@normal@sq{\let'=\tikzexternal@normal@sq}
1356 \gdef\tikzexternal@activate@normal@semic{\let;=\tikzexternal@normal@semic}
1357 \gdef\tikzexternal@activate@normal@and{\let&=\tikzexternal@normal@and}
1358 \gdef\tikzexternal@activate@normal@dash{\let-=\tikzexternal@normal@dash}
1359 \catcode`\|=0
1360 \catcode`\\=12
1361 |xdef|tikzexternal@normal@backslash{\}%
1362 }
1363 {
1364 \catcode`\^^I=12
1365 \catcode`\$=12
1366 \catcode`\%=12
1367 \catcode`\#=12
1368 \gdef\tikzexternal@HASHchar{#}
1369 \gdef\tikzexternal@TABchar{^^I}\gdef\tikzexternal@PERCENTchar{%}\xdef\tikzexternal@DOLLARchar{$}}
1370
1371 % Creates the '/tikz/external/system call' command as string and
1372 % returns it into the (global!) macro #2.
1373 % #1: the image file name (as returned by
1374 % \tikzexternalgetnextfilename)
1375 % #2: the global return value macro
1376 %
1377 \def\tikzexternal@assemble@systemcall#1#2{%
1378 \begingroup
1379 \def\image{#1}%
1380 \iftikzexternal@auto@detect@jobname
1381 \edef\texsource{\string\def\string\tikzexternalrealjob{\tikzexternal@realjob}\string\input{\tikzexternal@realjob}}%
1382 \else
1383 \let\texsource=\tikzexternal@realjob
1384 \fi
1385 \ifnum\the\catcode`\"=13 \tikzexternal@activate@normal@dq\fi
1386 \ifnum\the\catcode`\'=13 \tikzexternal@activate@normal@sq\fi
1387 \ifnum\the\catcode`\;=13 \tikzexternal@activate@normal@semic\fi
1388 \ifnum\the\catcode`\-=13 \tikzexternal@activate@normal@dash\fi
1389 \let\\=\tikzexternal@normal@backslash
1390 \xdef#2{\pgfkeysvalueof{/tikz/external/system call}}%
1391 \endgroup
1392 }%
1393 \long\def\tikzexternal@externalizefig@systemcall@@#1{%
1394 \tikzexternal@externalizefig@systemcall@uptodatecheck{#1}%
1395 \tikzexternal@assemble@systemcall{\tikzexternal@curfilename}{\pgf@tempa}%
1396 \global\let\tikzexternal@cursyscall=\pgf@tempa
1397 \gdef\tikzexternal@typeset@picture@on@failure{1}%
1398 \iftikzexternal@file@isuptodate
1399 \iftikzexternal@verboseio
1400 \pgf@typeout{===== Image '\tikzexternal@curfilename' is up-to-date. ======}%
1401 \fi
1402 \let\pgf@filename=\tikzexternal@curfilename
1403 \else
1404 \begingroup
1405 % no such image. Generate it!
1406 %
1407 \tikzexternalauxlock@setlock1%
1408 \tikzexternal@externalizefig@systemcall@call{\tikzexternal@cursyscall}%
1409 \tikzexternalauxlock@setlock0%
1410 \expandafter\tikzexternal@externalizefig@systemcall@assertsuccess\expandafter{\tikzexternal@cursyscall}%
1411 \pgfmath@smuggleone\pgf@filename
1412 \endgroup
1413 \fi
1414 \ifx\pgf@filename\pgfutil@empty
1415 \if1\tikzexternal@typeset@picture@on@failure
1416 % error recovery: something did not work! Try to load it
1417 % anyway. Perhaps it was just that shell-escape wasn't
1418 % enabled.
1419 \begingroup
1420 \toks0={%
1421 \tikzexternaldisable
1422 }%
1423 % FIXME : THIS WILL FAIL IF THERE IS '##' INSIDE OF '#1'!
1424 % for example something like /.style={#1} in the picture environment *will* fail.
1425 \toks1={%
1426 \tikzpicture
1427 #1%
1428 }%
1429 \toks2={%
1430 \tikzexternal@TEXDIALECT@endpicture
1431 \tikzexternalenable
1432 }%
1433 \xdef\tikzexternal@externalizefig@systemcall@next{%
1434 \the\toks0
1435 % try reading them again as if they were in the input file.
1436 \noexpand\scantokens{\the\toks1 }%
1437 \the\toks2
1438 }%
1439 \endgroup
1440 \else
1441 % Ah -- this picture failed, but the failure has already
1442 % been handled somehow. Do not typeset it again!
1443 %
1444 % A use-case is that the picture has been rescheduled for
1445 % \end{document}.
1446 \gdef\tikzexternal@externalizefig@systemcall@next{\tikzexternal@closeenvironments}%
1447 \fi
1448 \else
1449 % ok, take the image!
1450 \gdef\tikzexternal@systemcall@reschedule@to@end@document{0}%
1451 \expandafter\pgfincludeexternalgraphics\expandafter{\tikzexternal@curfilename}%
1452 \if1\tikzexternal@systemcall@reschedule@to@end@document
1453 \pgfutil@IfUndefined{AtVeryEndDocument}{%
1454 }{%
1455 \pgf@typeout{===== The previous system call resulted in undefined references inside of the output file. Rescheduling it for \string\end{document}. ========}%
1456 \expandafter\tikzexternal@externalizefig@systemcall@reschedule\expandafter{\tikzexternal@cursyscall}%
1457 }%
1458 \fi
1459 \gdef\tikzexternal@externalizefig@systemcall@next{\tikzexternal@closeenvironments}%
1460 \fi
1461 \tikzexternal@externalizefig@systemcall@next
1462 }%
1463
1464 \def\tikzexternal@externalizefig@systemcall@call#1{%
1465 \iftikzexternal@verboseio
1466 \pgf@typeout{===== 'mode=convert with system call': Invoking '#1' ========}%
1467 \fi
1468 \pgfutil@shellescape{#1}%
1469 }%
1470
1471 % Sets \iftikzexternal@file@isuptodate to false if one of the "force
1472 % remake" things is active.
1473 \def\tikzexternal@checkforceremake{%
1474 \iftikzexternal@force@remake
1475 \global\tikzexternal@file@isuptodatefalse
1476 \else
1477 \iftikzexternalremakenext
1478 \global\tikzexternal@file@isuptodatefalse
1479 \global\tikzexternalremakenextfalse
1480 \fi
1481 \fi
1482 }
1483
1484 \pgfutil@IfUndefined{pdfmdfivesum}{%
1485 }{%
1486 % predefine to this value. This does only make sense for pdftex.
1487 % note that the latex library for 'external' loads \usepackage{pdftexcmds} which \let's this to \pdf@mdfivesum:
1488 \let\tikzexternal@mdfivesum=\pdfmdfivesum
1489 }%
1490
1491 \long\def\tikzexternal@computemdfivesum#1{%
1492 \t@tikzexternal@tmpb={#1}%
1493 \edef\tikzexternal@temp{\the\t@tikzexternal@tmpb}%
1494 % \meaning results in a string of catcode 12 - which is expandable.
1495 \edef\pgfretval{\tikzexternal@mdfivesum{\meaning\tikzexternal@temp}}%
1496 }
1497 \long\def\tikzexternal@computemdfivesum@diff@fallback#1{%
1498 \t@tikzexternal@tmpb={#1}%
1499 \edef\tikzexternal@temp{\the\t@tikzexternal@tmpb}%
1500 % \meaning results in a string of catcode 12 - which is expandable.
1501 \edef\pgfretval{\meaning\tikzexternal@temp}%
1502 }
1503
1504 % Will be set dynamically, depending on 'up to date check'.
1505 %
1506 % It expands to code such that \tikzexternallastkey contains the
1507 % serialized version of '#1'.
1508 %
1509 % It is used as argument for \write{..}
1510 %
1511 % #1 the hash key to serialize.
1512 % \def\tikzexternal@hashfct@serialize
1513 \long\def\tikzexternal@hashfct@serialize@std#1{%
1514 \noexpand\def\noexpand\tikzexternallastkey{#1}%
1515 }%
1516
1517 % serializes using temporary token registers. Necessary if #1 contains
1518 % executable code.
1519 \long\def\tikzexternal@hashfct@serialize@tok#1{%
1520 \noexpand\begingroup
1521 % in order to allow '#' inside of the body, we have to use token registers:
1522 \noexpand\toks0={#1}%
1523 \noexpand\xdef\noexpand\tikzexternallastkey{\noexpand\the\noexpand\toks0 }%
1524 \noexpand\endgroup
1525 }%
1526
1527 \def\tikzexternal@check@uptodate@ext{.md5}
1528
1529 \def\tikzexternal@check@uptodate@mode@warn@fallback{%
1530 \message{Package tikz Warning: The key 'up to date check=md5' is impossible, there is no macro to compute MD5. Falling back to 'up to date check=diff'.}%
1531 %
1532 % warn only once:
1533 \global\let\tikzexternal@check@uptodate@mode@warn@fallback=\relax
1534 }%
1535
1536 % assigns the boolean \iftikzexternal@file@isuptodate
1537 \long\def\tikzexternal@check@uptodate@mode#1{%
1538 \if1\tikzexternal@uptodate@mode
1539 % up to date check=md5:
1540 % check if we CAN compute MD5 sums:
1541 \pgfutil@IfUndefined{tikzexternal@mdfivesum}{%
1542 % we cannot compute md5 sums - fallback to diff.
1543 \tikzexternal@check@uptodate@mode@warn@fallback
1544 \def\tikzexternal@uptodate@mode{2}%
1545 }{}%
1546 \fi
1547 %
1548 \ifcase\tikzexternal@uptodate@mode
1549 % up to date check=simple
1550 % nothing to do -- file existence is checked anyway.
1551 \let\tikzexternal@hashfct=\pgfutil@empty
1552 \let\tikzexternal@hashfct@serialize=\pgfutil@empty
1553 \or
1554 % up to date check=md5
1555 \let\tikzexternal@hashfct=\tikzexternal@computemdfivesum
1556 \let\tikzexternal@hashfct@serialize=\tikzexternal@hashfct@serialize@std
1557 \or
1558 \let\tikzexternal@hashfct=\tikzexternal@computemdfivesum@diff@fallback
1559 \let\tikzexternal@hashfct@serialize=\tikzexternal@hashfct@serialize@tok
1560 \fi
1561 \ifx\tikzexternal@hashfct\pgfutil@empty
1562 \else
1563 \tikzpicturedependsonfile{\tikzexternal@curfilename\tikzexternal@check@uptodate@ext}%
1564 \tikzexternal@hashfct{#1}%
1565 \let\tikzexternal@lastkey@new=\pgfretval
1566 \begingroup
1567 % no '@' token in this macro: avoid messing up the catcodes in input files:
1568 \global\let\tikzexternallastkey=\pgfutil@empty
1569 \openin\r@pgf@reada=\tikzexternal@curfilename\tikzexternal@check@uptodate@ext\relax %
1570 \ifeof\r@pgf@reada
1571 \else
1572 % read first line...
1573 \endlinechar=-1 % older versions did not have a '%' at the end-of-line. Avoid spurious spaces.
1574 \read\r@pgf@reada to\pgf@tempa
1575 % ... and execute it.
1576 \pgf@tempa
1577 % it should contain a definition of \tikzexternallastkey.
1578 \fi
1579 \closein\r@pgf@reada
1580 %
1581 \let\tikzexternal@lastkey=\tikzexternallastkey
1582 \pgfmath@smuggleone\tikzexternal@lastkey
1583 \endgroup
1584 % normalize catcodes. Unfortunately, they cannot be expected to be the same.
1585 % \meaning will use catcode 12 for each token:
1586 \edef\tikzexternal@lastkey@normalized{\meaning\tikzexternal@lastkey}%
1587 \edef\tikzexternal@lastkey@new@normalized{\meaning\tikzexternal@lastkey@new}%
1588 \iftikzexternal@verboseuptodate
1589 \pgf@typeout{Up-to-date check of \tikzexternal@curfilename: new \tikzexternal@lastkey@new@normalized; old \tikzexternal@lastkey@normalized.^^J}%
1590 \fi
1591 \ifx\tikzexternal@lastkey@normalized\tikzexternal@lastkey@new@normalized
1592 \tikzexternal@file@isuptodatetrue
1593 \else
1594 \tikzexternal@file@isuptodatefalse
1595 \immediate\openout\w@pgf@writea=\tikzexternal@curfilename\tikzexternal@check@uptodate@ext\relax%
1596 \immediate\write\w@pgf@writea{\tikzexternal@hashfct@serialize{\tikzexternal@lastkey@new}\tikzexternal@PERCENTchar}%
1597 \immediate\closeout\w@pgf@writea
1598 \fi
1599 \fi
1600 }
1601
1602 % Checks whether the current picture needs to be externalized.
1603 %
1604 % This is the case if
1605 % a) there is no external image yet,
1606 % b) the picture has been invalidated manually.
1607 %
1608 % It returns \iftikzexternal@file@isuptodate accordingly.
1609 % #1: the picture-content
1610 \long\def\tikzexternal@externalizefig@systemcall@uptodatecheck#1{%
1611 \tikzexternal@check@uptodate@mode{#1}%
1612 \tikzexternal@checkforceremake
1613 \iftikzexternal@file@isuptodate
1614 % check if there is already a file.
1615 % In that case, use it. If that is not the case, generate it and include it afterwards.
1616 \gdef\pgf@filename{}%
1617 \xdef\pgf@tempa{\noexpand\pgf@findfile\pgfsys@imagesuffixlist:+{\tikzexternal@curfilename}}%
1618 \pgf@tempa
1619 \ifx\pgf@filename\pgfutil@empty%
1620 \global\tikzexternal@file@isuptodatefalse
1621 \fi
1622 \fi
1623 }%
1624
1625 % \pgf@filename will be empty if the assertion failed.
1626 \def\tikzexternal@externalizefig@systemcall@assertsuccess#1{%
1627 % check if there is a file now and raise an error message if not.
1628 \gdef\pgf@filename{}%
1629 \xdef\pgf@tempa{\noexpand\pgf@findfile\pgfsys@imagesuffixlist:+{\tikzexternal@curfilename}}%
1630 \pgf@tempa
1631 \ifx\pgf@filename\pgfutil@empty%
1632 \tikzexternal@externalizefig@systemcall@handleexception{#1}%
1633 \fi
1634 }%
1635 \def\tikzexternal@externalizefig@systemcall@handleexception@msg#1{%
1636 \tikzerror{Sorry, the system call '#1' did NOT result in a usable output file '\tikzexternal@curfilename' (expected one of \pgfsys@imagesuffixlist). Please verify that you have enabled system calls. For pdflatex, this is 'pdflatex -shell-escape'. Sometimes it is also named 'write 18' or something like that. Or maybe the command simply failed? Error messages can be found in '\[email protected]'. If you continue now, I'll try to typeset the picture}{}%
1637 }
1638 \def\tikzexternal@externalizefig@systemcall@handleexception@retry@later#1{%
1639 \gdef\tikzexternal@systemcall@reschedule@to@end@document{0}%
1640 \expandafter\pgfexternalreaddpth\expandafter{\tikzexternal@curfilename}%
1641 \if1\tikzexternal@systemcall@reschedule@to@end@document
1642 \pgfutil@IfUndefined{AtVeryEndDocument}{%
1643 \tikzexternal@externalizefig@systemcall@handleexception@msg{#1}%
1644 }{%
1645 \pgf@typeout{===== The last system call resulted in an EMPTY output file. Maybe it is part of \string\ref. Rescheduling it for \string\end{document}. ========}%
1646 \gdef\tikzexternal@typeset@picture@on@failure{0}%
1647 \tikzexternal@externalizefig@systemcall@reschedule{#1}%
1648 }%
1649 \else
1650 \tikzexternal@externalizefig@systemcall@handleexception@msg{#1}%
1651 \fi
1652 }
1653
1654 % reschedules the externalization of the current file to
1655 % \AtVeryEndDocument.
1656 %
1657 % This hook allows to execute code AFTER the main .aux file is
1658 % finished; it will even re-read the main aux file.
1659 \def\tikzexternal@externalizefig@systemcall@reschedule#1{%
1660 \t@tikzexternal@tmpb={%
1661 \global\let\tikzexternal@externalizefig@systemcall@handleexception=\tikzexternal@externalizefig@systemcall@handleexception@msg%
1662 % ... no auxlocks!
1663 \tikzexternal@externalizefig@systemcall@call{#1}%
1664 \tikzexternal@externalizefig@systemcall@assertsuccess{#1}%
1665 }%
1666 \xdef\pgfutil@tempa{%
1667 \noexpand\def\noexpand\tikzexternal@curfilename{\tikzexternal@curfilename}%
1668 \the\t@tikzexternal@tmpb
1669 }%
1670 \expandafter\AtVeryEndDocument\expandafter{\pgfutil@tempa}%
1671 \gdef\tikzexternal@has@rescheduled@something{1}%
1672 }%
1673 \let\tikzexternal@externalizefig@systemcall@handleexception=\tikzexternal@externalizefig@systemcall@handleexception@retry@later%
1674
1675 \def\tikzexternal@has@rescheduled@something{0}%
1676 \pgfutil@IfUndefined{AtVeryEndDocument}{}{%
1677 \AtVeryEndDocument{%
1678 \if1\tikzexternal@has@rescheduled@something
1679 \pgf@typeout{===== tikzexternal: \string\end{document} reached. Working on rescheduled images to resolve references... ========}%
1680 \fi
1681 }%
1682 }
1683
1684 % Overwrite error message of pgf.
1685 % This happens if the generated image was empty, i.e. if there was no \shipout.
1686 \def\pgfexternal@error@no@shipout{%
1687 \begingroup
1688 \begingroup
1689 % overwrite .dpth file in order to flag the file as "need to be scheduled
1690 % for \AtVeryEndDocument" :
1691 \immediate\openout\pgf@plotwrite=\pgfactualjobname.dpth
1692 \let\w@pgfexternal@auxout=\pgf@plotwrite
1693 \pgfexternalstorecommand@isexporting{\gdef\noexpand\tikzexternal@systemcall@reschedule@to@end@document{1}}%
1694 \immediate\closeout\pgf@plotwrite
1695 \endgroup
1696 \tikzexternal@assemble@systemcall{\pgfactualjobname}{\pgf@tempa}%
1697 \toks0=\expandafter{\pgf@tempa}%
1698 \tikzerror{Sorry, image externalization failed: the resulting image was EMPTY. I tried to externalize '\pgfactualjobname', but it seems there is no such image in the document!?
1699 \if\tikzexternal@opmode4 ^^J
1700 \space\space You are currently using 'mode=convert with system call'. This problem can happen if the image (or one of the images preceding it) was declared inside of a \string\label{} (i.e. in the .aux file): 'convert with system call' has no access to the main aux file.^^J
1701 \space\space Possible solutions in this case:^^J
1702 \space\space (a) Try using 'mode=list and make',^^J
1703 \space\space (b) Issue the externalization command '\the\toks0' *manually* (also check the preceding externalized images, perhaps the file name sequence is not correct).^^J
1704 \space\space Repeat: the resulting image was EMPTY, your attention is required
1705 \else
1706 \if\tikzexternal@opmode5 ^^J
1707 \space\space You are currently using 'mode=list and make'. Possible solutions:^^J
1708 \space\space (a) Try to delete `\[email protected]'. Perhaps it is not up-to-date.^^J
1709 \space\space (b) Perhaps pictures are declared inside of \string\label{} and the .aux file is not up-to-date. Re-run latex, perhaps re-generate the graphics as well.^^J
1710 \space\space Repeat: the resulting image was EMPTY, your attention is required
1711 \fi
1712 \fi}%
1713 \endgroup
1714 }%