12

I want to use dash rectangle in outer node but use solid rectangle in inner nodes. But my sample code below actually dashed all node for inner or outer nodes.

\documentclass[tikz]{standalone}

\begin{document}

\begin{tikzpicture}[
  inner/.style={draw,fill=blue!5,thick,inner sep=3pt,minimum width=8em},
  outer/.style={draw=gray,dashed,fill=green!1,thick,inner sep=5pt}
  ]
%---------------------------------------------------%
\node[outer] (A) {
    \begin{tikzpicture}[node distance=1cm,outer sep = 0pt]
      \node [inner,minimum width=18em] (A1) {Mr. A};
      \node [inner,anchor=south west,minimum width=8em] (A2) at ([yshift=1em]A1.north west) {Mr. A1};
      \node [inner,anchor=south east,minimum width=8em] (A3) at ([yshift=1em]A1.north east) {Mr. A2};
      \node (text) [anchor=north] at ([yshift=4em]A1.north) {Hello Tikz};
    \end{tikzpicture}
}; 
\end{tikzpicture}

\end{document} 

The output looks like below:

enter image description here

I only use dashed on outer style but actually inner nodes has got such attribute also! I don't want that!

1
  • 2
    Nesting tikz pictures is almost always a bad idea as you can see here. All settings are inherited etc. Use the fit library instead
    – percusse
    Commented Aug 20, 2014 at 20:02

2 Answers 2

13

Using fit and backgrounds for avoiding nested tikzpicture. The Zarko's answer is easier than mine but i wanted to illustrate this option. It can be useful in other contexts.

\documentclass[tikz]{standalone}
\usetikzlibrary{fit,backgrounds} % <- added
\begin{document}

\begin{tikzpicture}[
  inner/.style={draw,fill=blue!5,thick,inner sep=3pt,minimum width=8em},
  outer/.style={draw=gray,dashed,fill=green!1,thick,inner sep=5pt}
  ]
%---------------------------------------------------%
  \node [inner,minimum width=18em] (A1) {Mr. A};
  \node [inner,anchor=south west,minimum width=8em] (A2) at ([yshift=1em]A1.north west) {Mr. A1};
  \node [inner,anchor=south east,minimum width=8em] (A3) at ([yshift=1em]A1.north east) {Mr. A2};
  \node (text) [anchor=north] at ([yshift=4em]A1.north) {Hello Tikz};
\begin{pgfonlayer}{background}
\node[outer,fit=(A1) (A2) (A3) (text)] (A) {};
\end{pgfonlayer}
\end{tikzpicture}

\end{document} 

The result is the same.

2
  • Cool, but if I want to use multiple group such things, I wish to treat it as a group for positioning, how can I group this version as one? such as it maybe better to use "below=1cm of A" of B group.
    – Beatlej
    Commented Aug 20, 2014 at 21:23
  • 1
    your group is in reality a referenced node with label (A), so you can use "below=1cm of A" whenever you load the positioning library.
    – skpblack
    Commented Aug 20, 2014 at 21:45
9

Try:

\documentclass[tikz]{standalone}
\begin{document}
    \begin{tikzpicture}[
inner/.style={draw,
              solid,% <-- added
              fill=blue!5,thick,inner sep=3pt,minimum width=8em},
outer/.style={draw=gray,dashed,fill=green!1,thick,inner sep=5pt}
                        ]
\node[outer] (A) {
    \begin{tikzpicture}[node distance=1cm,outer sep = 0pt]
      \node [inner,minimum width=18em] (A1) {Mr. A};
      \node [inner,anchor=south west,minimum width=8em] (A2) at ([yshift=1em]A1.north west) {Mr. A1};
      \node [inner,anchor=south east,minimum width=8em] (A3) at ([yshift=1em]A1.north east) {Mr. A2};
      \node (text) [anchor=north] at ([yshift=4em]A1.north) {Hello Tikz};
    \end{tikzpicture}
                };
    \end{tikzpicture}
\end{document} 

enter image description here

Edit: Nesting tikzpicture directly in nodes works just by accident. Better approach is suggested in accepted answer. so here is small variation of it. Positions of nodes are determined by use of the positioning library, text "Hello Tikz" in outer node is written as its label:

\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{backgrounds,
                fit,
                positioning}
                
\begin{document}
    \begin{tikzpicture}[
node distance = 2ex and 0em,
inner/.style={draw, thick, fill=blue!5,
              inner sep=3pt,minimum width=8em},
outer/.style={draw=gray, thick, 
              densely dashed, fill=green!5,
              inner xsep=1ex, inner ysep=2ex, yshift=1ex,
              fit=#1}
                        ]
\node (A1)  [inner, minimum width=18em] {Mr. A};
\node (A2)  [inner,above right=of A1.north west] {Mr. A1};
\node (A2)  [inner,above  left=of A1.north east] {Mr. A2};
\scoped[on background layer]
\node [outer=(A1) (A2),
       label={[anchor=north]:Hello Tikz}] {};
    \end{tikzpicture}
\end{document}

enter image description here

2
  • Good suggestion, that means it has been override actually, but maybe some other things need to rewrite also. do we have method to prevent such heritage?
    – Beatlej
    Commented Aug 20, 2014 at 21:26
  • 1
    In nested TikZ pictures the parameters of outside picture always influence on inside picture. Some times you can help yourself with trick as I show you in my answer, but generally the better approach is given in second answer.
    – Zarko
    Commented Aug 20, 2014 at 23:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .