如何将节点块定义为从内到外的组合?

如何将节点块定义为从内到外的组合?

我找到了问答关于如何创建块的组节点。它有效,但与我需要的方式完全相反。它定义内部结构,然后在它们周围绘制块。

我需要用内部 LaTeX 伪代码定义块:

\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}

有没有办法,如何定义myblock为“复杂节点” - 位置作为一个节点并且它移动其所有内部节点。

我已经找到部分解决方案(尚未输入命令\def):

\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}

这是我在没有内部引用的情况下得到的结果: 复杂节点/组,无内部引用 它可以工作:布局子节点,将其放入节点并将其布局为节点。但是,它将属性传递group_a给其子节点,这是不受欢迎的行为!

以下是添加内部引用后得到的结果: 复杂节点/组,具有内部引用

这已经完全被打破了。

答案1

嵌套 TiZ 图片不是一个好主意。它可能会产生奇怪的效果,就像你遇到的情况一样。

你可以使用 Timatrix而是Z。

但是,您想要定位在哪里并不明确Aux B,而且Aux C由于它们位于您的 MWE 中,因此它们与其他节点重叠。

\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}

在此处输入图片描述

答案2

使用 TikZ库arrows.metachainsfitpositioning

\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}

在此处输入图片描述

相关内容