2

I've found a Q/A about how to create group nodes to blocks. It works, but exact opposite way as I needed. It defines internals and then draws blocks around them.

I need to define block with internals, LaTeX pseudocode:

\begin{tikzpicture}
  \myblock(main){Main block name}{
    \mynode(anode)[params]
    \mynode(bnode)[params,right=of anode]
    \myblock(subblock){Subblock name}[below=of anode]{
      \mynode(subnodea)[p...]
      \mynode(subnodeb)[p...]
    }
  }
  \myblock(sideblock){Aux block}[right=of main]{
    ... mynodes ...
  }
\end{tikzpicture}

Is there a way, how to define myblock as "complex node" - position is as one node and it moves all its internal nodes.

I've figured out part of the solution (didn't put commands into \def yet):

\begin{tikzpicture}[
        element/.style={draw=black, fill=white},
        group/.style={draw=black, rounded corners, fill=white}
    ]

    \node(main_a)[element]{MAIN A};
    \node(group_a)[group, below=of main_a]{
        \begin{tikzpicture}
            \node(b)[element]{bb};
            \node(c)[element, left=of b]{ccc - left};
        \end{tikzpicture}
    };
    \node(group_a_aux_r)[element,right=of group_a]{Aux Group Right};
    \node(group_a_aux_l)[element,left=of group_a]{Aux Group Left};
    \node(group_a_aux_b)[element,below=of group_a]{Aux Group Bottom};

    \draw[->,red] (main_a) -- (group_a);
    \draw[->,red] (group_a) -- (group_a_aux_r);
    \draw[->,red] (group_a) -- (group_a_aux_l);
    \draw[->,red] (group_a) -- (group_a_aux_b);

    \node(group_b)[group, right=of group_a_aux_r]{
        \begin{tikzpicture}
            \node(d)[element]{dddd};
            \node(e)[element, left=of d]{eeee - left};
        \end{tikzpicture}
    };

    %code below fails - second image!!
    \node(aux_b)[element,right=of b,draw=blue]{Aux B};
    \node(aux_c)[element,left=of c,draw=blue]{Aux C};
    \draw[->,blue] (b) -- (aux_b);
    \draw[->,blue] (c) -- (aux_c);


    \draw[->,green] (c) -- (d);
\end{tikzpicture}

Here is what I get without inner references: complex node / group, without inner references It works: lays out children, puts it into node and lays it out as node. However, It passes properties of group_a to its children, which is unwanted behaviour!

Here is what I get when inner references are added: complex node / group, with inner references

This is completely broken.

kravemir
  • 360

2 Answers2

1

Nesting TikZ pictures is not a good idea. It may give strange effects, as it happened to you.

You could use a TikZ matrix instead.

However, it is not clear where you want to position Aux B and Aux C because as they are in your MWE, they overlap with the other nodes.

\documentclass{standalone} 
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{positioning}
\begin{document} 
\begin{tikzpicture}[
        element/.style={draw=black},
        group/.style={draw=black, rounded corners}
    ]
    \node(main_a)[element]{MAIN A};
    \matrix[group,
        column sep=3em,
        matrix of nodes,
        nodes={element}, 
        below=of main_a
        ] (group_a) {
            |[name=b]|bb &
            |[name=c]|{ccc - left}\\
            };
    \node(group_a_aux_r)[element,right= of group_a]{Aux Group Right};
    \node(group_a_aux_l)[element,left= of group_a]{Aux Group Left};
    \node(group_a_aux_b)[element,below=of group_a]{Aux Group Bottom};
    \draw[->,red] (main_a) -- (group_a);
    \draw[->,red] (group_a) -- (group_a_aux_r);
    \draw[->,red] (group_a) -- (group_a_aux_l);
    \draw[->,red] (group_a) -- (group_a_aux_b);
    \matrix[group,
        column sep=3em,
        matrix of nodes,
        nodes={element}, 
        right=of group_a_aux_r
        ] (group_b) {
            |[name=d]|dddd &
            |[name=e]|{eeee - left}\\
        };
    \node(aux_b)[above left= of b,draw=blue]{Aux B};
    \node(aux_c)[below right=of c,draw=blue]{Aux C};
    \draw[->,blue] (b) -- (aux_b);
    \draw[->,blue] (c) -- (aux_c);

    \draw[->,green] (c) -- (d);
\end{tikzpicture}
\end{document}

enter image description here

CarLaTeX
  • 62,716
1

with use of TikZ libraries arrows.meta, chains, fit and positioning:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
                chains,
                fit,
                positioning}

\begin{document}
    \begin{tikzpicture}[
  node distance = 8mm and 8mm,
    start chain = going right,
    rbox/.style = {% rounded box
                   draw, rounded corners,
                   minimum height=5mm, inner sep=1mm},
    sbox/.style = {% sharp box
                   draw=#1, inner sep=1mm, text=black},
  sbox/.default = black,
              > = Straight Barb
                        ]
]
\node   [sbox,on chain]  {Aux Group Left};       % A-1
\node   [rbox,on chain]  {ccc - left};
\node   [rbox,on chain]  {bb};
\node   [rbox,on chain]  {Aux Group Right};
\node   [rbox,on chain]  {eeee - left};
\node   [rbox,on chain]  {dddd};                 % A-6
%
\node   (f1)    [rbox, fit=(A-2) (A-3)] {};
\node   (f2)    [rbox, fit=(A-5) (A-6)] {};
%
\node   (n11)   [sbox=blue,above=of A-1]    {Aux C};
\node   (n12)   [sbox, at={(n11 -| f1)}]    {MAIN A};
\node   (n13)   [sbox=blue,above=of A-4]    {Aux BC};
%
\node   (n21)   [sbox,below=of f1]          {Aux Group Bottom};
    \begin{scope}[red,->]
\draw   (f1) -- (A-1);
\draw   (f1) -- (A-4);
\draw   (n12) -- (f1);
\draw   (n21) -- (f1);
    \end{scope}
    \begin{scope}[blue,->]
\draw   (n12) -- (n11);
\draw   (n12) -- (n13);
    \end{scope}
    \end{tikzpicture}
\end{document}

enter image description here

Zarko
  • 296,517