Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I often nest a tikzpicture within a tikzpicture element, such as a node, to treat subparts as unity and then place it as such relative to other parts of the picture. However, I'm wondering whether it is considered good or bad practice to nest tikzpictures. I'm also interested of knowing the disadvantages, if any, of nesting tikzpictures.

For example, in my latest figure, I've used nesting to achieve the following:

enter image description here

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning}

\colorlet{shade1}{black! 10}
\colorlet{shade2}{black! 20}
\colorlet{shade3}{black! 35}

\begin{document}
\begin{tikzpicture}[%
    function/.style={%
      draw,
      circle,
      minimum size=0pt,
      inner sep=2pt,
      outer sep=0pt,
      node distance=0pt,
    },
    function argument/.style={%
      function,
      fill=shade3,
    },
    gpgpu kernel/.style={%
      function,
      fill=shade2,
    },
    invoker/.style={%
      function,
      fill=shade1,
    },
    label/.style={%
      rectangle,
      inner sep=0pt,
      outer sep=0pt,
      node distance=6pt,
    },
  ]

  % Functions
  \node [invoker, inner sep=-2pt] {%
    \begin{tikzpicture}
      \node [gpgpu kernel] (gpgpu_kernel) {%
        \begin{tikzpicture}
          \node [function argument] (func_arg) {%
            \begin{tabular}{@{}c@{}}
              C \\
              function
            \end{tabular}
          };
          \node [label, below=of func_arg] {GPGPU kernel};
        \end{tikzpicture}
      };
      \node [label, below=of gpgpu_kernel] {Invoker};
    \end{tikzpicture}
  };
\end{tikzpicture}
\end{document}
share|improve this question
    
2  
One disadvantage is that most settings on the outer picture and also enabled for the inner pictures. So doesn't allow you to copy and past any picture into a node and still have the same output. Some settings might be difficult to reset to the default value. A method to avoid this is to store the inner pictures into saved boxes before and only use these boxes inside the other picture. –  Martin Scharrer Apr 8 '12 at 16:44
    
Also related: tex.stackexchange.com/questions/47359/…, and the question linked from there. –  krlmlr Apr 8 '12 at 18:19
    
Here's one of my favourites on this: tex.stackexchange.com/q/46598/86 Basically, Bad practice, but as with all such it can be done if done with extreme care. I would recommend avoiding it if at all possible, and certainly just for positioning then it is always possible to do within a single picture. –  Loop Space Apr 9 '12 at 8:16

1 Answer 1

up vote 9 down vote accepted

I think node nesting is not required (and not recommended for reasons in the linked questions) except for really complicated cases where you can't replicate the contents of a node in a straightforward fashion. However, in this specific case it is certainly doable with even less labor. You can even consider using the fit library for more complicated nesting etc. I chose to use opacity settings and the shade builds up automatically as we lay one on top of the other.

\documentclass{article} 
\usepackage{tikz} 
\usetikzlibrary{positioning}
\tikzset{somestyle/.style={draw,circle,fill,fill opacity=0.1}}
\begin{document}
\begin{tikzpicture}
\node[somestyle,
        inner sep=1.7cm,
        label={[below = -0.9 cm,name=invtext]south:Invoker}] (invcirc) at (6,0) {};
\node[somestyle,
        inner sep=1.2cm,
        label={[below = -1cm,name=gptext]south:GPGU kernel},
        above= 0.1cm of invtext,
        anchor=south] (gpcirc) {};
\node[somestyle,
        above= 0.1cm of gptext,
        anchor=south,
        align=center,
        fill opacity=0.15, % For that extra 5 percent in shade3
        text opacity=1 % To recover the text color
     ] (Cfun) {C\\function};
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.