Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm using beamer and tikz to create an animation of a photon travelling between snowflakes. I have ten Koch snowflakes (hence the \newcommand in my MWE) typeset with decorate{ decorate{ decorate{ decorate{..., and subsequently I have many slides within a frame. Since each slide takes about 20 seconds to typeset, I have decided to use \savebox and \usebox to save time (I tried externalization first, but I rely heavily on external macros, external references, pointers from one tikzpicture to another, and therefore gave up). However, I don't know how to match the coordinate systems of the tikzpicture inside the savebox and the one containing the node where I use the savebox.

This question appears related, but since my image is off in both coordinates, not only vertically, it appears my issue is a different one.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,spy,fit,positioning,backgrounds,arrows,shapes,intersections,decorations.fractals,decorations.markings,external}
\newcommand{\snowflake}[2]{
    \path[fill,draw] decorate{ decorate{ decorate{ decorate{ #1 ++(-150:.5*#2/0.866) -- ++(60:#2) -- ++(-60:#2) -- cycle }}}};
}
\newsavebox{\manyflakes}
\savebox{\manyflakes}{
\begin{tikzpicture}[decoration={Koch snowflake,amplitude=1pt,segment
length=1pt,start radius=1pt},draw=blue,fill=white,very thin]
\snowflake{(1.81, 1.42)}{0.89}
\end{tikzpicture}
}
\begin{document}
\begin{tikzpicture}
\node[anchor=south west,inner sep=0] at (0,0) {\usebox{\manyflakes}};
\filldraw[red] (1.81, 1.42) circle [radius=0.1];
\end{tikzpicture}
\end{document}

Output with photon not centred as desired

How do I match the coordinate systems of the savebox with the one of the tikzpicture, so that the circle at 1.81, 1.42 is exactly in the middle of the snowflake?

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

One thing which is wrong is that you have two spaces, one before and one after the tikzpicture, which push the savebox to the right. Note that source file line breaks are turned to spaces (except two in a row which are turned into a \par. You need to add a % after the opening { to remove that space and then one after \end{tikzpicture}.

Then you should also the outer sept to 0pt, which affects the positioning, too.

I also figured that your \showflake macro has a bug. It seems that the path starts at (0,0) and makes a moveto operation to the start position given by #1. This seems to cause the have picture to have a slightly negative X and Y component which pushes the savebox to the upper right. To fix this you can make the \path operation already start at #1. Then you need to add an empty \path which includes (0,0), otherwise TikZ will crop the image to only contain the flake, but not the origin.

The following code gives me a perfect match. I added a second circle for easy comparison.

\documentclass[border=2]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,spy,fit,positioning,backgrounds,arrows,shapes,intersections,decorations.fractals,decorations.markings,external}
\newcommand{\snowflake}[2]{%
    \path (0,0);
    \path[fill,draw] #1 decorate{ decorate{ decorate{ decorate{ #1 ++(-150:.5*#2/0.866) -- ++(60:#2) -- ++(-60:#2) -- cycle }}}};
}
\newsavebox{\manyflakes}
\savebox{\manyflakes}{%
    \begin{tikzpicture}[decoration={Koch snowflake,amplitude=1pt,segment
        length=1pt,start radius=1pt},draw=blue,fill=white,very thin]
        \snowflake{(1, 2)}{0.89}
        \filldraw[green] (1, 2) circle [radius=0.11];% For comparison
    \end{tikzpicture}%
}

\begin{document}
\begin{tikzpicture}
\node[anchor=south west,inner sep=0pt,outer sep=0pt] at (0,0) {\usebox{\manyflakes}};
\filldraw[red] (1, 2) circle [radius=0.1];
\end{tikzpicture}
\end{document}
share|improve this answer
    
I'd like to accept both answers! –  gerrit Dec 10 '11 at 17:40
1  
@gerrit: Please have a look at my updated answer. There was an hidden issue I fixed now. –  Martin Scharrer Dec 10 '11 at 17:47
    
Thanks! That was not even part of question :) –  gerrit Dec 10 '11 at 20:39
add comment

Remove the anchor=south west option and use the overlay and remember picture options; there were also a pair of spurious blank spaces which I removed in my example code (one after \savebox{\manyflakes}{ and the other one at the end after \end{tikzpicture}):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,spy,fit,positioning,backgrounds,arrows,shapes,intersections,decorations.fractals,decorations.markings,external}

\newcommand{\snowflake}[2]{
    \path[fill,draw] decorate{ decorate{ decorate{ decorate{ #1 ++(-150:.5*#2/0.866) -- ++(60:#2) -- ++(-60:#2) -- cycle }}}};
}
\newsavebox{\manyflakes}
\savebox{\manyflakes}{%
\begin{tikzpicture}[overlay,remember picture,decoration={Koch snowflake,amplitude=1pt,segment
length=1pt,start radius=1pt},draw=blue,fill=white,very thin]
\snowflake{(1.81, 1.42)}{0.89}
\end{tikzpicture}%
}
\begin{document}

\begin{tikzpicture}
  \node[inner sep=0] at (0,0) {\usebox{\manyflakes}};
  \filldraw[red] (1.81, 1.42) circle [radius=0.1];
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer
    
I'd like to accept both answers! I finally chose Martins because I find it a bit cleaner and it explains why my current one goes wrong. –  gerrit Dec 10 '11 at 17:41
    
@gerrit Yes, it's better to accept his answer. I'll delete mine. –  Gonzalo Medina Dec 10 '11 at 17:44
    
@GonzaloMedina: No need to delete yours (I took the liberty to undelete it). Using overlay here is a good idea, because it avoids issues with the bounding box (because there is none then). Using remember picture however, might not work with saveboxes. Anyway, AFAIK it is only needed if you want to address included nodes by name. (But I'm aware that in them majority of cases this both options are used together.) –  Martin Scharrer Dec 10 '11 at 18:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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