Debian Codegraph @

texmf-dist/tex/latex/pgf/utilities/tikzexternal.sty

raw
1 % Copyright 2020 by Christian Feuersaenger
2 %
3 % This file may be distributed and/or modified
4 %
5 % 1. under the LaTeX Project Public License and/or
6 % 2. under the GNU Public License.
7 %
8 % See the file doc/generic/pgf/licenses/LICENSE for more details.
9 %
10 % In addition to the above licenses, this particular file may also be
11 % directly inlined into other documents and this copyright notice may
12 % be removed. Additionally, when you inline this file, you may modify
13 % it as needed and you may remove comments.
14 %
15 % This file is intended to be distributed or even inlined into a file
16 % when a document needs to be typeset on a system that does not have
17 % pgf/TikZ installed.
18 %
19 % It replaces \tikzpicture/ \endtikzpicture and \tikz and invokes the
20 % \includegraphics with the correct file name instead.
21
22 \RequirePackage{graphicx}
23
24 \toksdef\t@tikzexternal@tmpa=0
25 \toksdef\t@tikzexternal@tmpb=1
26
27 \def\tikzexternalize{\@ifnextchar[{\tikzexternalize@opt}{\tikzexternalize@opt[]}}%
28 \def\tikzexternalize@opt[#1]{\@ifnextchar\bgroup{\def\tikzexternal@realjob}{\let\tikzexternal@realjob=\jobname}}%
29
30 \def\tikzsetnextfilename#1{\gdef\tikzexternal@nextfile{#1}}
31 \def\tikzsetexternalprefix#1{\def\tikzexternal@filenameprefix{#1}}
32 \def\tikzsetfigurename#1{\def\tikzexternal@figurename{#1}}%
33 \def\tikzappendtofigurename#1{\expandafter\def\expandafter\tikzexternal@figurename\expandafter{\tikzexternal@figurename #1}}%
34 \tikzsetnextfilename{}
35 \tikzsetexternalprefix{}
36 \tikzsetfigurename{\tikzexternal@realjob-figure}%
37
38 \long\def\tikzpicture#1\end#2{% collect every thing up to \end{tikzpicture}
39 \def\tikzexternal@laTeX@collectpicture@@{#2}%
40 \ifx\tikzexternal@laTeX@collectpicture@@\tikzexternal@laTeX@tikzpicturestring
41 \tikzexternal@image
42 \end{tikzpicture}%
43 \else
44 \expandafter
45 \tikzpicture
46 \fi
47 }%
48 \def\endtikzpicture{}
49 \def\tikzexternal@laTeX@tikzpicturestring{tikzpicture}%
50
51 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 % Replacement for \tikz short command:
53 \def\tikz{\@ifnextchar[{\tikz@opt}{\tikz@opt[]}}
54 \def\tikz@opt[#1]{\@ifnextchar\bgroup{\tikz@opt@{#1}}{\tikz@opt@@{#1}}}
55 \def\tikz@opt@#1#2{\tikz@opt@process{#1}{#2}}
56 \def\tikz@opt@@#1{%
57 \def\tikz@next{\tikz@collectnormalsemicolon{#1}}%
58 \ifnum\the\catcode`\;=\active\relax%
59 \def\tikz@next{\tikz@collectactivesemicolon{#1}}%
60 \fi%
61 \tikz@next}
62 \def\tikz@collectnormalsemicolon#1#2;{\tikz@opt@process{#1}{#2;}}
63 {
64 \catcode`\;=\active
65 \gdef\tikz@collectactivesemicolon#1#2;{%
66 \tikz@opt@process{#1}{#2;}%
67 }
68 }
69 \def\tikz@opt@process#1#2{\tikzexternal@image}
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71
72 \def\tikzexternal@image{%
73 \leavevmode
74 \begingroup
75 \t@tikzexternal@tmpa=\expandafter{\tikzexternal@filenameprefix}%
76 \ifx\tikzexternal@nextfile\empty
77 \expandafter\ifx\csname c@tikzext@no@\tikzexternal@figurename\endcsname\relax
78 \expandafter\gdef\csname c@tikzext@no@\tikzexternal@figurename\endcsname{0}%
79 \fi
80 \t@tikzexternal@tmpb=\expandafter{\tikzexternal@figurename}%
81 \count0=\csname c@tikzext@no@\tikzexternal@figurename\endcsname
82 \xdef\tikzexternal@curfilename{\the\t@tikzexternal@tmpa\the\t@tikzexternal@tmpb\the\count0 }%
83 \advance\count0 by1\relax
84 \expandafter\xdef\csname c@tikzext@no@\tikzexternal@figurename\endcsname{\the\count0 }%
85 \else
86 \t@tikzexternal@tmpb=\expandafter{\tikzexternal@nextfile}%
87 \xdef\tikzexternal@curfilename{\the\t@tikzexternal@tmpa\the\t@tikzexternal@tmpb}%
88 \fi
89 \endgroup
90 \global\let\tikzexternal@nextfile=\empty
91 \pgfincludeexternalgraphics\tikzexternal@curfilename
92 }%
93 \def\tikzset#1{}\def\pgfkeys#1{}\def\pgfqkeys#1#2{}
94 \long\def\beginpgfgraphicnamed#1#2\endpgfgraphicnamed{\pgfincludeexternalgraphics{#1}}
95 \def\tikzifinpicture#1#2{#2}
96 \def\pgfincludeexternalgraphics#1{%
97 \begingroup
98 \pgfexternalreaddpth{#1}%
99 \setbox1=\hbox{\includegraphics{#1}}%
100 \ifdim\pgfretval=0pt
101 \box1
102 \else
103 \dimen0=\pgfretval\relax
104 \hbox{\lower\dimen0 \box1 }%
105 \fi
106 \endgroup
107 }
108 \newread\r@pgf@reada
109 \def\pgfexternalreaddpth#1{%
110 \edef\pgfexternalreaddpth@restore{%
111 \noexpand\endlinechar=\the\endlinechar\space
112 \noexpand\catcode`\noexpand\@=\the\catcode`\@\space
113 }%
114 \def\pgfretval{0pt}%
115 \endlinechar=-1 % suppress white space at end
116 \catcode`\@=11
117 \openin\r@pgf@reada=#1.dpth
118 \pgfincludeexternalgraphics@read@dpth
119 \pgfexternalreaddpth@restore
120 }%
121 \def\pgfincludeexternalgraphics@read@dpth{%
122 \ifeof\r@pgf@reada
123 \closein\r@pgf@reada
124 \else
125 \read\r@pgf@reada to\pgfincludeexternalgraphics@auxline
126 \ifx\pgfincludeexternalgraphics@auxline\empty
127 \else
128 \expandafter\pgfincludeexternalgraphics@read@dpth@line\pgfincludeexternalgraphics@auxline\pgfincludeexternalgraphics@read@dpth@line@EOI
129 \fi
130 \expandafter\pgfincludeexternalgraphics@read@dpth
131 \fi
132 }%
133 \long\def\pgfincludeexternalgraphics@read@dpth@line#1#2\pgfincludeexternalgraphics@read@dpth@line@EOI{%
134 \ifcat\noexpand#1\relax
135 \if@filesw
136 {\toks0={#1#2}%
137 \immediate\write\@auxout{\noexpand\def\noexpand\dpthimport{\the\toks0 }\noexpand\dpthimport }%
138 }%
139 \fi
140 \else%
141 \def\pgfretval{#1#2}%
142 \fi
143 }%