# Combine tikz external with pgf layers to “split up” a huge tikzpicture?

I have a huge tikz picture (consisting of up to several 10k elements, mostly rectangles and arrows) and want to "split up" the file using externalization to avoid expanding tex's main memory size (or use luatex).

Since I work with pgf layers anyway, my idea was to externalize each layer. Here is a minimal example of what I did so far:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{fp}
\usetikzlibrary{arrows}
\usetikzlibrary{external}
\tikzexternalize[prefix=tikz/]

\pgfdeclarelayer{background}
\pgfdeclarelayer{layer1}
\pgfdeclarelayer{layer2}
\pgfsetlayers{background,layer1,layer2,main}

\def\picWidth{416}
\def\picHeight{240}
\FPeval\relation{\picHeight/\picWidth}

\tikzstyle{blkLine}=[thin]
\tikzstyle{vecLine}=[thick,>=stealth,->]

\begin{document}

\begin{pgfonlayer}{background}
\begin{tikzpicture}
\node[anchor=south west,inner sep=0] (image) at (0,0) {\includegraphics[width=\textwidth]{../TikZ_test/BasketballPass_416x240_50_frm16.jpg}};
\end{tikzpicture}
\end{pgfonlayer}

\begin{pgfonlayer}{layer1}
\begin{tikzpicture}
\node[anchor=south west,inner sep=0,fill=white,opacity=0,minimum width=\textwidth,minimum height=\relation\textwidth] (image) at (0,0) {};
\begin{scope}[blkLine,fill opacity=0.2,x={(image.south east)},y={(image.north west)},shift={(image.north west)},yscale=-1]
\filldraw[fill=red!20!yellow] (0,0) rectangle (0.5,0.5);
\filldraw[fill=red!40!yellow] (0,0.5) rectangle (0.5,1);
\filldraw[fill=red!60!yellow] (0.5,0) rectangle (1,1);
\end{scope}
\end{tikzpicture}
\end{pgfonlayer}

\begin{pgfonlayer}{layer2}
\begin{tikzpicture}
\node[anchor=south west,inner sep=0,fill=white,opacity=0,minimum width=\textwidth,minimum height=\relation\textwidth] (image) at (0,0) {};
\begin{scope}[vecLine,x={(image.south east)},y={(image.north west)},shift={(image.north west)},yscale=-1]
\filldraw[color=black] (0.25,0.25) -- (0.75,0.75);
\end{scope}
\end{tikzpicture}
\end{pgfonlayer}

\end{document}


For the different layers (or rather tikzpictures), the pdf files are produced flawlessly, but the "combined" pdf file is empty. Since hierarchical tikz pictures don't work with externalization afaik, is there another way correctly combine the pictures?

-
is this the same question as tex.stackexchange.com/questions/128006/… ? –  David Carlisle Aug 14 '13 at 10:18
it's about same problem, but since I made a lot of progress the question became more specific, so I thought that starting a new question would be okay –  Bastian35022 Aug 14 '13 at 10:28
new question is OK but you might want to delete or at least modify the first one, so people don't work on it without noticing this one. –  David Carlisle Aug 14 '13 at 10:40

I'm pretty sure layers won't save you anything. I'm not sure my idea (overlays) is any better. TeX keeps everything in memory until the end of the page, but CiruiTikz keeps all sorts of other things in memory as well. If CircuiTikz reduces everything to something more compact (PGF?) when it closes, you might be in luck.

Anyway, the idea is to break the circuit into pieces, then overlay the pieces.

\documentclass{article}
\usepackage{circuitikz}

% Syntax: \overlay(x,y)overlay
% x and y are registration corrections (need units)
% overlay is terminated by a space or end of line, not braces.

\def\overlay(#1,#2)#3
{\noindent\makebox[\textwidth][l]{\hspace*{#1}\raisebox{#2}[0pt][0pt]{#3}
}\hspace*{-\textwidth}}

% \centerbox{text/picture}

\newcommand{\centerbox}[1]{\makebox[\textwidth][c]{#1}}

\begin{document}

Put a border around your overlays to aid registration.
You can always change the color to white.

\begin{tabular}{cc}
\begin{circuitikz}
\draw[color=blue] (-2,-1) -- (-2,3) -- (3,3) -- (3,-1) -- cycle;
\draw (0,0) to[short, *-*] (2,0) (0,2) to[short, *-*] (2,2);
\end{circuitikz}
&
\begin{circuitikz}
\draw[color=blue] (-2,-1) -- (-2,3) -- (3,3) -- (3,-1) -- cycle;
\draw (0,0) to[C=$C_1$] (0,2) (2,0) to[R=$R_1$] (2,2);
\end{circuitikz}
\end{tabular}

\overlay(0pt,0pt)\centerbox{
\begin{circuitikz}
\draw[color=blue] (-2,-1) -- (-2,3) -- (3,3) -- (3,-1) -- cycle;
\draw (0,0) to[short, *-*] (2,0) (0,2) to[short, *-*] (2,2);
\end{circuitikz}}
\centerbox{
\begin{circuitikz}
\draw[color=blue] (-2,-1) -- (-2,3) -- (3,3) -- (3,-1) -- cycle;
\draw (0,0) to[C=$C_1$] (0,2) (2,0) to[R=$R_1$] (2,2);
\end{circuitikz}}

You can also do it like this:

\overlay(1.05cm,-.06cm)\mbox{
\begin{circuitikz}
\draw (0,0) to[short, *-*] (2,0) (0,2) to[short, *-*] (2,2);
\end{circuitikz}}
\mbox{
\begin{circuitikz}
\draw (0,0) to[C=$C_1$] (0,2) (2,0) to[R=$R_1$] (2,2);
\end{circuitikz}}

\end{document}

-