4

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

3

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

  • Thank you for your response. I am aware that nesting tikzpictures is not recommended, but I am using two Sankey Diagrams (using this code). Placing the second diagram into a savebox and using it as in my solution proposed, works for my case. – bellerophons_pegasus May 22 '17 at 10:18
0

A solution that works for me is using \savebox and using this box inside the same tikzpicture where the other elements are drawn. Thus I am avoiding the extra tikzpicture environment with overlay and everything works fine.

Maybe something for others who come across this problem:

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

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

\begin{document}

\newsavebox{\complicatedPicture}
\savebox{\complicatedPicture}{%
    \begin{tikzpicture}
        \node[circle, fill=yellow, minimum height=1cm]{overlay};
    \end{tikzpicture}
}

\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};

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

    \node[below right=-13pt and -13pt of circle-bg] (circle-main){\usebox{\complicatedPicture}};

\end{tikzpicture}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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