4

I created following example script to draw a TikZ that contains a nested tikz:

Screenshot

\documentclass[tikz,border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, shapes.multipart, backgrounds, fit}

\tikzset{
  box/.style={
    draw,
    rectangle,
    minimum height=3cm,
    fill=white,
    align=center,
    inner sep=2ex
  }
}

\begin{document}

\begin{tikzpicture}[]

    % Box 1   
    \node[box, label={Label 1}] (Box1) {\rotatebox{90}{Content 1}};

    % Box 2 - Inner
    \node[right=of Box1] (Box2) {
        \begin{tikzpicture}
            \node[box] (Inner) {\rotatebox{90}{\ttfamily{Something goes in here}}};
        \end{tikzpicture}
    };

    % Box 2 - Outer
    \begin{scope}[on background layer]
        \node[box, inner sep=0ex, fit=(Box2), label={Label 2}] {};
    \end{scope}

    % Box 3    
    \node[box, right=of Box2, label={Label 3}] (Box3) {\rotatebox{90}{Content 2}};

    \draw[dashed] (Box1) -- (Box2);
    \draw[dashed] (Box2) -- (Box3);

\end{tikzpicture}

\end{document}

Assume that the true content of Box2, where we currently have placeholder "Something goes in here" is a more complex TikZ drawing with many nodes.

Is it necessary to have the \begin{tikzpicture} within Box2, or is there a better way? I tried without, but it produced errors.

Thanks!

1 Answers1

8

Don't use nested tikzpictures

  1. Start drawing the complex figure.
  2. Put a fit node aroung it on background layer.
  3. Draw Box1 to the left.
  4. Draw Box2 to the right.

Reordered code:

\documentclass[tikz,border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, shapes.multipart, backgrounds, fit}

\tikzset{
  box/.style={
    draw,
    rectangle,
    minimum height=3cm,
    fill=white,
    align=center,
    inner sep=2ex
  }
}

\begin{document}

\begin{tikzpicture}[]


    % Box 2 - Inner
            \node[box, font=\ttfamily, text width=2cm] (a) {Something goes in here};
            \node[box, circle, below right=2mm and 5mm of a, font=\ttfamily, text width=2cm]  (b) {Something goes in here};

%    % Box 2 - Outer
    \begin{scope}[on background layer]
        \node[box, inner sep=1ex, fit=(a) (b), label={Label 2}] (Box2) {};
    \end{scope}

    % Box 1   
    \node[box, label={Label 1}, left =of Box2 ] (Box1) {\rotatebox{90}{Content 1}};


    % Box 3    
    \node[box, right=of Box2, label={Label 3}] (Box3) {\rotatebox{90}{Content 2}};

    \draw[dashed] (Box1) -- (Box2);
    \draw[dashed] (Box2) -- (Box3);

\end{tikzpicture}

\end{document}

enter image description here

Ignasi
  • 136,588