# node, scope and pgfonlayer

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}
-
I don't know if it is intentional but drawing inside the node is, as far as I know not allowed. You can use a path picture but I think I did not understand the final product. Are the lines supposed to be centered inside the node? –  percusse Jun 17 '12 at 4:44
Nodes inside layers should work. But drawing inside a note (without opening a sub-tikzpicture) won't work (IIRC TikZ undefines its commands inside the node text). –  Caramdir Jun 17 '12 at 5:34

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}

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}

-
Thanks. The figure looks like what I want but we see black bars in place of magenta. Nesting, or lack of it, remains one of the current limitations of tikz. –  Ahmed Musa Jun 17 '12 at 6:36
You don't need the scopes and I placed the node at the center of the rectangle (now I know what you want ...) but it's possible to avoid this node. You draw the lines without this node relatively to (current bounding box.center). I keep your method to show that it's possible. –  Alain Matthes Jun 17 '12 at 6:52
Finally I used the scopes inside the second example without the node. –  Alain Matthes Jun 17 '12 at 7:56
Yes, I can see the short-hand scope. –  Ahmed Musa Jun 17 '12 at 20:44