4

I construct a TikZ image with nested nodes:

As you can see, I try to add arrows from the outer node to the inner one. How is it possible to target the arrows to the correct position of the inner node (west and south)?

MWE:

\documentclass[border=1pt, tikz]{standalone}

\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[draw=black, scale=1, transform shape
    , every node/.style = {rectangle, draw=black, align=center, inner xsep=6mm, inner ysep=3mm}
    ]
    % change default arrow style
    \tikzset{very thick, ->, -latex, shorten <=0pt, shorten >=0pt}

    % nodes
    \node (outer) {
        Caption\\[3mm]
        \begin{tikzpicture}[align=center]
            \node (inner) {Inner Text};
        \end{tikzpicture}
    };
    \node [above left=1mm and 10mm of inner.west] (leftabove) {left\\above};
    \node [below left=1mm and 10mm of inner.west] (leftbelow) {left\\below};

    % connections
    \draw (leftabove.east) to (outer.west);
    \draw (leftbelow.east) to (inner.west);
    \draw (leftbelow.east) to (inner.south);
\end{tikzpicture}
\end{document}
  • 4
    You're not nesting nodes, you're nesting tikz pictures (not advisable). – Alenanno Apr 14 '16 at 16:05
4

As Alenanno explained, it's not a good idea to nest tikzpictures. If you want to nest nodes there are several options. One of them is shown in following code. It uses fit library to define a node which contains inner node and its label. (I've declared Caption as inner label, although it could be another non drawn node).

\documentclass[border=1pt, tikz]{standalone}

\usetikzlibrary{positioning, fit}

\begin{document}
\begin{tikzpicture}
    [mynode/.style = {rectangle, draw, align=center, 
            inner xsep=6mm, inner ysep=3mm},
    -latex,     
    ]

    % nodes
    \node[mynode, label={[name=lab]Caption}] (inner) {Inner text};
    \node[fit={(inner) (lab)}, draw] (outer) {};
    \node[mynode, above left=1mm and 10mm of inner.west] (leftabove) {left\\above};
    \node[mynode, below left=1mm and 10mm of inner.west] (leftbelow) {left\\below};

    % connections
    \draw (leftabove.east) to (outer.west);
    \draw (leftbelow.east) to (inner.west);
    \draw (leftbelow.east) to (inner.south);
    \draw (leftabove.east) to (lab);
\end{tikzpicture}
\end{document}

enter image description here

1

like this :

\documentclass[border=1pt, tikz]{standalone}

\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[draw=black, scale=1, transform shape
    , every node/.style = {rectangle, draw=black, align=center, inner xsep=6mm, inner ysep=3mm}
    ]
    % change default arrow style
    \tikzset{very thick, ->, -latex, shorten <=0pt, shorten >=0pt}

    % nodes
    % \node (outer) {
    %     Caption\\[3mm]
    %     \begin{tikzpicture}[align=center]
    %         \node (inner) {Inner Text};
    %     \end{tikzpicture}
    % };
    \node[name=outer, minimum width = 4cm, minimum height= 2cm] {};
    \node[name=caption, below=(2mm of outer.north), draw=white, inner sep=0pt] {Caption};

    \node[name=inner, above=(2mm of outer.south)] {Inner Text};

    \node [above left=1mm and 10mm of inner.west] (leftabove) {\shortstack{left\\above}};
    \node [below left=1mm and 10mm of inner.west] (leftbelow) {\shortstack{left\\below}};

    % connections
    \draw (leftabove.east) to (outer.west);
    \draw (leftbelow.east) to (inner.west);
    \draw (leftbelow.east) to (inner.south);
\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.