2

I'm drawing a block diagram with tikz, in which I have a node containing sub-nodes by means of nested tikzpictures.

Here is a MWE:

\documentclass[]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{calc} 

\begin{document}

\tikzstyle{virtual}=[coordinate]

\tikzstyle{block}=[draw, inner sep=3pt]

\begin{tikzpicture}[node distance=20pt]
    % nodes

    \node[virtual] (in) at (0mm,0mm) {};
    \node[block] (g) [right = of in] {G};

    \node[draw, anchor = east, inner xsep = 0, inner ysep = 5pt] (group) at ([xshift=100pt, yshift=50pt]g.west) {
    \begin{tikzpicture}
        \node[virtual] (in) at (0,0) {};
        \node[block] (a) [right =  of in] {$A$};
        \node[block] (b) [above =  of a] {$B$};
        \node[draw,
              circle,
              anchor = center,
              inner sep = 1pt,
              xshift = 20pt,
              minimum size=3pt,
             ] (sum) at ($(a.east)!0.5!(b.east)$) {};
        \draw [->] (in.east) -- (a.west);
        \draw [->] (in.east|-b) -- (b.west);
        \draw [->] (a.east) -| (sum.south);
        \draw [->] (b.east) -| (sum.north);
        \draw [-] (sum.east) -- ([shift={(10pt,0)}]sum.east);
        \end{tikzpicture}
    };


    %edges
    \draw[->] (g.east) -- ([shift={(100pt,0)}]g.east);
    \draw[->] ([shift={(10pt,0)}]g.east) |- (group.west); % <- I would like to connect to the in of block A

\end{tikzpicture}
\end{document}

whose output is

MWE output

As I sketched with a red arrow in the picture, I would like to connect the signal coming up from the output of the node g to the node group in the position corresponding to the children-node a.

Is it possible to achieve this without the need of hard coding an yshift?

mrk_brn
  • 291

1 Answers1

1

This is an attempt to politely convince you that one should never nest tikzpictures. This is not supported, and can lead to uncontrollable errors. And you do not need it. Just make the contents of the inner tikzpicture a pic, and use its coordinates as explained on p. 261 of the pgfmanual (version 3.1).

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{positioning}
\usetikzlibrary{calc} 

\begin{document}

\tikzset{virtual/.style={coordinate},
block/.style={draw, inner sep=3pt}}

\begin{tikzpicture}[node distance=20pt,
my subpicture/.pic={\begin{scope}[local bounding box=#1]
        \node[virtual] (in) at (0,0) {};
        \node[block] (a) [right =  of in] {$A$};
        \node[block] (b) [above =  of a] {$B$};
        \node[draw,
              circle,
              anchor = center,
              inner sep = 1pt,
              xshift = 20pt,
              minimum size=3pt,
             ] (sum) at ($(a.east)!0.5!(b.east)$) {};
        \draw [->] (in.east) -- (a.west);
        \draw [->] (in.east|-b) -- (b.west);
        \draw [->] (a.east) -| (sum.south);
        \draw [->] (b.east) -| (sum.north);
        \draw [-] (sum.east) -- ([shift={(10pt,0)}]sum.east);
    \end{scope}}]
    % nodes

    \node[virtual] (in) at (0mm,0mm) {};
    \node[block] (g) [right = of in] {G};

    \pic at ([xshift=33pt, yshift=50pt]g.west) (mypic) {my subpicture=group};
    \draw ([yshift=-5pt]group.south west) rectangle ([yshift=5pt]group.north east);


    %edges
    \draw[->] (g.east) -- ([shift={(100pt,0)}]g.east);
    \draw[->] ([shift={(10pt,0)}]g.east) |- (mypicin); % <- I would like to connect to the in of block A

\end{tikzpicture}
\end{document}

enter image description here

  • Ok, got it. Just a pity that relatively positioning pictures is a pain. Consider having many pictures that needs to be aligned one with respect to the other in order to align "inputs" and "outputs", the only solution I found is to manually compute shifts, since the pic is positioned with respect its origin. – mrk_brn Feb 06 '19 at 08:23
  • @mrk_brn The problem you are describing is usually solved by declaring a shape and playing with its anchors. For instance, circuitikz has tons of shapes which have such in an out anchors. –  Feb 06 '19 at 15:18