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.

Is it possible to combine two tikz pictures that were created using separate environments?

Basically I have a macro create a backdrop that I will use often but I would like to create a layer on top of it which will change each time I use it so

\backdrop
\begin{tikzpicture}

\end {tikzpicture}

but have the tikzpicture use the same coordinate axis, scale, etc and location as the one created in the backdrop

(Backdrop is just another tikz pictures as mentioned in the title)

or can I nest pictures?

\begin{tikzpicture}
\backdrop
\end {tikzpicture}

(I haven't tried this to see if it works)

share|improve this question
    
Is backdrop in pdf or png... format? If yes, then I hope it should be possible. But not as you mentioned above. –  Harish Kumar Mar 25 '12 at 8:16
    
Can you describe \backdrop a little ? Nested tikzpictures are not recommended but it's possible if you take care but it's often possible to avoid this kind of situation ! –  Alain Matthes Mar 25 '12 at 10:13
    
@HarishKumar as the title says "Combine tikz pictures" (this should imply backdrop is another tikzpicture) –  Uiy Mar 25 '12 at 18:23
    
@Altermundus I can avoid it by keeping the tikspicture open but then I cannot modify it easily. It would even be nice to create the picture in one spot, at some other stuff then modify it later on. e.g., give the tikzpicture a name then use a sort of modify environment to reopen it. –  Uiy Mar 25 '12 at 18:25
    
Have you tried it? Given that you know what \backdrop is and we don't, it seems that the simplest thing would be simply to try it and see what happens. Then if something doesn't quite work, you can ask specifically about that and it might be easier for us to help. –  Andrew Stacey Mar 25 '12 at 18:43
show 1 more comment

2 Answers

Somthing like that?

\documentclass[border=1cm]{standalone}

\usepackage{tikz}

\newcommand{\backdrop}{
   \draw (0,0) grid (5,5);
   % more TikZ code
}

\begin{document}
\begin{tikzpicture}
   \backdrop
   \draw [thick, green] (1,1) -- (4,4);
\end{tikzpicture}\quad
\begin{tikzpicture}[xscale=2]
   \backdrop
   \draw [thick, red] (1,1) -- (4,4);
\end{tikzpicture}
\end{document}

result

When using layer it even doesn’t matter where you call \backdrop:

\pgfdeclarelayer{backdrop}
\pgfsetlayers{backdrop,main}

\newcommand{\backdrop}{
      \begin{pgfonlayer}{backdrop}
         \fill [yellow] (2,2) rectangle (3,3);
         \draw (0,0) grid (5,5);
         % more TikZ code
      \end{pgfonlayer}
}
share|improve this answer
    
Yes, but I was hoping to have backdrop in it's own tikzpicture environment. I was going to use your idea but mine is a slightly more complex. The reason is, I might want to have \backdrop as it's own standalone picture in many cases and then combine it with other pictures. I was curious if tikz could easily "merge" picture environments properly. –  Uiy Mar 25 '12 at 18:28
    
@Uiy: Yes I thought that you wanted that, and I found tex.stackexchange.com/q/46596/4918 and tried to add the {tikzpicture} inly if we’re not in one already but I had no success in my short test. You may try it yourself … –  Tobi Mar 25 '12 at 22:26
    
What I would actually like is to "pre-compile" the "backdrop" so it is faster. Sort of convert it to an image then use the image... (automatically of course) –  Uiy Mar 27 '12 at 10:18
add comment

It is very hard to help you because you gave not much information and no minimal working example (MWE) , that at least shows how your backdrop will look like.

Here’s my new suggest.

Create the backdrop image using the standalone class:

% backdrop.tex
\documentclass[border=0cm]{standalone}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
   \draw [blue] (0,0) grid (5,5);
\end{tikzpicture}
\end{document}

Give the image file backdrop.pdf that we now can include in the main document. For that we create a command \backdrop with three optional arguments:

\backdrop(coordiate)[node options][graphic options]

The first on takes a TikZ coordinate like 0,0, the second passes options to the node and the latter passes options to \includegraphics. Since all are optional you must give an empty second argument if you want to use only the third (see the example below). If you call \backdrop in text mode it will certainly ignore the first two arguments. Note: You can only change the node containing the image but not the image itself because it’s a PDF …

\documentclass{scrartcl}

\usepackage{tikz}
\usepackage{graphicx}
\usepackage{xparse}

% define a default style for the backdrop node
\tikzset{
   backdrop node/.style={%
      inner sep=0pt,
      outer sep=0pt,
      anchor=south west,
   },
}

% define the layers
\pgfdeclarelayer{backdrop}
\pgfsetlayers{backdrop,main}

% defin the command to print the backdrop image
\NewDocumentCommand{\backdrop}{D(){0,0} O{} O{}}{%
   \tikzifinpicture{% inside a TikZ-Picture
      \begin{pgfonlayer}{backdrop}
         \node at (#1) [backdrop node,#2]
               {\includegraphics[#3]{backdrop}};
      \end{pgfonlayer}
   }{% in normal Text
      \includegraphics[#3]{backdrop}
   }%
}

\begin{document}
\section*{outside}
\backdrop
\section*{inside}
\begin{tikzpicture}
   \backdrop
   \fill [yellow] (1,1) rectangle (2,2);
   \draw [red, ultra thick] (1,1) -- (4,4);
   \backdrop(6,0)
   \backdrop[rotate=30]
   \backdrop[][width=1cm]
   \backdrop(8.5,2.5)[anchor=center][width=3cm]
\end{tikzpicture}
\end{document}

Thats the result:
result

share|improve this answer
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.