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
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% 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: Do 5. Aug 20:33:50 CEST 2010 %%%
% Copyright 2008-2010 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 '#1' if the external lib is currently externalizing the
% current picture. It invokes '#2' if that is not the case.
%
% If the command is invoked outside of a picture, '#2' will be
% invoked.
\def\tikzifexternalizingcurrent#1#2{%
\ifpgf@external@grabshipout
\tikzexternalgetcurrentfilename\tikzexternal@temp
\ifx\tikzexternal@temp\pgfutil@empty
#2%
\else
\tikzifexternaljobnamematches\tikzexternal@temp{#1}{#2}%
\fi
\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\pgfactualjobname\endcsname}%
\ifx\pgf@tempa\pgf@tempb #2\else#3 \fi%
}%
\pgfutil@ifundefined{pdfshellescape}{%
\def\tikzexternalcheckshellescape{}%
}{%
\ifnum\pdfshellescape=1
\def\tikzexternalcheckshellescape{\pgfkeysvalueof{/tikz/external/shell escape}\space}%
\else
\def\tikzexternalcheckshellescape{}%
\fi
}%
\newif\iftikzexternal@auto@detect@jobname
\pgfqkeys{/tikz/external}{
figure list/.is if=tikzexternal@genfigurelist,
aux in dpth/.style={/pgf/images/aux in dpth=#1},%
% 'mode' applies only if \jobname==real job name.
mode/.is choice,
mode/only graphics/.code = {%
\def\tikzexternal@opmode{0}%
\pgfkeysalso{/pgf/images/aux in dpth=true}%
},
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}%
\pgfkeysalso{/pgf/images/aux in dpth=true}%
},
mode/list only/.code = {\def\tikzexternal@opmode{3}\tikzexternal@genfigurelisttrue},
mode/convert with system call/.code={%
\def\tikzexternal@opmode{4}%
\pgfkeysalso{/tikz/external/figure list=false,/pgf/images/aux in dpth=true}% ATTENTION: this *can't* work if \label{} contains pictures!
},
mode/list and make/.code = {%
\def\tikzexternal@opmode{5}%
\pgfkeysalso{/tikz/external/figure list=true,/pgf/images/aux in dpth=true}%
},
mode=convert with system call,
system call/.initial={pdflatex \tikzexternalcheckshellescape -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,
failed ref warnings for/.initial={\ref,\cite,\pageref},
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,
verbose/.style={
verbose IO=#1,
verbose optimize=#1,
},
shell escape/.initial=-shell-escape,
read main aux/.is if=pgfexternalreadmainaux,
image discarded text/.initial={%
[[ {\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
'}~]]%
\pgfutil@ifundefined{tikzexternal@warning@at@end}{%
\pgfutil@ifundefined{AtEndDocument}{}{%
\AtEndDocument{%
\message{! Package tikz Warning: Some images are not up-to-date and need to be generated. }%
}%
}%
\gdef\tikzexternal@warning@at@end{1}%
}{}%
},
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}}%
\def\tikzappendtofigurename#1{\pgfkeys{/tikz/external/figure name/.add={}{#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 command does need any argument, but it has two optional ones:
%
% 1. \tikzexternalize[<options>]
%
% 2. \tikzexternalize{<real job's name>}
%
% 3. \tikzexternalize[<options>]{<real job's name>}
%
% The <real job's name> is the job's file name, without the suffix
% .tex .
% If the <real job's name> is omitted, it will be deduced
% automatically.
%
% The effect is:
% 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@opt[#1]{%
\pgfutil@ifnextchar\bgroup{%
\tikzexternal@auto@detect@jobnamefalse
\tikzexternalize@opt@withname[#1]%
}{%
\tikzexternal@auto@detect@jobnametrue
\pgfutil@ifundefined{tikzexternalrealjob}{%
% ok, \tikzexternalrealjob is not known.
% Assume we are currently *not* externalizing.
\let\tikzexternalrealjob=\jobname
}{}%
\def\pgf@tempa{\tikzexternalize@opt@withname[#1]}%
\expandafter\pgf@tempa\expandafter{\tikzexternalrealjob}%
}%
}%
\def\tikzexternalize@opt@withname[#1]#2{%
\gdef\tikzexternalrealjob{#2}%
\if1\tikzexternalize@hasbeencalled
\else
\pgfqkeys{/tikz/external}{#1}%
\def\tikzexternal@realjob{#2}%
\tikzexternalauxlock@init
\pgfrealjobname{#2}%
\def\tikzexternalize@hasbeencalled{1}%
\tikzexternalenable
\def\tikzexternal@determineimgextension##1:##2\relax{\gdef\tikzexternalimgextension{##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
}%
\let\tikzexternalfiledependsonfile=\tikzexternalfiledependsonfile@ACTIVE
\let\tikzpicturedependsonfile=\tikzpicturedependsonfile@ACTIVE
\fi
}
\def\tikzexternalize@hasbeencalled{0}%
% If mode=`convert with system call', the boolean \ifpgfexternalreadmainaux
% will be set depending on the current lock file.
%
% The idea is as follows: Suppose
% pdflatex -shell-escape mainjob.tex
% is running.
%
% While it runs,
% a) it modifies its mainjob.aux file,
% b) it issues pdflatex -jobname mainjob-figure0 mainjob.tex .
%
% The call (b) will try to read mainjob.aux in order to resolve \ref
% commands. This may fail since the aux
% file is not complete; it may be subject to output buffering. To
% avoid such failure, a locking mechanism is established.
%
% The locking mechanisms causes (a) to write a lock command into
% mainjob.auxlock
% just before (b) is called. Then, (b) will check it. After (b) has
% completed, the lock will be reset.
%
% Thus: mode=convert with system call does NOT support \ref commands
% inside of images. You will need to call the externalization command
% manually. In this case, it should work.
%
% The lock file allows to detect automatically whether an image is created by
% 'convert with system call' or if the user issued the required command manually.
%
% @PRECONDITION : this command must be invoked AFTER
% \tikzexternal@realjob has been defined but BEFORE \pgfrealjobname is
% called.
%
% @POSTCONDITION : If \ifpgfexternalreadmainaux=\iffalse, we won't do
% anything. Otherwise, it will be changed depending for 'mode=convert
% with system call' if necessary.
\def\tikzexternalauxlock@init{%
\if4\tikzexternal@opmode
% mode=convert with system call
\ifpgfexternalreadmainaux
% ohoh. This case needs care because the main.aux might
% not be ready at this time!
% check.
%
\edef\pgf@tempa{\expandafter\string\csname\tikzexternal@realjob\endcsname}%
\edef\pgf@tempb{\expandafter\string\csname\jobname\endcsname}%
\ifx\pgf@tempa\pgf@tempb
% we are *not* externalizing. Set lock into its
% initial "unlocked" state.
\tikzexternalauxlock@setlock{0}%
\else
% we are externalizing. Query the lock's value:
\tikzexternalauxlock@getlockvalue
\if1\tikzexternallocked
\pgfexternalreadmainauxfalse
% the main .aux file won't be read. Handle \ref commands:
\tikzexternalauxlock@handleref
\else
\pgfexternalreadmainauxtrue
\fi
\fi
\else
% NO-OP. We won't read the main aux file anyway.
\def\tikzexternalauxlock@setlock##1{}%
\fi
\fi
}
% defines \tikzexternallocked to be either 0 or 1, depending on the
% lock file.
\def\tikzexternalauxlock@getlockvalue{%
\openin\r@pgf@reada=\tikzexternal@realjob.auxlock
\ifeof\r@pgf@reada
% no lock file. Unlock.
\def\tikzexternallocked{0}%
\else
% read first line...
\read\r@pgf@reada to\pgf@tempa
% ... and execute it.
\pgf@tempa
% it should contain a definition of \tikzexternallocked. If
% not, lock it anyway.
\pgfutil@ifundefined
{tikzexternallocked}
{\def\tikzexternallocked{1}}%
{}%
\fi
\closein\r@pgf@reada
}%
\def\tikzexternalauxlock@setlock#1{%
\immediate\openout\w@pgf@writea=\tikzexternal@realjob.auxlock
\immediate\write\w@pgf@writea{\noexpand\def\noexpand\tikzexternallocked{#1}}%
\immediate\closeout\w@pgf@writea
}
% Installs a special \ref{} command such that externalized pictures
% can use \ref and the user gets a warning if something fails.
%
% The special \ref handling is installed for every reference command
% in the list '/tikz/external/failed ref warnings for' which contains
% '\ref,\pageref,\cite'.
%
% For use in the aux lock handling only.
\def\tikzexternalauxlock@handleref{%
\let\pgf@external@grab@refundefinedtrue=\pgf@external@grab@refundefinedtrue@orig
%
\pgfkeysgetvalue{/tikz/external/failed ref warnings for}\tikzexternal@temp
\expandafter\tikzexternalauxlock@handleref@loop\tikzexternal@temp,\@EOI,%
}
\def\tikzexternalauxlock@handleref@loop#1,{%
\def\tikzexternal@temp{#1}%
\ifx\tikzexternal@temp\pgfutil@empty
\expandafter\tikzexternalauxlock@handleref@loop
\else
\ifx#1\@EOI
\else
\expandafter\let\csname tikzexternalauxlock@handleref@orig@\string#1\endcsname=#1%
\def#1{\tikzexternalauxlock@handleref@repl{#1}}%
\expandafter\expandafter\expandafter\tikzexternalauxlock@handleref@loop
\fi
\fi
}%
\def\tikzexternalauxlock@handleref@repl#1{\pgfutil@ifnextchar[{\tikzexternalauxlock@handleref@repl@{#1}}{\tikzexternalauxlock@handleref@repl@{#1}[]}}%
\def\tikzexternalauxlock@handleref@repl@#1[#2]#3{%
\csname tikzexternalauxlock@handleref@orig@\string#1\endcsname{#3}%
\begingroup
\def\n{\pgfexternal@hat\pgfexternal@hat J}%
\tikzexternal@assemble@systemcall{\pgfactualjobname}{\pgf@tempa}%
\def\space{\noexpand\space}%
\pgfexternalstorecommand{%
\noexpand\begingroup
\noexpand\toks0={\pgf@tempa}%
\noexpand\immediate\noexpand\write16{\tikzexternalauxlock@handleref@warning{#3}{\noexpand\the\noexpand\toks0}}%
\noexpand\G@refundefinedtrue
\noexpand\endgroup
}%
\endgroup
}%
\def\tikzexternalauxlock@handleref@warning#1#2{%
\n
LaTeX Warning: Reference `#1' in external picture `\pgfactualjobname' could not be resolved\noexpand\on@line.\n
This is because the \tikzexternal@realjob.aux file is not accessable in this context, you will need to issue the externalize command\n
\space\space\space#2\n
manually.\n%
}
% Expands to the default image extension (it is set by
% \tikzexternalize).
%
% This image extension may not resemble the correct one; you may need
% to overwrite this macro *after* \tikzexternalize in this case.
%
% The default setting uses the first registered image extension.
\def\tikzexternalimgextension{}%
% 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
\let\tikzexternal@curfilename=\relax
% 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
\global\tikzexternal@file@isuptodatetrue% may be set to false during checks.
\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
\def\tikzexternal@protocol@to@file##1{}% NOP
\tikzexternal@getnextfilename#1%
\pgfmath@smuggleone#1%
\endgroup
}%
% Returns the file name of the current picture.
%
% It returns the empty string in case the command is invoked outside
% of a picture.
% #1 the macro which will contain the file name.
% @see \tikzexternalgetnextfilename
\def\tikzexternalgetcurrentfilename#1{%
\ifx\tikzexternal@curfilename\relax
% we are outside of a picture.
\let#1=\pgfutil@empty
\else
% we are inside of a picture.
\let#1=\tikzexternal@curfilename
\fi
}
% 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
%
\tikzexternal@protocol@to@file#1%
}%
% #1: the image file name which should be protocolled. (can be a
% macro)
\def\tikzexternal@protocol@to@file#1{%
\ifpgf@external@grabshipout%
\else
\iftikzexternal@genfigurelist
\edef\tikzexternal@temp{#1}%
\ifx\tikzexternal@temp\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'
\tikzexternal@list@and@make@prepare
\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@list@and@make@gentarget{#1}%
\fi
\fi
\fi
\fi
}%
\def\tikzexternal@list@and@make@gentarget#1{%
\tikzexternal@assemble@systemcall{#1}{\pgf@tempa}%
\iftikzexternal@verboseio
\immediate\write16{Writing '#1' to '\tikzexternal@realjob.makefile'.}%
\fi
\global\tikzexternal@file@isuptodatetrue% only check for force remake:
\tikzexternal@checkforceremake%
\immediate\write\tikzexternal@outmakefile{#1\tikzexternalimgextension: \iftikzexternal@file@isuptodate\else FORCEREMAKE\fi}%
\immediate\write\tikzexternal@outmakefile{\tikzexternal@TABchar\pgf@tempa}%
\immediate\write\tikzexternal@outmakefile{}%
}
\def\tikzexternal@list@and@make@prepare{%
\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\tikzexternalimgextension)}%
\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{}%
\immediate\write\tikzexternal@outmakefile{FORCEREMAKE:}%
\immediate\write\tikzexternal@outmakefile{}%
%
% support for .dep files and auto-dependencies:
\immediate\write\tikzexternal@outmakefile{include $(ALL_FIGURE_NAMES:\tikzexternal@PERCENTchar=\tikzexternal@PERCENTchar.\tikzexternaldepext)}%
\immediate\write\tikzexternal@outmakefile{}%
\tikzexternalmakefiledefaultdeprule
\immediate\write\tikzexternal@outmakefile{}%
\tikzexternal@outmakefile@pendingcommands
\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. =====}}%
}%
}%
\def\tikzexternaldepext{dep}
\def\tikzexternalmakefiledefaultdeprule{%
\immediate\write\tikzexternal@outmakefile{\tikzexternal@PERCENTchar.\tikzexternaldepext:}%
\immediate\write\tikzexternal@outmakefile{\tikzexternal@TABchar touch $@ \tikzexternal@HASHchar\space will be filled later.}%
}%
% Invokes '#1' if a makefile is to be written and '#2' if not.
\def\tikzexternalifwritesmakefile#1#2{%
\if5\tikzexternal@opmode #1\else #2\fi
}%
\def\tikzexternal@outmakefile@pendingcommands{}%
% Will write something to the make file. If the makefile is not yet
% opened, #1 will be written as soon as it *is* opened.
\def\tikzexternalwritetomakefile#1{%
\pgfutil@ifundefined{tikzexternal@outmakefile}{%
\expandafter\gdef\expandafter\tikzexternal@outmakefile@pendingcommands\expandafter{%
\tikzexternal@outmakefile@pendingcommands
\immediate\write\tikzexternal@outmakefile{#1}%
}%
}{%
\immediate\write\tikzexternal@outmakefile{#1}%
}%
}%
\def\tikzexternal@dep@file@name{}
% Adds a dependency for the externalized picture file name `#1',
% namely another file `#2'.
%
% #1: the fully qualified path name (without image extension) of the
% external graphics for which we are adding a dependency.
% #2: a file name. If this file gets changed, #1 shall be remade.
%
% This command will be actived as soons as \tikzexternalize has been
% called.
\def\tikzexternalfiledependsonfile@ACTIVE#1#2{%
\begingroup
\def\tikzpicturedependsonfile@name{#1}%
\ifx\tikzpicturedependsonfile@name\pgfutil@empty
% could be auto-generated: empty file name means "picture
% won't be externalized". Skip it.
\else
\edef\tikzpicturedependsonfile@name{#1.\tikzexternaldepext}%
\ifx\tikzexternal@dep@file@name\tikzpicturedependsonfile@name
% file is open and ready.
\else
% create new output file.
\pgfutil@ifundefined{tikzexternal@dep@file}{%
\begingroup
% this makes \tikzexternal@dep@file global:
\globaldefs=1
% this gets round '\outer\newwrite' in plain TeX:
\csname newwrite\endcsname\tikzexternal@dep@file
\endgroup
}{}%
\ifx\tikzexternal@dep@file@name\pgfutil@empty
\else
% it is already open. Close it.
\immediate\closeout\tikzexternal@dep@file
\fi
\global\let\tikzexternal@dep@file@name=\tikzpicturedependsonfile@name
\immediate\openout\tikzexternal@dep@file=\tikzexternal@dep@file@name\relax
\fi
\immediate\write\tikzexternal@dep@file{#1\tikzexternalimgextension: #2}%
\fi
\endgroup
}%
\def\tikzexternalfiledependsonfile#1#2{}% NO-OP until \tikzexternalize has been called.
% Adds a dependency for the NEXT picture which is about to be
% externalized. If we are currently inside of a picture, the
% dependency is added for this current picture.
%
% #1: a file name. If this file gets changed, the tikzpicture's
% externalized graphics shall be remade.
%
% This command will be actived as soons as \tikzexternalize has been
% called.
\def\tikzpicturedependsonfile@ACTIVE#1{%
\begingroup
\tikzexternalgetcurrentfilename\tikzpicturedependsonfile@name
\ifx\tikzpicturedependsonfile@name\pgfutil@empty
\tikzexternalgetnextfilename\tikzpicturedependsonfile@name
\fi
\expandafter\tikzexternalfiledependsonfile\expandafter{\tikzpicturedependsonfile@name}{#1}%
\endgroup
}%
\def\tikzpicturedependsonfile#1{}% NO-OP until \tikzexternalize has been called.
% 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
}%
% 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
\let\tikzexternal@curfilename=\relax
}
% 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{%
\pgfkeysvalueof{/tikz/external/image discarded text}%
\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
\catcode`\#=12
\gdef\tikzexternal@HASHchar{#}
\gdef\tikzexternal@TABchar{^^I}\gdef\tikzexternal@PERCENTchar{%}\xdef\tikzexternal@DOLLARchar{$}}
% Creates the '/tikz/external/system call' command as string and
% returns it into the (global!) macro #2.
% #1: the image file name (as returned by
% \tikzexternalgetnextfilename)
% #2: the global return value macro
%
\def\tikzexternal@assemble@systemcall#1#2{%
\begingroup
\def\image{#1}%
\iftikzexternal@auto@detect@jobname
\edef\texsource{\string\def\string\tikzexternalrealjob{\tikzexternal@realjob}\string\input{\tikzexternal@realjob}}%
\else
\let\texsource=\tikzexternal@realjob
\fi
\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#2{\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{\tikzexternal@curfilename}{\pgf@tempa}%
\iftikzexternal@verboseio
\immediate\write16{===== 'mode=convert with system call': Invoking '\pgf@tempa' ========}%
\fi
%
\tikzexternalauxlock@setlock1%
\immediate\write18{\pgf@tempa}%
\tikzexternalauxlock@setlock0%
\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
}%
% Sets \iftikzexternal@file@isuptodate to false if one of the "force
% remake" things is active.
\def\tikzexternal@checkforceremake{%
\iftikzexternal@force@remake
\global\tikzexternal@file@isuptodatefalse
\else
\iftikzexternalremakenext
\global\tikzexternal@file@isuptodatefalse
\global\tikzexternalremakenextfalse
\fi
\fi
}
% 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{%
\tikzexternal@checkforceremake
\iftikzexternal@file@isuptodate
% 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
\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
}%
% Overwrite error message of pgf.
% This happens if the generated image was empty, i.e. if there was no \shipout.
\def\pgfexternal@error@no@shipout{%
\begingroup
\tikzexternal@assemble@systemcall{\pgfactualjobname}{\pgf@tempa}%
\toks0=\expandafter{\pgf@tempa}%
\PackageError{tikz}{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!?
\if\tikzexternal@opmode4 ^^J
\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
\space\space Possible solutions in this case:^^J
\space\space a) Try using 'mode=list and make',^^J
\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
\space\space Repeat: the resulting image was EMPTY, your attention is required
\else
\if\tikzexternal@opmode5 ^^J
\space\space You are currently using 'mode=list and make'. Possible solutions:^^J
\space\space a) Try to delete `\tikzexternal@realjob.makefile'. Perhaps it is not up-to-date.^^J
\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
\space\space Repeat: the resulting image was EMPTY, your attention is required
\fi
\fi}%
\endgroup
}%
|