[Tex/LaTex] Combine Tikz pictures

tikz-pgf

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)

Best Answer

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}
}
Related Question