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
    You may use a scope and its local bounding box. – Paul Gaborit Jan 11 '18 at 9:02
  • In the script above, I do make use of a scope. That means, the content of node "Box", containing mentioned tikz, is part of a scope. Should I just replace " \begin{tikzpicture}" with " \begin{scope}"? – WolfgangRtry Jan 11 '18 at 9:04
  • It's a bit more complicated than that ... – Paul Gaborit Jan 11 '18 at 9:08
  • That's what I thought, because I tried it. Do you have a solution or link? – WolfgangRtry Jan 11 '18 at 9:23
  • My first comment provides a link that shows the usage of local bounding box... – Paul Gaborit Jan 11 '18 at 9:40
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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