# TikZ Graph with Graphdrawing and Layered Layout: Nested Nodes do not work at all

I am using TikZ with the libraries graphs and graphdrawing together with the grid layout to generate graphs where the nodes are placed automatically by libraries. Everything, except one thing, works pretty well. Because the standard node types (rectangle, diamond, ellipse...) do not match my needs in one special but frequently case, I need to create a custom Node - rather in the meening of a Graph-Node than a TikZ-Node.

So I tried to put a tikzpicture inside a node and create the needed shape. Outside the graph this works just as expected, but insided not at all.

The reason seems to be the inherited options from the tikzpicture/scope. To solve this, I tried to find a way by

• trying to prevend the inheritance. But did not find a way to do this.
• going back to the "default" layout inside the new tikzpicture inside the node. But did not find the name of the default layout to overwrite the inherited layout.
• using an own pgf-layer for that node.
• Matrices inside the Node (of which every single element is recognized as a node and therefore treated as a graph-node...)
• things I don't remember anymore...

None of the above things works. So my questions is: How can I let embedded nodes appear as one node for the TikZ Graphs Library? Or, as an alternative, is there any other way so solve this (creating an own node-shape with variable content)?

\RequirePackage{luatex85}
\documentclass[tikz,border=1pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes}
\usetikzlibrary{arrows,arrows.meta}
\usetikzlibrary{graphs,graphdrawing}
\usetikzlibrary{fit}
\usetikzlibrary{positioning}
\usegdlibrary{layered}

\newcommand{\ToNode}{edge[->, looseness=0.5, out=east, in=west]}

\tikzset{NodeStyle1/.style   = {draw, anchor=east, rectangle, fill=black!10,  text height=2ex, text depth=0.25ex, minimum height=3ex}}
\tikzset{NodeStyle3a/.style  = {      anchor=east, rectangle,                 text height=2ex, text depth=0.25ex, minimum height=3ex}}
\tikzset{NodeStyle3b/.style  = {draw, anchor=east, rectangle, fill=yellow!20,                                                         minimum width=5em, text centered, inner sep=0, rounded corners}}
\tikzset{NodeStyleRaw/.style = { }}

\tikzset{PicureOptions/.style = {%
layered layout,
level sep=1.5cm,
level distance=0.0cm,
sibling sep=1.5em,
sibling distance=1.0cm,
grow'=right,
components go down left aligned
}}

\pgfdeclarelayer{sub1}
\pgfdeclarelayer{sub2}
\pgfsetlayers{sub2,sub1,main}

\newcommand{\NodeInputNumName}[2]{%
\begin{tikzpicture}
\begin{pgfonlayer}{main}
\node[NodeStyle3a]                          (number) {No. #1};
\node[NodeStyle3a, right = 0mm of number]   (name)   {#2};
\draw (number.north east) -- (number.south east);
\end{pgfonlayer}
\begin{pgfonlayer}{sub1}
\node[NodeStyle3b]   (frame) [fit = (number) (name)] {};
\end{pgfonlayer}
\end{tikzpicture}
}

\begin{document}
\begin{tikzpicture}[]
\begin{scope}[PicureOptions]
\node[NodeStyle1]     (A)   {Graph-Node A};
\node[NodeStyle1]     (B)   {Graph-Node B};
\node[NodeStyle1]     (C)   {Graph-Node C};
\node[NodeStyleRaw]   (D)   {
\begin{tikzpicture}
\begin{pgfonlayer}{main}
\node[NodeStyle3a]                          (number) {No. X};
\node[NodeStyle3a, right = 0mm of number]   (name)   {Graph-Node D, inside Graph};
\draw (number.north east) -- (number.south east);
\end{pgfonlayer}
\begin{pgfonlayer}{sub1}
\node[NodeStyle3b]   (frame) [fit = (number) (name)] {};
\end{pgfonlayer}
\end{tikzpicture}
};
\node         (ABCD) {Result ABCD};

\draw (A) \ToNode  (ABCD);
\draw (B) \ToNode  (ABCD);
\draw (C) \ToNode  (ABCD);
\draw (D) \ToNode  (ABCD);
\end{scope}

%%%%%%%

\begin{scope}[]
\node[NodeStyleRaw] at (0cm, -5cm) () {
\begin{tikzpicture}
\begin{pgfonlayer}{main}
\node[NodeStyle3a]                          (number) {No. x};
\node[NodeStyle3a, right = 0mm of number]   (name)   {Graph-Node, outside graph};
\draw (number.north east) -- (number.south east);
\end{pgfonlayer}
\begin{pgfonlayer}{sub1}
\node[NodeStyle3b]   (frame) [fit = (number) (name)] {};
\end{pgfonlayer}
\end{tikzpicture}
};
\end{scope}

%%%%%%%%%%

\begin{scope}[]
\node[NodeStyleRaw] at (0cm, -10cm) () {\NodeInputNumName{5}{Graph-Node as Command, outside Graph}};
\end{scope}

\end{tikzpicture}
\end{document}


• Welcome to TeX-SE! I didn't look at the code in detail but see that there are all sorts of things that one shouldn't do, perhaps most importantly you are nesting tikzpictures which one shouldn't do. This explains many of the things that do not work. Please get rid of this and then also replace \tikzstyle by the corresponding tikzset syntax. – user121799 Jun 11 at 12:51
• tikzstyle is now replaced by tikzset. I know that nesting tikzpicture is considered bad. It was just an attempt to create an independent area inside a graph. Because the nodes are auto-placed it is not possible placing them manually at specific coordinates, nor would it make sense, because the positions where they should be will change with every new node. If you can tell me an alternative or a hint to make it better, i will use that. – dimple mind Jun 11 at 14:08
• Is using \tikz...; inside a tikzpicture better? – dimple mind Jun 11 at 14:12
• No \tikz...; and a tikzpicture environment are equally bad, see the answers to tex.stackexchange.com/q/47377/121799. You may "prevent the inheritance" by using \saveboxes. – user121799 Jun 11 at 16:13
• Very thanks for that hint and your link, @marmot. Did not find that yet. Will try the savebox. The other approach of that link, [...]For this sort of partial inheritance, the best approach is to ensure that the inner tikzpicture explicitly sets those values that it doesn't want to inherit and then leaves the rest alone.[...] is just the idea of my question's second point from. – dimple mind Jun 12 at 5:46