| 1 |
% Copyright 2019 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 |
% |
| 11 |
% This file provides a high-level automatic export feature for tikz pictures. |
| 12 |
% It allows to export EACH SINGLE FIGURE into a separate PDF. |
| 13 |
% |
| 14 |
% The simplest way is to use |
| 15 |
% |
| 16 |
% \tikzexternalize{myarticle} |
| 17 |
% |
| 18 |
% it simply converts every figure using the write18 method. If that is not possible, |
| 19 |
% you need to reconfigure \tikzexternalize: it can generate a list of figure file names |
| 20 |
% and you need to generate these figures manually (or with a script). |
| 21 |
% |
| 22 |
% It replaces \tikzpicture/ \endtikzpicture and \tikz and invokes \beginpgfgraphicnamed ... \endpgfgraphicnamed |
| 23 |
% commands if necessary. |
| 24 |
|
| 25 |
% load this package in order to get \pdf@mdfivesum . |
| 26 |
% \pdf@mdfivesum is available for pdftex and lualatex and computes the |
| 27 |
% md5 sum. Note that pdftex has \pdfmdfivesum, but lualatex does not |
| 28 |
% have it. |
| 29 |
% |
| 30 |
% ... also used for \pdf@shellescape. |
| 31 |
\usepackage{pdftexcmds} |
| 32 |
\pgfutil@IfFileExists{atveryend.sty}{% |
| 33 |
\usepackage{atveryend}% |
| 34 |
}{% |
| 35 |
\message{! Package tikz Warning: \string\usepackage{atveryend} impossible. Automatic resolving of references within external files impossible.}% |
| 36 |
}% |
| 37 |
|
| 38 |
% source generic implementation: |
| 39 |
\input tikzexternalshared.code.tex |
| 40 |
|
| 41 |
\pgfutil@IfUndefined{pdf@mdfivesum}{}{% |
| 42 |
\let\tikzexternal@mdfivesum=\pdf@mdfivesum |
| 43 |
}% |
| 44 |
|
| 45 |
\def\tikzexternal@TEXDIALECT@begpicture{\begin{tikzpicture}} |
| 46 |
\def\tikzexternal@TEXDIALECT@endpicture{\end{tikzpicture}}% |
| 47 |
|
| 48 |
% this latex specific this assumes it starts at the beginning of an |
| 49 |
% environment. The environment is named |
| 50 |
% '\tikzexternal@laTeX@collect@until@end@tikzpicturetikzpicturestring'. |
| 51 |
% |
| 52 |
% Everything up to \end{<name>} will be collected and passed as |
| 53 |
% argument to the macro '#1'. |
| 54 |
% |
| 55 |
% This does also work if <name> is NOT tikzpicture! |
| 56 |
\def\tikzexternal@TEXDIALECT@collectpicture#1{% |
| 57 |
\begingroup |
| 58 |
\t@tikzexternal@tmpa={}% |
| 59 |
\tikzexternal@laTeX@collect@until@end@tikzpicture{#1}% |
| 60 |
} |
| 61 |
|
| 62 |
% Loop until we find '\end{tikzpicture}'. We have to make sure that |
| 63 |
% even strings like '#1' are allowed inside of the collected argument. |
| 64 |
\long\def\tikzexternal@laTeX@collect@until@end@tikzpicture#1#2\end#3{% |
| 65 |
\def\tikzexternal@laTeX@collect@until@end@tikzpicture@{#3}% |
| 66 |
\ifx\tikzexternal@laTeX@collect@until@end@tikzpicture@\tikzexternal@laTeX@collect@until@end@tikzpicturetikzpicturestring |
| 67 |
\t@tikzexternal@tmpa=\expandafter{\the\t@tikzexternal@tmpa #2}% |
| 68 |
\edef\tikzexternal@TEXDIALECT@collectpicture@next{% |
| 69 |
\noexpand\endgroup |
| 70 |
\noexpand#1{\the\t@tikzexternal@tmpa}% |
| 71 |
}% |
| 72 |
\t@tikzexternal@tmpa={}% release memory. |
| 73 |
\else |
| 74 |
\t@tikzexternal@tmpa=\expandafter{\the\t@tikzexternal@tmpa #2\end{#3}}% |
| 75 |
\def\tikzexternal@TEXDIALECT@collectpicture@next{% |
| 76 |
\tikzexternal@laTeX@collect@until@end@tikzpicture{#1}}% |
| 77 |
\fi |
| 78 |
\tikzexternal@TEXDIALECT@collectpicture@next |
| 79 |
}% |
| 80 |
\def\tikzexternal@laTeX@collect@until@end@tikzpicturetikzpicturestring{tikzpicture}% |
| 81 |
|
| 82 |
\def\tikzexternal@handle@landscape{% |
| 83 |
\ltx@ifpackageloaded{pdflscape}{% |
| 84 |
% Work around a bug in conjunction with pdflscape and |
| 85 |
% external: without this optimize-away-instruction, the |
| 86 |
% externalized image would be rotated - and it will be rotated |
| 87 |
% once more again when it is included. |
| 88 |
% |
| 89 |
% if we "optimize it away", the landscape stuff will be |
| 90 |
% omitted. I hope this is enough... might not be entirely |
| 91 |
% fool-proof; perhaps we need to set it to \relax. For now, it |
| 92 |
% will probably do the job |
| 93 |
\tikzset{ |
| 94 |
external/.cd, |
| 95 |
optimize command away=\landscape, |
| 96 |
optimize command away=\endlandscape |
| 97 |
}% |
| 98 |
}{% |
| 99 |
}% |
| 100 |
}% |
| 101 |
|
| 102 |
% FIXME: See also https://github.com/latex3/latex2e/issues/441 |
| 103 |
% for compatibility with the new LaTeX hook management redefine \@currname to |
| 104 |
% force a separate scope |
| 105 |
\let\tikzexternal@currname\@currname |
| 106 |
\def\@currname{tikzlibraryexternal} |
| 107 |
|
| 108 |
\AtBeginDocument{% |
| 109 |
\pgfutil@ifundefined{tikzexternalrealjob}{% |
| 110 |
\global\let\tikzexternalrealjob=\jobname |
| 111 |
}{}% |
| 112 |
\tikzexternal@handle@landscape |
| 113 |
}% |
| 114 |
|
| 115 |
\let\@currname\tikzexternal@currname |