# tikz: Saving repeatedly drawn parts of pictures to increase compilation speed

I'm drawing a number of very similar images using tikz, like this:

Code (and motivation) are given at Making cards for Magic: The Gathering and similar card games.

Compilation is slow. I've timed it, and most of the time is being spent repeatedly drawing the same image, in this case

It seems to me that there should be some way to save this material once it is rendered, to prevent the duplication of work. Is it possible to reuse a part of a tikz image points me in a sensible direction, in that it suggests using a box. My problem is that I have no idea how to get the text to be placed sensibly relative to the material saved in a box... .

-
You can look in to the pic facility of tikz, I hope. –  Harish Kumar Jun 15 at 13:25
Thanks! In case anyone else is reading this: texample.net/tikz/examples/tree-pic –  Mohan Jun 15 at 13:36
@Harish Kumar: From the 3.0.0 manual, it seems as if the material is redrawn every time. Are you sure pic is faster? –  Mohan Jun 15 at 13:46
tex.stackexchange.com/questions/837/… It might be possible to reuse the image as a background image as suggested here? –  srao Jun 15 at 14:13

This assumes that saveboxes are faster than macros. It should be remembered that the savebox is larger than the text area inside.

\documentclass{article}
\usepackage{tikz}

%create border around empty text area 4cm by 2cm

\newsavebox{\myborder}
\savebox{\myborder}{%
\begin{tikzpicture}
\node[draw=red, thick] {\parbox[t][2cm]{4cm}{\strut}};
\end{tikzpicture}}

\begin{document}

% align edges

\begin{tikzpicture}
\path (0,0) node[inner sep=0] (border) {\usebox{\myborder}}
(border.north west) node[below right] {\begin{minipage}{4cm}
With any luck this text will fit nicely into the borders drawn previously.
\end{minipage}};
\end{tikzpicture}

% align centers

\begin{tikzpicture}
\path (0,0) node[inner sep=0] {\usebox{\myborder}}
(0,0) node {\parbox[t][2cm]{4cm}
{With any luck this text will fit nicely into the borders drawn previously.}};
\end{tikzpicture}

\end{document}


-
"savebox is larger than the text area inside": due to outer sep? –  Mohan Jun 15 at 20:20
Actually, the inner sep and line thickness. But it is worse than that. Further tests with registration marks showed that even when both nodes are centered, the second node is 1.5pt left of the first. –  John Kormylo Jun 15 at 20:59
Aha! I needed a % after { to prevent adding an extra space. –  John Kormylo Jun 15 at 21:14