4

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

enter image description here

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

enter image description here

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... .

4
  • 1
    You can look in to the pic facility of tikz, I hope.
    – user11232
    Jun 15, 2014 at 13:25
  • Thanks! In case anyone else is reading this: texample.net/tikz/examples/tree-pic
    – Mohan
    Jun 15, 2014 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, 2014 at 13:46
  • 1
    tex.stackexchange.com/questions/837/… It might be possible to reuse the image as a background image as suggested here?
    – srao
    Jun 15, 2014 at 14:13

1 Answer 1

2

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}

aligned border

3
  • "savebox is larger than the text area inside": due to outer sep?
    – Mohan
    Jun 15, 2014 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. Jun 15, 2014 at 20:59
  • Aha! I needed a % after { to prevent adding an extra space. Jun 15, 2014 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.