# Nested node class with pictogram decoration

I'm wondering how can I define custom TikZ node class mynode thats allows my to use following syntax:

\node [mynode,fill=yellow!40] {component}
\child{
\node [mynode,fill=yellow!40] {subcomponent}
};


and gives following result:

-
\draw mynode is very difficult to make work. Would \draw node[mynode] be okay? – Loop Space Dec 22 '12 at 17:35
So, @AndrewStacey do you know how to do it, using \draw node[mynode] syntax, because it will be of course enough. – Meter Dec 23 '12 at 10:43
You need to add a little more information. What types of input do you want? How many levels of nesting? – Loop Space Dec 23 '12 at 16:43
@AndrewStacey as you should see there should be of course infinite levels of nesting. What do you mean by saying types of input? I think that answer to it is that i want just simple text input. – Meter Dec 23 '12 at 18:26
Is the "code" in your question what you would like to write in your document? – Loop Space Dec 23 '12 at 18:28

This probably would also be doable with a tree (child) but the first thing that got to my mind was a rectangle split node.

The left node mimics what I tried to achieve and is even not good because the inner node is used to calculate the bounding box (this is the reason I had to set border=8pt for the standalone class).

So to have a node-node with <Outer Name> and <Inner Name> you just have to write the usual:

<path> node[subcomponent=<Inner Name>] {Outer Name} <path>;


or

\node[subcomponent=<Inner Name>] {Outer Name};


The inner node has an extra fill=none added which consequence can be seen if the opacity key is used.

If such little icon in the upper right corner should appear too, you should provide either code or an example for it as it will probably require a little effort on the \tikzlastnode side.

## Code

\usetikzlibrary{shapes.multipart}
\tikzset{
mycomponent/.style={
draw,
fill=yellow!40,
rectangle split,
rectangle split parts=2,
rectangle split allocate boxes=2,
rectangle split empty part height=4ex,
rectangle split empty part width=0ex,
},
subcomponent/.style={
mycomponent,
measure me={#1},
rectangle split every empty part={},% resets empty-part macro
% see http://tex.stackexchange.com/a/88700
rectangle split empty part height={max(4ex,\qrrInnerNodeHeight)},
rectangle split empty part width=\qrrInnerNodeWidth,
append after command={
node[mycomponent,fill=none,anchor=south west] at (\tikzlastnode.two) {#1}
}
},
measure me/.code={%
\global\sbox0{\tikz[baseline=0pt]\node[mycomponent,anchor=south]{#1};}%
\edef\qrrInnerNodeWidth{\the\wd0}%
\edef\qrrInnerNodeHeight{\the\ht0}%
},
}
\begin{document}
\begin{tikzpicture}
\node [mycomponent] {%
Component
\nodepart{two}
\tikz{\node[mycomponent] {Subcomponent};}
};
\node [xshift=3cm,subcomponent=Subcomponent] (t) {Component};
\end{tikzpicture}
\end{document}


## Output

-
+1: This is probably a bit fragile in the long end, but I certainly do like the measure me trick :-) – Daniel Dec 28 '12 at 10:49
But it wont provide syntax that i prsented – Meter Dec 30 '12 at 11:25