summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgfplots/oldpgfcompatib/pgfplotsoldpgfsupp_tikzexternalshared.code.tex
blob: 1993db33660aea4f9daf61a976b52adb2a2ed99c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% This file is a copy of some part of PGF/Tikz.
%%% It has been copied here to provide :
%%%  - compatibility with older PGF versions
%%%  - availability of PGF contributions by Christian Feuersaenger
%%%    which are necessary or helpful for pgfplots.
%%%
%%% For reasons of simplicity, I have copied the whole file, including own contributions AND
%%% PGF parts. The copyrights are as they appear in PGF.
%%%
%%% Note that pgfplots has compatible licenses.
%%% 
%%% This copy has been modified in the following ways:
%%%  - nested \input commands have been updated
%%%  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Date of this copy: Di 29. Dez 22:11:16 CET 2009 %%%



% Copyright 2008 by Christian Feuersaenger
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.
%
% ******************************
% This here is the shared implementation of TeX-dialect specific files
%   tikzlibraryexternal.code.tex
% ******************************
%
% 
% This file provides a high-level automatic export feature for tikz pictures.
% It allows to export EACH SINGLE FIGURE into a separate PDF.
%
% 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).
%
%
% It replaces \tikzpicture/ \endtikzpicture and \tikz and invokes \beginpgfgraphicnamed ... \endpgfgraphicnamed
% commands if necessary.

% handle with extreme care and only in small, local groups:
\toksdef\t@tikzexternal@tmpa=0
\toksdef\t@tikzexternal@tmpb=1

\newwrite\tikzexternal@outfile
\newif\iftikzexternal@nestedflag
\newif\iftikzexternal@verboseio
\newif\iftikzexternal@genfigurelist
\newif\iftikzexternal@onlynamed
\newif\iftikzexternal@file@isuptodate
\newif\iftikzexternal@force@remake
\newif\iftikzexternal@optimize
\newif\iftikzexternal@export@enabled
\tikzexternal@export@enabledtrue

% This 'if' can be used as part of the public user interface.
%
% It is set by the 'remake next' key.
%
% It disables any up-to-date checks for the *next* picture, forcing a
% remake of it.
%
% It applies only to *one* picture.
\newif\iftikzexternalremakenext
\newif\iftikzexternal@verbose@optimize
\tikzexternal@verbose@optimizetrue

% A global boolean which can be used to skip single figures.
\newif\iftikzexternalexportnext
\tikzexternalexportnexttrue

% Invokes '#1' if the external lib is currently externalizing
% something and '#2' if not.
%
% This command must be called *after* \tikzexternalize.
\def\tikzifexternalizing#1#2{%
	\ifpgf@external@grabshipout #1\else #2\fi
}%
% Invokes '#1' if the external lib is currently externalizing the NEXT FOLLOWING
% tikzpicture. It invokes '#2' if that is not the case.
\def\tikzifexternalizingnext#1#2{%
	\ifpgf@external@grabshipout 
		\tikzexternalgetnextfilename\tikzexternal@temp
		\tikzifexternaljobnamematches\tikzexternal@temp{#1}{#2}%
	\else 
		#2
	\fi
}%

% Invokes '#2' if \jobname equals '#1' and '#3' if not.
% I suppose that '#1' is a macro containing the file name.
%
\def\tikzifexternaljobnamematches#1#2#3{%
	\edef\pgf@tempa{\expandafter\string\csname#1\endcsname}%
	\edef\pgf@tempb{\expandafter\string\csname\jobname\endcsname}%
	\ifx\pgf@tempa\pgf@tempb #2\else#3 \fi%
}%

\pgfqkeys{/tikz/external}{
	figure list/.is if=tikzexternal@genfigurelist,
	figure list=true,
	% 'mode' applies only if \jobname==real job name.
	mode/.is choice,
	mode/only graphics/.code	= {\def\tikzexternal@opmode{0}},
	mode/no graphics/.code		= {\def\tikzexternal@opmode{1}},
	% an alias for 'no graphics:'
	mode/only pictures/.code	= {\def\tikzexternal@opmode{1}},
	mode/graphics if exists/.code= {\def\tikzexternal@opmode{2}},
	mode/list only/.code		= {\def\tikzexternal@opmode{3}\tikzexternal@genfigurelisttrue},
	mode/convert with system call/.code={\def\tikzexternal@opmode{4}\tikzexternal@genfigurelistfalse},
	mode/list and make/.code	= {\def\tikzexternal@opmode{5}\tikzexternal@genfigurelisttrue},
	mode=convert with system call,
	system call/.initial={pdflatex -shell-escape -halt-on-error -interaction=batchmode -jobname "\image" "\texsource"},
%	system call/.initial={pdflatex -interaction=batchmode -jobname '\image' '\texsource'; pdftops -eps \image.pdf},
	force remake/.is if=tikzexternal@force@remake,
	force remake/.default=true,
	export next/.is if=tikzexternalexportnext,
	export/.is if=tikzexternal@export@enabled,
	remake next/.is if=tikzexternalremakenext,
	remake next/.default=true,
	verbose IO/.is if=tikzexternal@verboseio,
	verbose IO/.default=true,
	verbose IO=true,
	verbose optimize/.is if=tikzexternal@verbose@optimize,
	optimize/.is choice,
	optimize/true/.code={%
		\tikzexternal@optimizetrue
		\iftikzexternal@optimize
			\ifpgf@external@grabshipout
				% we have already started the externalization! Install
				% optimization commands to activate the new one:
				\tikzexternal@optimize@REPLACE
			\fi
		\fi
	},
	optimize/false/.code={%
		\tikzexternal@optimizefalse%
		\tikzexternal@optimize@RESTORE
	},%
	optimize=true,
	optimize/install/.code={},%
	optimize/restore/.code={},%
	% Expects two arguments, the command name and (optionally) a count
	% of expected arguments.
	% Example: 
	% 	'optimize command away=\includegraphics'
	% 	'optimize command away={\mycommand}{3}'
	%
	% It accepts commands which have '[]' arguments and whose first
	% argument always begins with '{'.
	%
	% #1: the command name
	% #2: either empty or a number of EXPECTED arguments. It will be checked
	% for one optional argument in square brackets as well.
	optimize command away/.code 2 args={%
		\expandafter\global\expandafter\let\csname\string#1@ORIG\endcsname=#1%
		\pgfkeysalso{%
			/tikz/external/optimize/install/.append code={%
				\def#1{\tikzexternal@optimize@away@cmd{#1}{#2}}%
			},
			/tikz/external/optimize/restore/.append code={%
				\expandafter\let\expandafter#1\csname\string#1@ORIG\endcsname
			}
		}%
		\iftikzexternal@optimize
			\ifpgf@external@grabshipout
				% we have already started the externalization! Install
				% optimization commands to activate the new one:
				\tikzexternal@optimize@REPLACE
			\fi
		\fi
	},
	optimize command away=\includegraphics,
	% EXPERIMENTAL (UNTESTED):
	optimize latex env away/.code={%
		\expandafter\let\expandafter\pgf@tempa\csname #1\endcsname
		\expandafter\global\expandafter\let\csname #1@ORIG\endcsname=\pgf@tempa%
		\pgfkeysalso{%
			/tikz/external/optimize/install/.append code={%
				\pgfutil@namedef{#1}{\tikzexternal@optimize@away@latex@env{#1}}%
			},
			/tikz/external/optimize/restore/.append code={%
				\pgfutil@namelet{#1}{#1@ORIG}%
			}%
		}%
	},
	only named/.is if=tikzexternal@onlynamed,
	only named/.default=true,
	figure name/.initial=\tikzexternal@realjob-figure,
	reset counter/.code={%
		\expandafter\gdef\csname c@tikzext@no@#1\endcsname{0}%
	},%
	prefix/.code={\tikzsetexternalprefix{#1}},
}

\def\tikzsetfigurename#1{\pgfkeyssetvalue{/tikz/external/figure name}{#1}}%

% This method sets the file name prefix used for every import/export.
%
% It is invoked by the '/tikz/external/prefix' key.
\def\tikzsetexternalprefix#1{\def\tikzexternal@filenameprefix{#1}}
\tikzsetexternalprefix{}

% This method initialises automatic externalization.
% 
% The first argument is the job's real name, without suffix.
%
% \tikzexternalize{myarticle}
%
% Doing so will 
% 1. automatically surround EVERY tikzpicture by \beginpgfgraphicnamed % sections.
% 2. in case that the job's real name and the current \jobname differ,
% it will process ONLY the matching tikzpicture.
\def\tikzexternalize{%
	\pgfutil@ifnextchar[{%
		\tikzexternalize@opt
	}{%
		\tikzexternalize@opt[]%
	}%
}%
\def\tikzexternalize@hasbeencalled{0}%
\def\tikzexternalize@opt[#1]#2{%
	\pgfqkeys{/tikz/external}{#1}%
	\def\tikzexternal@realjob{#2}%
	\pgfrealjobname{#2}%
	\def\tikzexternalize@hasbeencalled{1}%
	\tikzexternalenable
	\def\tikzexternal@determineimgextension##1:##2\relax{\gdef\tikzexternal@imgext{##1}}%
	\xdef\pgf@tempa{\pgfsys@imagesuffixlist}%
	\expandafter\tikzexternal@determineimgextension\pgf@tempa\relax
	\pgfutil@ifundefined{includegraphics}{%
		\let\tikzexternal@orig@includegraphics=\relax
	}{%
		\let\tikzexternal@orig@includegraphics=\includegraphics
	}%
}

% can be used to (temporarily) disable the externalization.
\def\tikzexternaldisable{%
	\let\tikz=\tikzexternal@origtikz
	\tikzexternal@TEXDIALECT@restore@picture
	\pgfutil@ifundefined{tikzexternal@orig@tikzfadingfrompicture}
	{}% NOP
	{%
		\let\tikzfadingfrompicture=\tikzexternal@orig@tikzfadingfrompicture
		\let\endtikzfadingfrompicture=\tikzexternal@orig@endtikzfadingfrompicture
		\let\tikzfading=\tikzexternal@orig@tikzfading
	}%
	\iftikzexternal@optimize
		\ifpgf@external@grabshipout
			\tikzexternal@optimize@RESTORE
		\fi
	\fi
}%
% re-enables externalization after a \tikzexternalizedisable.
\def\tikzexternalenable{%
	\if\tikzexternalize@hasbeencalled1%
		\let\tikz=\tikzexternal@tikz@replacement
		\tikzexternal@TEXDIALECT@installreplacement@picture
		\pgfutil@ifundefined{tikzfadingfrompicture}%
		{}% no special handling for 'fading lib'
		{%
			% oh, ok -- patch fading lib!
			\pgfutil@ifundefined{tikzexternal@orig@tikzfadingfrompicture}{%
				\let\tikzexternal@orig@tikzfadingfrompicture=\tikzfadingfrompicture
				\let\tikzexternal@orig@endtikzfadingfrompicture=\endtikzfadingfrompicture
				\let\tikzexternal@orig@tikzfading=\tikzfading
			}{}%
			\def\tikzfadingfrompicture{%
				\begingroup
				\tikzexternaldisable
				\tikzexternal@orig@tikzfadingfrompicture}%
			\def\endtikzfadingfrompicture{%
				\tikzexternal@orig@endtikzfadingfrompicture
				\endgroup}%
			\def\tikzfading[##1]{%
				\begingroup
				\tikzexternaldisable
				\tikzexternal@orig@tikzfading[##1]%
				\endgroup}%
		}%
		\iftikzexternal@optimize
			\ifpgf@external@grabshipout
				\tikzexternal@optimize@REPLACE
			\fi
		\fi
	\fi
}%

% Sets the filename for the next tikzpicture or \tikz shortcommand.
%
% It will *only* be used for the next picture.
%
% If you don't call \tikzsetnextfilename for a picture, a file name
% will be assembled automatically.
%
% Please note that the global file prefix will be prepended anyway.
\def\tikzsetnextfilename#1{\gdef\tikzexternal@nextfile{#1}}
\tikzsetnextfilename{}


% These are few TeX dialect-specific commands which need to be overriden when used with
% something different than plain TeX:
%
% 1. this command collects a complete image into a macro, up to (but not including) the
%    final TeX-dialect specific end-image command.
%    This is really difficult for LaTeX because \end{tikzpicture} either involves dirty
%    catcode-hacks to parse the braces or an ugly loop which loops until it
%	finds \end followed by {tikzpicture}. 
% 
% #1: a macro which will be called with the collected environment contents.
% all following tokens: the environment contents.
\long\gdef\tikzexternal@TEXDIALECT@collectpicture#1#2\endtikzpicture{#1{#2}}
%
% 2. the following commands should expand to the TeX-dialect specific begin and end image command, i.e.
%	\tikzpicture / \endtikzpicture
%    or 
%	\begin{tikzpicture} / \end{tikzpicture}
%	or
%	\starttikzpicture / \stoptikzpicture
\def\tikzexternal@TEXDIALECT@begpicture{\tikzpicture}
\def\tikzexternal@TEXDIALECT@endpicture{\endtikzpicture}%
%
% 3. This installs the replacement macros:
\def\tikzexternal@TEXDIALECT@installreplacement@picture{%
	\let\tikzpicture=\tikzexternal@tikzpicture@replacement
}%
\def\tikzexternal@TEXDIALECT@restore@picture{%
	\let\tikzpicture=\tikzexternal@origpicture
}%




\let\tikzexternal@origtikz=\tikz
\let\tikzexternal@origpicture=\tikzpicture
\let\tikzexternal@origendpicture=\endtikzpicture

% Replacement for '\tikzpicture'.
%
% There are two different states in which this method is applied:
% - normal typesetting. Then, the 'mode' key controls its behavior.
% - externalize mode (i.e. jobname != real job name). Then, it will
%   externalize the picture selected by 'jobname' (and only this picture).
\def\tikzexternal@tikzpicture@replacement{%
	\leavevmode
	\tikzexternal@getnextfilename\tikzexternal@curfilename
	\ifx\tikzexternal@curfilename\pgfutil@empty
		\ifpgf@external@grabshipout
			% This picture won't be externalized.
			% But maybe we can optimize it away:
			\iftikzexternal@optimize
				% -> it won't be shipped out anyway, so save typesetting time!
				\let\tikzexternal@next=\tikzexternal@skipfigure
			\else
				% include this graphics into the output (even if
				% it will be discarded anyway).
				\let\tikzexternal@next=\tikzexternal@normalpicture@nographics
			\fi
		\else
			\let\tikzexternal@next=\tikzexternal@normalpicture@nographics
		\fi
	\else
		\ifpgf@external@grabshipout
			\tikzifexternaljobnamematches\tikzexternal@curfilename{%
				% ok, this here IS the picture for which
				% pdflatex --jobname <my name>
				% has been invoked.
				\let\tikzexternal@next=\tikzexternal@externalizefig
			}{%
				\iftikzexternal@optimize
					% No, another picture should be exported. Simply skip this one
					% -> it won't be shipped out anyway, so save typesetting time!
					\let\tikzexternal@next=\tikzexternal@skipfigure
				\else
					% include this graphics into the output (even it
					% it will be discarded anyway).
					\let\tikzexternal@next=\tikzexternal@normalpicture@nographics
				\fi
			}%
		\else
			\ifcase\tikzexternal@opmode\relax
				% 'mode=only graphics'
				\let\tikzexternal@next=\tikzexternal@forcegraphics
			\or
				% 'mode=no graphics'
				\let\tikzexternal@next=\tikzexternal@normalpicture@nographics
			\or
				% 'mode=graphics if exists'
				\let\tikzexternal@next=\tikzexternal@externalizefig
			\or
				% 'mode=list only'
				\let\tikzexternal@next=\tikzexternal@listmodepicture
			\or
				% 'mode=convert with system call'
				\let\tikzexternal@next=\tikzexternal@externalizefig@systemcall
			\or
				% 'mode=list and make'
				\let\tikzexternal@next=\tikzexternal@list@and@makefile@mode@picture
			\fi
		\fi
	\fi
	\tikzexternal@next
}%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Replacement for \tikz short command:
\def\tikzexternal@tikz@replacement{%
	\pgfutil@ifnextchar[{\tikzexternal@tikz@replacement@opt}{\tikzexternal@tikz@replacement@opt[]}%
}%
\def\tikzexternal@tikz@replacement@opt[#1]{%
	\pgfutil@ifnextchar\bgroup{\tikzexternal@tikz@replacement@opt@{#1}}{\tikzexternal@tikz@replacement@opt@@{#1}}%
}
\long\def\tikzexternal@tikz@replacement@opt@#1#2{%
	\tikzexternal@tikz@replacement@opt@process{#1}{#2}%
}%
\def\tikzexternal@tikz@replacement@opt@@#1{%
  \def\tikz@next{\tikzexternal@tikz@replacement@collectnormalsemicolon{#1}}%
  \ifnum\the\catcode`\;=\active\relax%
    \def\tikz@next{\tikzexternal@tikz@replacement@collectactivesemicolon{#1}}%
  \fi%
  \tikz@next}
\long\def\tikzexternal@tikz@replacement@collectnormalsemicolon#1#2;{%
	\tikzexternal@tikz@replacement@opt@process{#1}{#2;}%
}%
{
  \catcode`\;=\active
  \long\gdef\tikzexternal@tikz@replacement@collectactivesemicolon#1#2;{%
	\tikzexternal@tikz@replacement@opt@process{#1}{#2;}%
  }
}
\long\def\tikzexternal@tikz@replacement@opt@process#1#2{%
	\begingroup
	\t@tikzexternal@tmpa=\expandafter{\tikzexternal@TEXDIALECT@begpicture[#1]#2}%
	\t@tikzexternal@tmpb=\expandafter{\tikzexternal@TEXDIALECT@endpicture}%
	\xdef\pgf@tempa{\the\t@tikzexternal@tmpa\the\t@tikzexternal@tmpb}%
	\endgroup
	\pgf@tempa
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\tikzexternal@getnextfilename@advancecount{%
	\begingroup
	\c@pgf@counta=\csname c@tikzext@no@\tikzexternal@figurename\endcsname\relax
	\advance\c@pgf@counta by1 
	\expandafter\xdef\csname c@tikzext@no@\tikzexternal@figurename\endcsname{\the\c@pgf@counta}%
	\endgroup
}%

% Returns the file name which will be used for the next tikzpicture (based on the currently available information only, of course).
\def\tikzexternalgetnextfilename#1{%
	\begingroup
	\let\tikzexternal@getnextfilename@advancecount\relax% NOP
	\tikzexternal@getnextfilename#1%
	\pgfmath@smuggleone#1%
	\endgroup
}%

% Fills #1 with a filename for the current picture.
%
% The filename will be generated automatically by appending '-figure<number>' to the real jobname.
%
% This method deals with
% - \tikzsetnextfilename
% - \tikzexternalexportnextfalse
%
% It returns #1={} if the current figure shall NOT be exported.
%
% Please note that both, \tikzsetnextfilename and \tikzexternalexportnextfalse
% affect only ONE picture. They will be reset afterwards.
\def\tikzexternal@getnextfilename#1{%
	\let#1=\pgfutil@empty
	% determine next file name:
	\iftikzexternal@export@enabled
		\iftikzexternalexportnext
			\begingroup
			\t@tikzexternal@tmpa=\expandafter{\tikzexternal@filenameprefix}%
			\ifx\tikzexternal@nextfile\pgfutil@empty
				\iftikzexternal@onlynamed
					\xdef\pgf@tempa{}%
				\else
					\pgfkeysgetvalue{/tikz/external/figure name}\tikzexternal@figurename
					\pgfutil@ifundefined{c@tikzext@no@\tikzexternal@figurename}{%
						% initialise on first usage:
						\expandafter\gdef\csname c@tikzext@no@\tikzexternal@figurename\endcsname{0}%
					}{}%
					\t@tikzexternal@tmpb=\expandafter{\tikzexternal@figurename}%
					\xdef\pgf@tempa{\the\t@tikzexternal@tmpa\the\t@tikzexternal@tmpb\csname c@tikzext@no@\tikzexternal@figurename\endcsname}%
					% advance the counter for 'figure name':
					\tikzexternal@getnextfilename@advancecount
				\fi
			\else
				\t@tikzexternal@tmpb=\expandafter{\tikzexternal@nextfile}%
				\xdef\pgf@tempa{\the\t@tikzexternal@tmpa\the\t@tikzexternal@tmpb}%
			\fi
			\endgroup
			\let#1=\pgf@tempa
		\fi
	\fi
	% 
	% Reset global flags:
	\global\let\tikzexternal@nextfile=\pgfutil@empty
	\global\tikzexternalexportnexttrue
	%
	% Handle verbose output:
	\ifpgf@external@grabshipout%
	\else
		\iftikzexternal@genfigurelist
			\ifx#1\pgfutil@empty
			\else
				\pgfutil@ifundefined{tikzexternal@listmode@openedfile}{%
					\message{Opening '\tikzexternal@realjob.figlist' for writing.}%
					\immediate\openout\tikzexternal@outfile=\tikzexternal@realjob.figlist\relax
					\gdef\tikzexternal@listmode@openedfile{1}%
					\if\tikzexternal@opmode5% mode='list and make'
						\iftikzexternal@verboseio
							\immediate\write16{Opening '\tikzexternal@realjob.makefile' for writing.}%
						\fi
						\begingroup
							% this makes \tikzexternal@outmakefile global:
							\globaldefs=1
							% this gets round '\outer\newwrite' in plain TeX:
							\csname newwrite\endcsname\tikzexternal@outmakefile
						\endgroup
						\immediate\openout\tikzexternal@outmakefile=\tikzexternal@realjob.makefile\relax
						\immediate\write\tikzexternal@outmakefile{ALL_FIGURE_NAMES=\tikzexternal@DOLLARchar(shell cat \tikzexternal@realjob.figlist)}%
						\immediate\write\tikzexternal@outmakefile{ALL_FIGURES=\tikzexternal@DOLLARchar(ALL_FIGURE_NAMES:\tikzexternal@PERCENTchar=\tikzexternal@PERCENTchar\tikzexternal@imgext)}%
						\immediate\write\tikzexternal@outmakefile{}%
						\immediate\write\tikzexternal@outmakefile{allimages: \tikzexternal@DOLLARchar(ALL_FIGURES)}%
						\immediate\write\tikzexternal@outmakefile{\tikzexternal@TABchar @echo All images exist now. Use make -B to re-generate them.}%
						\immediate\write\tikzexternal@outmakefile{}%
						\pgfutil@ifundefined{AtEndDocument}{}{%
							\AtEndDocument{\immediate\write16{===== mode=`list and make': Use 'make -f \tikzexternal@realjob.makefile' to generate all images. Then, re-run (pdf)latex \tikzexternal@realjob. =====}}%
						}%
					\fi
				}{}%
				\iftikzexternal@verboseio
					\immediate\write16{Writing '#1' to '\tikzexternal@realjob.figlist'.}%
				\fi
				\immediate\write\tikzexternal@outfile{#1}%
				\if\tikzexternal@opmode5% mode='list and make'
					\tikzexternal@assemble@systemcall{\pgf@tempa}%
					\iftikzexternal@verboseio
						\immediate\write16{Writing '#1' to '\tikzexternal@realjob.makefile'.}%
					\fi
					\immediate\write\tikzexternal@outmakefile{#1\tikzexternal@imgext:}%
					\immediate\write\tikzexternal@outmakefile{\tikzexternal@TABchar\pgf@tempa}%
					\immediate\write\tikzexternal@outmakefile{}%
				\fi
			\fi
		\fi
	\fi
}%

% This command is invoked 
%  if and only if ( (grab mode && --jobname matches) || 'mode=graphics if exists' )
%
% It converts the current tikzpicture into an image in grab mode
% or processes the 'graphics if exist' mode.
\def\tikzexternal@externalizefig{%
	\ifpgf@external@grabshipout%
		% In this case, we already KNOW that the filename matches.
		\expandafter\tikzexternal@externalizefig@GRAB%
	\else
		\expandafter\tikzexternal@externalizefig@mode@graphics@if@exists
	\fi%
}
\def\tikzexternal@externalizefig@mode@graphics@if@exists{%
	% check if there is already a file.
	% In that case, use it. If not, typeset the picture normally.
	\gdef\pgf@filename{}%
	\xdef\pgf@tempa{\noexpand\pgf@findfile\pgfsys@imagesuffixlist:+{\tikzexternal@curfilename}}%
	\pgf@tempa
	\ifx\pgf@filename\pgfutil@empty%
		% Note: since we have no 'GRAB' mode, we do not have to deal with optimization.
		% there is no graphics.
		\expandafter\tikzexternal@normalpicture@nographics
	\else
		\expandafter\tikzexternal@forcegraphics
	\fi
}%

\def\tikzexternal@externalizefig@GRAB{%
	\iftikzexternal@optimize
		\ifpgf@external@grabshipout
			\tikzexternal@optimize@RESTORE
		\fi
	\fi
	\def\tikzpicture{%
		\def\tikzpicture{% make sure that nested \tikzpicture are processed normally.
			\begingroup
			\def\endtikzpicture{%
				\tikzexternal@origendpicture
				\endgroup
			}%
			\tikzexternal@origpicture
		}%
		\pgf@external@grab{\tikzexternal@curfilename}%
		\tikzexternal@origpicture
	}%
	\def\endtikzpicture{%
		\tikzexternal@origendpicture
		\pgf@externalend
		\let\tikzpicture=\tikzexternal@tikzpicture@replacement
		\let\endtikzpicture=\tikzexternal@origendpicture
		\iftikzexternal@optimize
			\ifpgf@external@grabshipout
				\tikzexternal@optimize@REPLACE
			\fi
		\fi
	}%
	\tikzpicture
}%

% FIXME : this does not respect catcode changes!
%--------------------------------------------------
% \long\def\tikzexternal@externalizefig@@#1{%
% 	\let\tikzpicture=\tikzexternal@origpicture
% 	\iftikzexternal@optimize
% 		\ifpgf@external@grabshipout
% 			\tikzexternal@optimize@RESTORE
% 		\fi
% 	\fi
% 	\expandafter\beginpgfgraphicnamed\expandafter{\tikzexternal@curfilename}%
% 	\tikzpicture
% 	#1
% 	\endtikzpicture
% 	\endpgfgraphicnamed
% 	\let\tikzpicture=\tikzexternal@tikzpicture@replacement
% 	\iftikzexternal@optimize
% 		\ifpgf@external@grabshipout
% 			\tikzexternal@optimize@REPLACE
% 		\fi
% 	\fi
% 	\tikzexternal@closeenvironments
% }%
%-------------------------------------------------- 

% Used by the optimization code.
% It will be called if GRAB mode is on.
% See the 'optimize command away' key.
\def\tikzexternal@optimize@away@cmd#1#2{%
	\pgfutil@ifnextchar[{%
		\tikzexternal@optimize@away@cmd@{#1}{#2}%
	}{%
		\tikzexternal@optimize@away@cmd@{#1}{#2}[]%
	}%
}%
\def\tikzexternal@optimize@away@cmd@#1#2[#3]{%
	\def\tikz@temp{#2}%
	\ifx\tikz@temp\pgfutil@empty
		\def\tikz@temp{\tikzexternal@optimize@away@cmd@auto{#1}{#3}}%
	\else
		\ifcase#2\relax
			\def\tikzexternal@optimize@away@cmd@manual{%
				\tikzexternal@optimize@away@cmd@@@{#1}{#3}{}%
			}%
		\or
			\def\tikzexternal@optimize@away@cmd@manual##1{%
				\tikzexternal@optimize@away@cmd@@@{#1}{#3}{{##1}}%
			}%
		\or
			\def\tikzexternal@optimize@away@cmd@manual##1##2{%
				\tikzexternal@optimize@away@cmd@@@{#1}{#3}{{##1}{##2}}%
			}%
		\or
			\def\tikzexternal@optimize@away@cmd@manual##1##2##3{%
				\tikzexternal@optimize@away@cmd@@@{#1}{#3}{{##1}{##2}{##3}}%
			}%
		\or
			\def\tikzexternal@optimize@away@cmd@manual##1##2##3##4{%
				\tikzexternal@optimize@away@cmd@@@{#1}{#3}{{##1}{##2}{##3}{##4}}%
			}%
		\or
			\def\tikzexternal@optimize@away@cmd@manual##1##2##3##4##5{%
				\tikzexternal@optimize@away@cmd@@@{#1}{#3}{{##1}{##2}{##3}{##4}{##5}}%
			}%
		\or
			\def\tikzexternal@optimize@away@cmd@manual##1##2##3##4##5##6{%
				\tikzexternal@optimize@away@cmd@@@{#1}{#3}{{##1}{##2}{##3}{##4}{##5}{##6}}%
			}%
		\or
			\def\tikzexternal@optimize@away@cmd@manual##1##2##3##4##5##6##7{%
				\tikzexternal@optimize@away@cmd@@@{#1}{#3}{{##1}{##2}{##3}{##4}{##5}{##6}{##7}}%
			}%
		\or
			\def\tikzexternal@optimize@away@cmd@manual##1##2##3##4##5##6##7##8{%
				\tikzexternal@optimize@away@cmd@@@{#1}{#3}{{##1}{##2}{##3}{##4}{##5}{##6}{##7}{##8}}%
			}%
		\or
			\def\tikzexternal@optimize@away@cmd@manual##1##2##3##4##5##6##7##8##9{%
				\tikzexternal@optimize@away@cmd@@@{#1}{#3}{{##1}{##2}{##3}{##4}{##5}{##6}{##7}{##8}{##9}}%
			}%
		\fi
		\def\tikz@temp{\tikzexternal@optimize@away@cmd@manual}%
	\fi
	\tikz@temp
}%
\def\tikzexternal@optimize@away@cmd@auto#1#2{%
	\pgfutil@ifnextchar\bgroup{%
		\tikzexternal@optimize@away@cmd@auto@{#1}{#2}%
	}{%
		\tikzexternal@optimize@away@cmd@auto@{#1}{#2}{<no argument in curly braces>}% give empty argument.
	}
}%

\def\tikzexternal@optimize@away@cmd@auto@#1#2#3{%
	\tikzexternal@optimize@away@cmd@@@{#1}{#2}{{#3}}% <-- provide braces
}%
\def\tikzexternal@optimize@away@cmd@@@#1#2#3{%
	\begingroup
	\toks0={#1[#2]#3}%
	\iftikzexternal@verbose@optimize
		\immediate\write16{The command '\the\toks0' has been optimized away. Use '/tikz/external/optimize=false' to disable this.}%
	\fi
	[ {\sc \string#1\ optimized away because it does not contribute to exported PDF}]%
	\endgroup
}%

\def\tikzexternal@optimize@away@latex@env#1{%
	\def\tikzexternal@optimize@away@latex@env@{#1}%
	\begingroup
		\def\tikzexternal@laTeX@collect@until@end@tikzpicturetikzpicturestring{#1}%
		\tikzexternal@TEXDIALECT@collectpicture\tikzexternal@optimize@away@latex@env@close
}%
\long\def\tikzexternal@optimize@away@latex@env@close#1{%
	\iftikzexternal@verbose@optimize
		\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 exported PDF. Use '/tikz/external/optimize=false' to disable this.}%
	\fi
	\endgroup
	% we still need to invoke \end{<name>} in latex because \begin{<name>}
	% starts a local group - that must be closed properly. Make sure then \end<name> does nothing:
	\expandafter\let\csname end\tikzexternal@optimize@away@latex@env@\endcsname=\relax
	\expandafter\end\expandafter{\tikzexternal@optimize@away@latex@env@}%
}%

\def\tikzexternal@optimize@REPLACE{%
	\pgfkeysvalueof{/tikz/external/optimize/install/.@cmd}\pgfeov
}%

\def\tikzexternal@optimize@RESTORE{%
	\pgfkeysvalueof{/tikz/external/optimize/restore/.@cmd}\pgfeov
}%

% Closes one 'tikzpicture' environment. This is only used for LaTeX,
% because the '\end{tikzpicture}' command would raise an exception otherwise.
% It does *not* call \endtikzpicture.
\def\tikzexternal@closeenvironments{%
	\let\endtikzpicture=\relax
	\tikzexternal@TEXDIALECT@endpicture
	\let\endtikzpicture=\tikzexternal@origendpicture
}

% Throws a tikzpicture away - without further notice.
% Used if we are currently converting *another* picture. No need to waste time
% with expensive pictures if they are not shipped out anyway.
%
% See the 'optimize' key to disable this.
\def\tikzexternal@skipfigure{%
	\tikzexternal@TEXDIALECT@collectpicture\tikzexternal@skipfigure@@
}
\long\def\tikzexternal@skipfigure@@#1{%
	\iftikzexternal@verbose@optimize
		\immediate\write16{A tikzpicture has been optimized away. Use '/tikz/external/optimize=false' to disable this.}%
	\fi
	\tikzexternal@closeenvironments
}

% Processes tikzpicture normally; without any externalization.
%
% We need to do further work here to deal with NESTED tikzpicture environments 
% because all of them shall also be typeset normally.
%
% Idea:
% 1. restore the original \tikzpicture macro
% 2. install the replacement \tikzpicture in \end{tikzpicture}.
% and keep track of nesting.
%
% FIXME: could it be possible that nested tikzpictures use other code anyway?
\def\tikzexternal@normalpicture@nographics{%
	\tikzexternal@nestedflagfalse
	\let\tikzpicture=\tikzexternal@normalpicture@begreplace
	\let\endtikzpicture=\tikzexternal@normalpicture@endreplace
	\tikzpicture
}
\def\tikzexternal@normalpicture@begreplace{%
	\begingroup
	\tikzexternal@nestedflagtrue
	\tikzexternal@origpicture
}
\def\tikzexternal@normalpicture@endreplace{%
	\tikzexternal@origendpicture
	\endgroup
	\iftikzexternal@nestedflag
	\else
		\let\tikzpicture=\tikzexternal@tikzpicture@replacement
		\let\endtikzpicture=\tikzexternal@origendpicture
	\fi
}%

% Assumes there is an image on disk and reads it. The tikzpicture is thrown away.
\def\tikzexternal@forcegraphics{%
	\tikzexternal@TEXDIALECT@collectpicture\tikzexternal@forcegraphics@@
}
\long\def\tikzexternal@forcegraphics@@#1{%
	\expandafter\pgfincludeexternalgraphics\expandafter{\tikzexternal@curfilename}%
	\tikzexternal@closeenvironments
}

% Simply replaces the complete picture by some placeholder text.
% It is used by 'mode=list only' to reduce runtime.
\def\tikzexternal@listmodepicture{%
	\tikzexternal@TEXDIALECT@collectpicture\tikzexternal@listmodepicture@@
}
\long\def\tikzexternal@listmodepicture@@#1{%
	[[ {\sc Image Discarded Due To} {\tt`/tikz/external/%
			\ifcase\tikzexternal@opmode\relax
				mode=only graphics%
			\or
				mode=no graphics%
			\or
				mode=graphics if exists%
			\or
				mode=list only%
			\or
				mode=convert with system call%
			\or
				mode=list and make%
			\fi
	'}~]]%
	\tikzexternal@closeenvironments
}

% check if there is already a file.
% In that case, use it. If not, skip it.
\def\tikzexternal@list@and@makefile@mode@picture{%
	\gdef\pgf@filename{}%
	\xdef\pgf@tempa{\noexpand\pgf@findfile\pgfsys@imagesuffixlist:+{\tikzexternal@curfilename}}%
	\pgf@tempa
	\ifx\pgf@filename\pgfutil@empty%
		% Note: since we have no 'GRAB' mode, we do not have to deal with optimization.
		% there is no graphics.
		\expandafter\tikzexternal@listmodepicture
	\else
		\expandafter\tikzexternal@forcegraphics
	\fi
}%

\def\tikzexternal@externalizefig@GRAB{%
	\iftikzexternal@optimize
		\ifpgf@external@grabshipout
			\tikzexternal@optimize@RESTORE
		\fi
	\fi
	\def\tikzpicture{%
		\def\tikzpicture{% make sure that nested \tikzpicture are processed normally.
			\begingroup
			\def\endtikzpicture{%
				\tikzexternal@origendpicture
				\endgroup
			}%
			\tikzexternal@origpicture
		}%
		\pgf@external@grab{\tikzexternal@curfilename}%
		\tikzexternal@origpicture
	}%
	\def\endtikzpicture{%
		\tikzexternal@origendpicture
		\pgf@externalend
		\let\tikzpicture=\tikzexternal@tikzpicture@replacement
		\let\endtikzpicture=\tikzexternal@origendpicture
		\iftikzexternal@optimize
			\ifpgf@external@grabshipout
				\tikzexternal@optimize@REPLACE
			\fi
		\fi
	}%
	\tikzpicture
}%


% 1. Discards the current picture in this document.
% 2. Checks whether an image exists already. If that is the case: acquire it.
% 3. If no image exists: call '/tikz/external/system call'. This will
%    process \tikzexternal@externalizefig.
% 4. Assert that finally an image exists and use it.
\def\tikzexternal@externalizefig@systemcall{%
	\tikzexternal@TEXDIALECT@collectpicture\tikzexternal@externalizefig@systemcall@@
}

{
\catcode`\"=12
\catcode`\'=12
\catcode`\;=12
\catcode`\&=12
\catcode`\-=12
\xdef\tikzexternal@normal@dq{"}
\xdef\tikzexternal@normal@sq{'}
\xdef\tikzexternal@normal@semic{;}
\xdef\tikzexternal@normal@and{&}
\xdef\tikzexternal@normal@dash{-}
\catcode`\"=13
\catcode`\'=13
\catcode`\;=13
\catcode`\&=13
\catcode`\-=13
\gdef\tikzexternal@activate@normal@dq{\let"=\tikzexternal@normal@dq}
\gdef\tikzexternal@activate@normal@sq{\let'=\tikzexternal@normal@sq}
\gdef\tikzexternal@activate@normal@semic{\let;=\tikzexternal@normal@semic}
\gdef\tikzexternal@activate@normal@and{\let&=\tikzexternal@normal@and}
\gdef\tikzexternal@activate@normal@dash{\let-=\tikzexternal@normal@dash}
\catcode`\|=0
\catcode`\\=12
|xdef|tikzexternal@normal@backslash{\}%
}
{
\catcode`\^^I=12 
\catcode`\$=12
\catcode`\%=12
\xdef\tikzexternal@TABchar{^^I}\xdef\tikzexternal@PERCENTchar{%}\xdef\tikzexternal@DOLLARchar{$}}

% Creates the '/tikz/external/system call' command as string and
% returns it into the (global!) macro #1.
\def\tikzexternal@assemble@systemcall#1{%
	\begingroup
		\let\image=\tikzexternal@curfilename
		\let\texsource=\tikzexternal@realjob
		\ifnum\the\catcode`\"=13 \tikzexternal@activate@normal@dq\fi
		\ifnum\the\catcode`\'=13 \tikzexternal@activate@normal@sq\fi
		\ifnum\the\catcode`\;=13 \tikzexternal@activate@normal@semic\fi
		\ifnum\the\catcode`\-=13 \tikzexternal@activate@normal@dash\fi
		\let\\=\tikzexternal@normal@backslash
		\xdef#1{\pgfkeysvalueof{/tikz/external/system call}}%
	\endgroup
}%
\long\def\tikzexternal@externalizefig@systemcall@@#1{%
	\tikzexternal@externalizefig@systemcall@uptodatecheck%
	\iftikzexternal@file@isuptodate
		\iftikzexternal@verboseio
			\immediate\write16{===== Image '\tikzexternal@curfilename' is up-to-date. ======}%
		\fi
		\let\pgf@filename=\tikzexternal@curfilename
	\else
		\begingroup
		% no such image. Generate it!
		\tikzexternal@assemble@systemcall{\pgf@tempa}%
		\iftikzexternal@verboseio
			\immediate\write16{===== 'mode=convert with system call': Invoking '\pgf@tempa' ========}%
		\fi
		\immediate\write18{\pgf@tempa}%
		\expandafter\tikzexternal@externalizefig@systemcall@assertsuccess\expandafter{\pgf@tempa}%
		\pgfmath@smuggleone\pgf@filename
		\endgroup
	\fi
	\ifx\pgf@filename\pgfutil@empty
		% error recovery: something did not work! Try to load it
		% anyway. Perhaps it was just that shell-escape wasn't
		% enabled.
		\begingroup
		\toks0={%
			\tikzexternaldisable
			\pgfutil@ifundefined{scantokens}{\long\def\scantokens##1{##1}}{}%
		}%
		% FIXME : THIS WILL FAIL IF THERE IS '##' INSIDE OF '#1'!
		% for example something like /.style={#1} in the picture environment *will* fail.
		\toks1={%
			\tikzpicture#1%
		}%
		\toks2={%
			\tikzexternal@TEXDIALECT@endpicture
			\tikzexternalenable
		}%
		\xdef\tikzexternal@externalizefig@systemcall@next{%
			\the\toks0
			%  try reading them again as if they were in the input file.
			\noexpand\scantokens{\the\toks1 }%
			\the\toks2
		}%
		\endgroup
	\else
		% ok, take the image!
		\expandafter\pgfincludeexternalgraphics\expandafter{\tikzexternal@curfilename}%
		\gdef\tikzexternal@externalizefig@systemcall@next{\tikzexternal@closeenvironments}%
	\fi
	\tikzexternal@externalizefig@systemcall@next
}%

% Checks whether the current picture needs to be externalized.
% 
% This is the case if 
% a) there is no external image yet,
% b) the picture has been invalidated manually.
%
% It returns \iftikzexternal@file@isuptodate accordingly.
\def\tikzexternal@externalizefig@systemcall@uptodatecheck{%
	\iftikzexternal@force@remake
		\global\tikzexternal@file@isuptodatefalse
	\else
		\iftikzexternalremakenext
			\global\tikzexternal@file@isuptodatefalse
			\global\tikzexternalremakenextfalse
		\else
			% check if there is already a file.
			% In that case, use it. If that is not the case, generate it and include it afterwards.
			\gdef\pgf@filename{}%
			\xdef\pgf@tempa{\noexpand\pgf@findfile\pgfsys@imagesuffixlist:+{\tikzexternal@curfilename}}%
			\pgf@tempa
			\ifx\pgf@filename\pgfutil@empty%
				\global\tikzexternal@file@isuptodatefalse
			\else
				\global\tikzexternal@file@isuptodatetrue
			\fi
		\fi
	\fi
}%

% \pgf@filename will be empty if the assertion failed.
\def\tikzexternal@externalizefig@systemcall@assertsuccess#1{%
	% check if there is a file now and raise an error message if not.
	\gdef\pgf@filename{}%
	\xdef\pgf@tempa{\noexpand\pgf@findfile\pgfsys@imagesuffixlist:+{\tikzexternal@curfilename}}%
	\pgf@tempa
	\ifx\pgf@filename\pgfutil@empty%
		\PackageError{tikz}{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 '\tikzexternal@curfilename.log'. If you continue now, I'll try to typeset the picture.}%
	\fi
}%