[Tex/LaTex] node, scope and pgfonlayer

tikz-pgf

Please can I use node inside pgfonlayer? The node in question is commented out in the following:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{scopes}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}
[
  my line/.style={line width=2.5pt},
  put circle/.style={circle,draw=blue!50,fill=blue!20,thick,
    inner sep=0pt,minimum size=5mm},
  execute at end picture={
    \begin{pgfonlayer}{background}
      \path[fill=yellow!25,rounded corners,my line,draw=red]
        (current bounding box.south west) rectangle
        (current bounding box.north east);
      %\node at (2,2) [my line] {
      {
        [magenta]
        \draw (0mm,10mm) -- (15mm,10mm);
        \draw (0mm,8mm) -- (15mm,8mm);
      }
        \draw (0mm,6mm) -- (15mm,6mm);
      {
        [green]
        \draw (0mm,4mm) -- (15mm,4mm);
        \draw (0mm,2mm) -- (15mm,2mm);
        \draw[blue] (0mm,0mm) -- (15mm,0mm);
      };
    %};
    \end{pgfonlayer}
  }
]
\node at (0,0) [put circle] {X};
\node at (4,1) [put circle] {Y};
\end{tikzpicture}
\end{document}

Best Answer

You can do that

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{scopes}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}
[
  my line/.style={line width=2.5pt},
  put circle/.style={circle,draw=blue!50,fill=blue!20,thick,
    inner sep=0pt,minimum size=5mm},
  execute at end picture={
    \begin{pgfonlayer}{background}
      \path[fill=yellow!25,rounded corners,my line,draw=red]
        (current bounding box.south west) rectangle
        (current bounding box.north east);
      % you can draw the next lines with yshift (or with the calc libraty)
      % and (current bounding box.center)
      % without the node !
      \node at (current bounding box.center) {\tikz  [my line]
        {\draw [magenta]
        (0mm,10mm) -- (15mm,10mm)
        (0mm,8mm) -- (15mm,8mm);
        \draw[black] (0mm,6mm) -- (15mm,6mm);
        \draw[green]  (0mm,4mm) -- (15mm,4mm)
                      (0mm,2mm) -- (15mm,2mm);
        \draw[blue] (0mm,0mm) -- (15mm,0mm);}
      }; 
    \end{pgfonlayer}
  }
]
\node at (0,0) [put circle] {X};
\node at (4,1) [put circle] {Y};
\end{tikzpicture}
\end{document} 

enter image description here

It's possible to use a tikz environment inside another tikz environment but you need to proceed carefully and it's better to avoid this when it's possible.

Without intern tikz environment

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,scopes}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}
[
  my line/.style={line width=2.5pt},
  put circle/.style={circle,draw=blue!50,fill=blue!20,thick,
    inner sep=0pt,minimum size=5mm},
  execute at end picture={
    \begin{pgfonlayer}{background}
      \path[fill=yellow!25,rounded corners,my line,draw=red]
        (current bounding box.south west) rectangle
        (current bounding box.north east);
     {[my line]
            \draw [magenta]
            ([shift={(-7.5mm,0mm)}]current bounding box.center) --++ (15mm,0mm);  
     }
    \end{pgfonlayer}
  }
]
\node at (0,0) [put circle] {X};
\node at (4,1) [put circle] {Y};
\end{tikzpicture}
\end{document}

enter image description here