I am trying to include PGF figures from files (in my case created by matplotlib) and combine them with the TikZ-externalizing workflow.
The benefit of including PGF figures in the first place is that text such as labels, legends, etc are rendered using the font settings of the including .tex file. The benefit of using tikzexternalize
is that during tex-compilation PDFs of the tikz figures are created so that (a) the time consuming "building" of the tikz figures need to be done only once and (b) the, e.g., publisher only needs to ever see and use the PDF versions of the tikz figures (they typically don't have a full tikz version installed).
I understand that PGF is a somewhat lower-level framework upon which tikz is built and which might cause some problems. Nonetheless, the pgf-manual seems to hint that the above described process should, in principle, be possible. As this questions has been asked before (Externalize pgfpicture) but was closed without a solution, I'd like to post a MWE and initiate a discussion :smile:
\documentclass{article}
\usepackage{filecontents}
\usepackage{tikz}
\begin{filecontents*}{img.pgf}
\begingroup%
\makeatletter%
\begin{pgfpicture}%
\pgfpathrectangle{\pgfpointorigin}{\pgfqpoint{3cm}{2cm}}%
\pgfusepath{use as bounding box, clip}%
\begin{pgfscope}%
\pgfpathmoveto{\pgfpointorigin}%
\pgfpathlineto{\pgfpoint{2cm}{2cm}}%
\pgfusepath{stroke}%
\pgftext[x=1cm,y=1cm]{\color{red}\large\selectfont 123456789}%
\end{pgfscope}%
\end{pgfpicture}%
\makeatother%
\endgroup%
\end{filecontents*}
\usetikzlibrary{external}
\tikzexternalize[mode=list and make]
\begin{document}%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
The first version is based on a simple tikz text and drawing and works OK:
\begin{figure}[h]
\centering
\begin{tikzpicture}
\draw[thick,rounded corners=8pt] (0,0) -- (2,2);
\node at (1,1){\color{red}\large\selectfont 123456789};
\end{tikzpicture}
\caption{... using tikz}
\end{figure}
\vspace{4em}
The second version uses the pgf file that was written using \texttt{filecontents}:
\begin{figure}[h]
\centering
% uncomment the tikzpicture environment for using "externalizing"
%\begin{tikzpicture}
\input{img.pgf}
%\end{tikzpicture}
\caption{... using a PGF image}
\end{figure}
\end{document}%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Copy the code into a file mwe.tex
and then compile it with
pdflatex -shell-escape mwe.tex
make -f mwe.makefile
pdflatex -shell-escape mwe.tex
.
This has created the file one.pdf that for all further compilation runs
will be directly included instead of compiling the tikzpicture. The PDFs mwe.pdf
also shows that the included PGF figure looks ok. However, when you un-comment the tikzpicture
environment for the second figure and error occurs during make -f mwe.makefile
:
(./img.pgf
! TeX capacity exceeded, sorry [input stack size=5000].
\pgf@selectfontorig ->\pgf@selectfontorig
\nullfont
l.10 ... \pgftext[x=1cm,y=1cm]{\color{red}\large
\selectfont 123456789}%
Now, the question would be, how to fix this problem -- or how to adjust the whole workflow without manually changing/replacing code or files? Thanks!