Debian Codegraph @

texmf-dist/tex/generic/pgf/basiclayer/pgfcoreexternal.code.tex

raw
1 % Copyright 2019 by Till Tantau
2 %
3 % This file may be distributed and/or modified
4 %
5 % 1. under the LaTeX Project Public License and/or
6 % 2. under the GNU Public License.
7 %
8 % See the file doc/generic/pgf/licenses/LICENSE for more details.
9
10 \ProvidesFileRCS{pgfcoreexternal.code.tex}
11
12
13
14 \newif\ifpgfexternal@aux@in@dpth
15 \newif\ifpgfexternal@info
16
17 \pgfkeys{
18 /pgf/images/include external/.code={\pgfimage{#1}},
19 /pgf/images/aux in dpth/.is if=pgfexternal@aux@in@dpth,
20 /pgf/images/aux in dpth/.default=true,
21 /pgf/images/trim external/.code n args={4}{%
22 \def\pgf@external@kern@left{#1}%
23 \def\pgf@external@kern@bottom{#2}%
24 \def\pgf@external@kern@right{#3}%
25 \def\pgf@external@kern@top{#4}%
26 \ifx\pgf@external@kern@left\pgfutil@empty\def\pgf@external@kern@left{0sp}\fi%
27 \ifx\pgf@external@kern@bottom\pgfutil@empty\def\pgf@external@kern@bottom{0sp}\fi%
28 \ifx\pgf@external@kern@right\pgfutil@empty\def\pgf@external@kern@right{0sp}\fi%
29 \ifx\pgf@external@kern@top\pgfutil@empty\def\pgf@external@kern@top{0sp}\fi%
30 },
31 /pgf/images/trim external={1truein}{1truein}{1truein}{1truein},
32 /pgf/images/external info/.is if=pgfexternal@info,
33 /pgf/images/external info/.default=true,
34 }
35
36 % The external read/write mechanism is used as follows:
37 %
38 % In your document, you put a number or all of your pgfpicture (or
39 % tikzpicture) environments inside a pair of \beginpgfexternal and
40 % \endpgfexternal. The \beginpgfexternal gets one parameter called the
41 % graphics name.
42 %
43 % Once this is done, the next step is to declare the names of your
44 % graphics in the document at the beginning using the
45 % \pgfdeclaregraphicname command.
46 %
47 % Now, when you typeset your document, each time a \beginpgfexternal
48 % command is encountered, it will be checked whether the corresponding
49 % graphics file exists. If this is the case, it will be included and
50 % the code between \beginpgfexternal and \endpgfexternal is
51 % ignored. Otherwise, the code is executed normally.
52 %
53 % Things change when you run tex on your file with the \jobname set to
54 % the name of a declared graphics name. In this case, only your
55 % graphic will be typeset, the rest of the document will be "thrown away".
56 % To be more precise, everything is gobbled up to the beginning
57 % of the first \beginpgfexternal with the given graphics name. Then,
58 % the content of the "environment" is put in a box (this "environment"
59 % need not contain a graphics, it may contain anything that can be put
60 % in a box). Then a single page is output whose size is exactly the
61 % size of the picture.
62 %
63 % So, once you have run tex repeatedly, each time with the jobname set
64 % to each of the graphics, you can then run tex on the whole document
65 % and this will cause all graphics to be included rather than begin
66 % typeset.
67 %
68 % Finally, once all the graphics have been created, you can also say
69 % \input pgfexternal.tex instead of including pgf/TikZ.
70
71
72 \newif\ifpgfexternalreadmainaux
73 \pgfexternalreadmainauxtrue
74
75 % Tells pgf which jobname is the name of the real file
76 %
77 % #1 = name
78 %
79 % Description:
80 %
81 % This command is used to tell pgf that the file named #1 should be
82 % typeset normally. If \jobname is not equal to #1, only
83 % the graphic called \jobname will be typeset.
84 %
85 % Example:
86 %
87 % \pgfrealjobname{survey}
88 %
89 % Note that afterwards, \jobname will be set to the argument. In our
90 % example, \jobname will be 'survey'. This is to accomplish
91 % compatibility with aux-file generation.
92
93 \def\pgfrealjobname#1{%
94 \global\let\pgfactualjobname=\jobname
95 \edef\pgf@tempa{\expandafter\string\csname #1\endcsname}%
96 \edef\pgf@tempb{\expandafter\string\csname\jobname\endcsname}%
97 \ifx\pgf@tempa\pgf@tempb%
98 \else%
99 \pgf@external@grabshipouttrue%
100 \pgfexternal@nofiles% see also \pgf@external@init@aux@in@dpth
101 \ifpgfexternalreadmainaux
102 % and reset the jobname. This should allow to handle any
103 % \label/\ref constructions which are stored in \jobname.aux (and
104 % which won't be found otherwise)
105 \gdef\jobname{#1}%
106 \fi
107 \ifpgfexternal@aux@in@dpth
108 \csname newwrite\endcsname\w@pgfexternal@auxout
109 \fi
110 \pgfexternal@geometry@compatibility
111 \fi%
112 \gdef\pgfrealjobname##1{}% avoid multiple calls.
113 }
114
115 \def\pgfexternal@geometry@compatibility{%
116 % check if \usepackage{geometry} is loaded:
117 \pgfutil@ifundefined{geometry}{%
118 }{%
119 % The geometry package hooks into \begin{document}, evaluates
120 % its driver -- and generates \special instructions to set the
121 % paper size.
122 %
123 % Unfortunately, this confuses dvips utterly - and perhaps
124 % other drivers as well.
125 %
126 % Thus, if we currently about to generate some external
127 % graphics, we WANT to disable geometry (it has no purpose in
128 % this context anyway).
129 %
130 % This here works well:
131 \pgf@typeout{Package TikZ externalization: calling \string\geometry{driver=none} during externalization.^^J}%
132 \geometry{driver=none}%
133 }%
134 }%
135
136 \def\pgfexternal@nofiles{%
137 % replace \relax. The \nofiles macros does
138 % \let\makeglossary=\relax
139 % but the glossary.sty calls \renewcommand\makeglossary (which
140 % will fail if \makeglossary=\relax). Stupid, but it works.
141 \let\pgfexternal@nofiles@=\relax
142 \def\relax{\relax}%
143 %
144 % suppress generation of LaTeX .aux, .toc etc files.
145 % generation of these files is not thread-safe.
146 % the \csname \endcsname yields \relax if \nofiles doesn't exist.
147 \csname nofiles\endcsname
148 %
149 \let\relax=\pgfexternal@nofiles@
150 }%
151
152 \newif\ifpgf@external@grabshipout
153 \newbox\pgfexternal@startupbox
154
155 \pgfutil@ifundefined{AtBeginDocument}{}{%
156 \AtBeginDocument{%
157 \ifpgf@external@grabshipout%
158 \global\let\pgfexternal@originalshipout=\shipout%
159 \global\def\shipout{\setbox\pgfpic=}%
160 \maxdeadcycles=10000%
161 % Ok, gather everything we have seen up to now in a box. This box
162 % will contain any specials that have been used.
163 {%
164 \output{\global\setbox\pgfexternal@startupbox=\vbox{\csname @begindvi\endcsname\unvbox255}}%
165 \hbox{}\eject%
166 }%
167 \fi%
168 }
169 }%
170
171 \pgfutil@ifundefined{AtEndDocument}{}{%
172 \AtEndDocument{%
173 \ifpgf@external@grabshipout
174 \pgfutil@ifundefined{pgfexternal@did@a@shipout}{%
175 \pgfexternal@error@no@shipout
176 }{}%
177 \fi
178 }%
179 }
180
181 % This will be overwritten by the tikz external lib if it is loaded.
182 \def\pgfexternal@error@no@shipout{%
183 \pgferror{Sorry, image externalization failed: the resulting image was EMPTY. I tried to externalize '\pgfactualjobname'. Perhaps there was a typo somewhere? Please check that your document contains '\string\beginpgfgraphicnamed{\pgfactualjobname} ... \string\endpgfgraphicnamed'}%
184 }%
185
186
187 % "Environment" for an external graphic.
188 %
189 % #1 = graphic name
190 %
191 % Example:
192 %
193 % \beginpgfgraphicnamed{main-graphic2}
194 % \begin{tikzpicture}
195 % ...
196 % \end{tikzpicture}
197 % \endpgfgraphicnamed
198
199 \def\pgf@externalbegin#1{%
200 % First, check whether we wish to grab this graphic:
201 \let\pgf@next=\pgf@external@normal%
202 \ifpgf@external@grabshipout%
203 \edef\pgf@tempa{\expandafter\string\csname #1\endcsname}%
204 \edef\pgf@tempb{\expandafter\string\csname\pgfactualjobname\endcsname}%
205 \ifx\pgf@tempa\pgf@tempb%
206 \let\pgf@next=\pgf@external@grab%
207 \fi%
208 \fi%
209 \pgf@next{#1}%
210 }
211
212
213 \AtBeginDocument{
214 \let\beginpgfgraphicnamed=\pgf@externalbegin% overwrite definition of pgfexternal.tex if necessary
215 \let\endpgfgraphicnamed=\unskip
216 }
217
218
219 % Normal operation: Include an external graphic instead of the
220 % picture, if such an external picture exists.
221 \def\pgf@external@normal#1{%
222 \let\pgf@filename=\pgfutil@empty%
223 \expandafter\pgf@findfile\pgfsys@imagesuffixlist:+{#1}%
224 \ifx\pgf@filename\pgfutil@empty%
225 % Ok, no such image... Just typeset the picture normally.
226 \let\pgf@next=\relax%
227 \else%
228 \let\pgf@next=\pgf@replacepicturebygraphic%
229 \fi%
230 \pgf@next%
231 }
232
233 \long\def\pgf@replacepicturebygraphic#1\endpgfgraphicnamed{%
234 \expandafter\pgfincludeexternalgraphics\expandafter{\pgf@filename}\unskip
235 }
236
237 % This is almost the same as \includegraphics{#1}, but it checks
238 % whether '#1.dpth' exists. In such case, it restores the boxes depth
239 % stored in #1.dpth.
240 %
241 % Furthermore, the .dpth file may contain .aux related information
242 % collected for the external graphics (references). They start after
243 % the first line (if any).
244 \def\pgfincludeexternalgraphics#1{%
245 \begingroup
246 \pgfexternalreaddpth{#1}%
247 \pgfkeysifdefined{/pgf/images/include external/#1/.@cmd}{%
248 \pgfkeysgetvalue{/pgf/images/include external/#1/.@cmd}\pgf@tempa
249 \pgfkeyslet{/pgf/images/include external/.@cmd}\pgf@tempa
250 }{}%
251 \setbox1=\hbox{\pgfkeysvalueof{/pgf/images/include external/.@cmd}{#1}\pgfeov}%
252 \ifdim\pgfexternaltrimleft=0sp \else \kern\pgfexternaltrimleft\relax \fi
253 \ifdim\pgfexternaldepth=0pt
254 \box1
255 \else
256 \dimen0=\pgfexternaldepth\relax
257 \hbox{\lower\dimen0 \box1 }%
258 \fi
259 \ifdim\pgfexternaltrimright=0sp \else \kern\pgfexternaltrimright\relax \fi
260 \endgroup
261 }
262
263 % Reads the .dpth file which contains meta data of the external
264 % picture.
265 %
266 % This command handles the following stuff:
267 % - it defines \pgfexternaldepth . The macro contains the contents of
268 % a line which does not start with a control sequence (for example a
269 % line containing '50pt')
270 % - it defines \pgfexternaltrimleft and \pgfexternaltrimright
271 % - it checks if the .dpth file contains content stored by means of
272 % \pgfexternalstorecommand. If so, the argument of
273 % \pgfexternalstorecommand will be restored.
274 % - anything else will be appended to the main .aux file, assuming that
275 % it contains \label or \ref information (only if the LaTeX switch
276 % \if@filesw is \iftrue).
277 %
278 %
279 % #1: the image file name (such that #1.dpth exists)
280 \def\pgfexternalreaddpth#1{%
281 % no \begingroup. Handle that manually:
282 \edef\pgfexternalreaddpth@restore{%
283 \noexpand\endlinechar=\the\endlinechar\space
284 \noexpand\catcode`\noexpand\@=\the\catcode`\@\space
285 }%
286 %
287 \def\pgfexternaldepth{0sp}%
288 \def\pgfexternaltrimleft{0sp}%
289 \def\pgfexternaltrimright{0sp}%
290 \endlinechar=-1 % suppress white space at end
291 \catcode`\@=11
292 \openin\r@pgf@reada=#1.dpth
293 \pgfincludeexternalgraphics@read@dpth
294 %
295 \pgfexternalreaddpth@restore
296 }%
297 %
298 % The .dpth consists of 0-N lines, where each is either a single
299 % dimension (the box' depth) or something which belongs to the .aux
300 % file (such lines will always start with a macro).
301 \def\pgfincludeexternalgraphics@read@dpth{%
302 \ifeof\r@pgf@reada
303 \closein\r@pgf@reada
304 \else
305 \read\r@pgf@reada to\pgfincludeexternalgraphics@auxline
306 \ifx\pgfincludeexternalgraphics@auxline\pgfutil@empty
307 \else
308 \expandafter\pgfincludeexternalgraphics@read@dpth@line\pgfincludeexternalgraphics@auxline\pgfincludeexternalgraphics@read@dpth@line@EOI
309 \fi
310 \expandafter\pgfincludeexternalgraphics@read@dpth
311 \fi
312 }%
313
314 \def\pgfexternal@restore#1{#1}%
315
316 \long\def\pgfincludeexternalgraphics@read@dpth@line@getfirst#1#2\pgfincludeexternalgraphics@read@dpth@line@EOI{%
317 \def\pgf@first{#1}%
318 }%
319 \long\def\pgfincludeexternalgraphics@read@dpth@line#1\pgfincludeexternalgraphics@read@dpth@line@EOI{%
320 \pgfincludeexternalgraphics@read@dpth@line@getfirst#1\pgfincludeexternalgraphics@read@dpth@line@EOI
321 \expandafter\ifcat\expandafter\noexpand\pgf@first\relax
322 % Ah -- the first token is a control sequence. It belongs to
323 % the .aux file.
324 %
325 \expandafter\ifx\pgf@first\pgfexternal@restore
326 % if the first line in the .dpth file starts with the
327 % magic string \pgfexternal@restore, the complete line
328 % will be executed as-is. Example:
329 % \pgfexternal@restore{\def\somevalue{1}}
330 #1%
331 \else
332 % do NOT execute #1! many LaTeX commands don't support it (\label for example)
333 \pgfutil@ifundefined{if@filesw}{%
334 % sorry, .aux file support only for latex
335 }{%
336 % append to main .aux file (for forward references)
337 \if@filesw
338 {%
339 \toks0={#1}%
340 % believe it or not, but the
341 % \def\dpthimport{...}\dpthimport *makes* a
342 % difference! In ensures any occurring `##' characters are properly expanded to `#'.
343 \immediate\write\@auxout{%
344 \noexpand\def\noexpand\dpthimport{\the\toks0}\noexpand\dpthimport}%
345 }%
346 \fi
347 }%
348 \fi
349 \else% it is the depth (which is simply a number for backwards compatibility)
350 \def\pgfexternaldepth{#1}%
351 \fi
352 }%
353
354 % Stores '#1' (expanded!) into the .dpth file of the currently
355 % exported image.
356 %
357 % This command has only an effect if an image is being exported.
358 %
359 % The stored contents will be read in the context where
360 % /pgf/images/include external is active (i.e. by
361 % \pgfexternalreaddpth).
362 %
363 \def\pgfexternalstorecommand#1{%
364 }%
365 \def\pgfexternalstorecommand@isexporting#1{%
366 \immediate\write\w@pgfexternal@auxout{\noexpand\pgfexternal@restore{#1}}%
367 }%
368
369
370
371 % Grab operation: If jobname matches the graphic name, typeset this
372 % picture normal.
373 % REMARK:
374 % this method is also invoked from within the tikz external library.
375 \def\pgf@external@grab#1{%
376 \def\pgf@filename{#1}%
377 \ifpgfexternal@aux@in@dpth
378 \begingroup
379 \pgf@external@init@aux@in@dpth
380 \let\G@refundefinedtrue=\pgf@external@grab@refundefinedtrue
381 \fi
382 \gdef\pgf@trimleft@final{0sp}%
383 \gdef\pgf@trimright@final{0sp}%
384 \setbox\pgfpic=\hbox\bgroup\bgroup%
385 \let\endpgfgraphicnamed=\pgf@externalend%
386 }
387
388 {\catcode`\^=12 \gdef\pgfexternal@hat{^}}
389
390 \expandafter\let\expandafter\pgf@external@grab@refundefinedtrue@orig\csname G@refundefinedtrue\endcsname
391
392 \def\pgf@external@grab@refundefinedtrue{%
393 \pgf@external@grab@refundefinedtrue@orig
394 \begingroup
395 \def\n{\pgfexternal@hat\pgfexternal@hat J}%
396 \pgfexternalstorecommand{%
397 \pgf@external@grab@refundefinedtrue@code
398 \noexpand\immediate\noexpand\write16{\pgf@external@grab@refundefinedtrue@warning}%
399 \noexpand\G@refundefinedtrue
400 }%
401 \endgroup
402 }%
403
404 % A hook. will be overwritten by \usetikzlibrary{external}
405 \def\pgf@external@grab@refundefinedtrue@code{}
406
407 \def\pgf@external@grab@refundefinedtrue@warning{%
408 LaTeX Warning: External picture `\pgfactualjobname' contains undefined references\noexpand\on@line.\n
409 }%
410
411 \let\pgf@external@@protected@write@orig=\protected@write
412 \long\def\pgf@external@@protected@write@immediate#1#2#3{%
413 \begingroup
414 \let\pgf@write@=\write
415 \def\write{\noexpand\immediate\pgf@write@}%
416 \pgf@external@@protected@write@orig{#1}{#2}{#3}%
417 \endgroup
418 }%
419
420 \def\pgf@external@init@aux@in@dpth{%
421 \let\pgfexternalstorecommand=\pgfexternalstorecommand@isexporting
422 %
423 % tell LaTeX to write aux files...
424 \csname @fileswtrue\endcsname
425 % ... but redirect output to the .dpth file!
426 \immediate\openout\w@pgfexternal@auxout=\[email protected]
427 \let\@auxout=\w@pgfexternal@auxout
428 % ... and disable the correct page numbers. I can't get that
429 % (because the correct page number is only available in the
430 % shipout routine). Use immediate output:
431 \let\protected@write=\pgf@external@@protected@write@immediate
432 }%
433
434 \def\pgf@externalend@storeshifts#1{%
435 \immediate\write#1{\the\dp\pgfpic}% store the depth simply as number for backw. compat.
436 \ifx\pgf@trimleft@final\pgfutil@empty\else
437 \immediate\write#1{\noexpand\pgfexternal@restore{\noexpand\def\noexpand\pgfexternaltrimleft{\pgf@trimleft@final}}}%
438 \fi
439 \ifx\pgf@trimright@final\pgfutil@empty\else
440 \immediate\write#1{\noexpand\pgfexternal@restore{\noexpand\def\noexpand\pgfexternaltrimright{\pgf@trimright@final}}}%
441 \fi
442 \ifpgfexternal@info
443 \immediate\write#1{\noexpand\pgfexternal@restore{\noexpand\def\noexpand\pgfexternalwidth{\the\wd\pgfpic}}}%
444 \immediate\write#1{\noexpand\pgfexternal@restore{\noexpand\def\noexpand\pgfexternalheight{\the\dimen0}}}%
445 \fi
446 }
447 % REMARK:
448 % this method is also invoked from within the tikz external library.
449 \def\pgf@externalend{%
450 \unskip\egroup\egroup%
451 {%
452 \def\pgf@external@trim{0}%
453 \def\pgf@external@store@dpth{0}%
454 %
455 \ifpgf@trim@lowlevel
456 \gdef\pgf@trimleft@final{}%
457 \gdef\pgf@trimright@final{}%
458 \else
459 \ifdim\pgf@trimleft@final=0sp \gdef\pgf@trimleft@final{} \else\def\pgf@external@trim{1}\fi
460 \ifdim\pgf@trimright@final=0sp \gdef\pgf@trimright@final{} \else\def\pgf@external@trim{1}\fi
461 \if1\pgf@external@trim
462 % UNDO the trimming! export to pdf doesn't supported trimmed
463 % bounding boxes (has to do with the mediabox/trimbox etc).
464 % I'll keep the bounding box intact and store the trim information
465 % into the .dpth file.
466 \setbox\pgfpic=\hbox{%
467 \ifx\pgf@trimleft@final\pgfutil@empty\else\kern-\pgf@trimleft@final\fi
468 \box\pgfpic
469 \ifx\pgf@trimright@final\pgfutil@empty\else\kern-\pgf@trimright@final\fi
470 }%
471 \def\pgf@external@store@dpth{1}%
472 \fi
473 \fi
474 %
475 \parindent0pt % leave the space
476 % \leftmargin0pt%
477 % \rightmargin0pt%
478 \hoffset=0pt % reset to default
479 \voffset=0pt
480 \dimen0\ht\pgfpic%
481 \advance\dimen0\dp\pgfpic%
482 \ifdim\dp\pgfpic=0pt\relax
483 \else% store the picture's depth. Otherwise, it would be lost.
484 \def\pgf@external@store@dpth{1}%
485 \fi
486 \ifpgfexternal@info
487 \def\pgf@external@store@dpth{1}%
488 \fi
489 %
490 \if1\pgf@external@store@dpth
491 \ifpgfexternal@aux@in@dpth
492 \pgf@externalend@storeshifts\@auxout
493 \immediate\closeout\@auxout
494 \else
495 \immediate\openout\pgf@plotwrite=\[email protected]
496 \pgf@externalend@storeshifts\pgf@plotwrite
497 \immediate\closeout\pgf@plotwrite
498 \fi
499 \fi
500 \pgfsys@prepare@papersize{\the\wd\pgfpic}{\the\dimen0}%
501 %
502 \setbox0=\vbox{%
503 % I admit that I don't know why there is this kerning:
504 \kern -\pgf@external@kern@top\relax%
505 \hbox{%
506 \kern -\pgf@external@kern@left\relax%
507 \hbox to0pt{%
508 \wd\pgfexternal@startupbox=0pt %
509 \ht\pgfexternal@startupbox=0pt %
510 \dp\pgfexternal@startupbox=0pt %
511 \box\pgfexternal@startupbox%
512 \pgfsys@atbegindocument\hss}%
513 \box\pgfpic%
514 \kern\pgf@external@kern@right\relax%
515 }%
516 \kern\pgf@external@kern@bottom\relax%
517 }%
518 %
519 % compatibility with eso-pic package: \nointerlineskip is not
520 % allowed here, but the eso-pic package uses \nointerlineskip in
521 % its shipout routine.
522 \let\pgfexternal@nointerlineskip@orig=\nointerlineskip
523 \let\nointerlineskip=\pgfexternal@nointerlineskip
524 %
525 \pgfexternal@originalshipout\box0 %
526 \gdef\pgfexternal@did@a@shipout{1}%
527 }%
528 \ifpgfexternal@aux@in@dpth
529 \endgroup
530 \fi
531 }
532
533 \def\pgfexternal@nointerlineskip{%
534 \ifvmode
535 \pgfexternal@nointerlineskip@orig
536 \else
537 \relax% \nointerlineskip in horizontal mode not allowed. Silently ignore the error message.
538 \fi
539 }%
540
541 \endinput
542 % vi: tabstop=4 shiftwidth=2 expandtab