[Tex/LaTex] Draw on background layer with mutiple tikzpictures, remember picture and overlay

backgroundsoverlaystikz-pgf

I am constructing an image which consists of multiple tikzpictures (because some parts of them otherwise would interfere with other parts). I would like to place a background image behind all of them consisting of multiple nodes connecting the nodes on the main layer.
With remember picture I already can adress nodes defined in the other tikzpicture. With overlay I managed to place the tikzpicture with the background layer on top of the other tikzpicture(s).
I suspect the problem hast to do with using 'overlay'. Is there something like 'underlay'?

This is a (extremely simplified) MWE to show what I mean:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, shapes}
\usepackage{xcolor}

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\begin{document}

\begin{tikzpicture}[remember picture]
    \node[circle, fill=blue] (circle) {Main};
    \node[circle, fill=blue, right=of circle] (circle2) {Main2};
    \node[circle, fill=blue, below right=of circle] (circle3) {Main3};
\end{tikzpicture}

\begin{tikzpicture}[remember picture, overlay]
    \begin{pgfonlayer}{background}
        \node[circle, fill=green, minimum height=1cm, below right=-5pt and -5pt of circle] (circle-bg) {BG};
    \end{pgfonlayer}

    \node[circle, fill=yellow, minimum height=1cm, below right=-5pt and -5pt of circle-bg] (circle-main) {overlay};

\end{tikzpicture}

\end{document}

I would like the green circle also be behind the blue circle.

Overlay of a tikzpicture with background layer on to of other

Best Answer

It's not recommended to nest tikzpictures. As OP doesn't give too much details about why he/she wants to nest tikzpictures, I'll try to solve the example problem.

The green node is not behind the blue ones because even being placed on background layer, it's placed on the background layer of a second tikzpicture which is drawn after the first one.

As OP already provides three layers in its code: background, main and foreground, these three layers can be used to distributed all nodes on their corresponding layer.

Three blue nodes from first tikzpicture can be drawn on main layer, the green node on background and the yellow on foreground. This way we can control which part of the figure is behind/above each one, and we use only one tikzpicture.

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, shapes}
%\usepackage{xcolor} %already loaded by tikz

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\begin{document}

\begin{tikzpicture}[remember picture]
    \begin{pgfonlayer}{main}
    \node[circle, fill=blue] (circle) {Main};
    \node[circle, fill=blue, right=of circle] (circle2) {Main2};
    \node[circle, fill=blue, below right=of circle] (circle3) {Main3};
     \end{pgfonlayer}

    \begin{pgfonlayer}{background}
        \node[circle, fill=green, minimum height=1cm, below right=-5pt and -5pt of circle] (circle-bg) {BG};
     \end{pgfonlayer}

     \begin{pgfonlayer}{foreground}
    \node[circle, fill=yellow, minimum height=1cm, below right=-5pt and -5pt of circle-bg] (circle-main) {overlay};
    \end{pgfonlayer}

    \begin{pgfonlayer}{background}
       \draw (circle) to[out=240, in=180] (circle-main);
       \draw (circle-bg)--(circle2);
     \end{pgfonlayer}

\end{tikzpicture}

\end{document}

enter image description here