Debian Codegraph @

texmf-dist/tex/generic/pgfplots/oldpgfcompatib/pgfplotsoldpgfsupp_tikzexternalshared.code.tex

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