Tikz - 如何在矩陣中嵌套節點以便它們可以連接?

Tikz - 如何在矩陣中嵌套節點以便它們可以連接?

我想製作一個在單一矩陣單元中有多個節點的矩陣。這些節點應該彼此居中。我設法透過使用嵌套的 tikz 環境來完成這項工作,如下所示:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{positioning}
\begin{document}

\begin{tikzpicture}[auto, semithick]
    \tikzstyle{block} = [rectangle, draw, 
        minimum width=5em, text centered, rounded corners, minimum
        height=4em] 

    \matrix[matrix of nodes, row sep = 2em,
    nodes={anchor=center}
    ] (mx2){
        % First row:
        label1
        & 
        \node{\tikz{
            \node[block](n1){node1}; 
            \node[block, right=of n1](n2){node2}; 
        }};
        \\
        % Second row:
        label2
        &
        \node{\tikz{
            \node[block] (n3) 
            {node 3};
            \node[block] (n4) [right=of n3] 
            {node 4};
            \node[block] (n5) [right=of n4] 
            {node 5};
        }};
        \\
        };
    \draw (n1) -- (n4); % this fails
\end{tikzpicture}

\end{document}  

在此輸入影像描述

現在由於嵌套的 tikz 環境,連接這些節點是不可能的。我試圖使用該fit庫獲得類似的圖像,但我沒有成功。你們有人能幫我嗎?

先感謝您。

答案1

一般來說,可以使用該remember picture選項並應用它全部圖片,稍後應該可以訪問。由於您的圖片是嵌套的並且選項是可繼承的,因此remember picture僅應用於上層就足夠了{tikzpicture}

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{positioning}
\begin{document}

\begin{tikzpicture}[auto, semithick, remember picture,
    block/.style={rectangle, draw, 
        minimum width=5em, text centered, rounded corners,
        minimum height=4em, text width=5em}
    ]
    \matrix[matrix of nodes, row sep = 2em,
    nodes={anchor=center}
    ] (mx2){
        % First row:
        label1
        & 
        \node{\tikz{
            \node[block] (n1) {node1}; 
            \node[block, right=of n1] (n2) {node2}; 
        }};
        \\
        % Second row:
        label2
        &
        \node{\tikz{
            \node[block] (n3) {node 3};
            \node[block] (n4) [right=of n3] {node 4};
            \node[block] (n5) [right=of n4] {node 5};
        }};
        \\
    };
    \draw (n1) -- (n4); % this works
\end{tikzpicture}

\end{document} 

結果

編譯兩次以獲得正確的結果。

請注意,我替換\tikzstyle{block}block/.style“which”是首選方式。看應該使用 \tikzset 或 \tikzstyle 來定義 TikZ 樣式嗎?

答案2

我認為matrix圖書館在這裡是多餘的(儘管我不斷濫用它)。直接使用也是可能的,並且(在我看來)更方便。無論如何,該positioning庫都會被使用,因此我們可以繼續依賴它。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[
    block/.style={rectangle, draw, 
        minimum width=5em, text centered, rounded corners, minimum
        height=4em,text width=5em}
    ]
\node (l1) at (0,2) {label 1};
\node (l2) at (0,0) {label 2};
\node[right = 2.5cm of l1,block] (n1) {Node 1};
\node[right = of n1,block] (n2) {Node 2};
\node[right = 1cm of l2,block] (n3) {Node 3};
\node[right = of n3,block] (n4) {Node 4};
\node[block,right=of n4] (n5) {node 5};
\draw (n1) -- (n4); % this fails not :)
\end{tikzpicture}
\end{document}

node distance請注意,您可以透過選項追蹤各個位置和預設設置,就像使用矩陣時<direction> = x cm of nodename需要做的那樣row sepcolumn sep

在此輸入影像描述


編輯正如 Altemundus 所評論的那樣,您可以透過以預先定義的座標網格為中心來堆疊行

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}

\begin{tikzpicture}[
    block/.style={rectangle, draw, text centered, rounded corners, minimum height=4em}
    ]
\foreach \x in {0,...,2} \node[circle,inner sep=1mm,fill] (cent\x) at (2,2*\x) {}; 
%The center goes in between
\node[right=of cent0,block] (n1) {A very wide node 1};
\node[left =of cent0,block] (n2) {2};
%The center hits the node
\node[block] at (cent1) (n4) {node 4};
\node[block,left =of n4] (n3) {Also a quite wide node 3};
\node[block,right=of n4] (n5) {5};
\draw (n1) -- (n4); % this fails not :)
%This uses eyeballing after compiling. Choose the widest and adjust.Can be absolute too.
\node[left= of n3] (l2) {label 2};
\node (l1) at (l2 |- cent0){label 1};
\node (l3) at (l2 |- cent2){label 3};
\end{tikzpicture}
\end{document}

在此輸入影像描述

答案3

您可以避免嵌套圖片

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{positioning}
\begin{document}

\begin{tikzpicture}[auto, semithick,remember picture,
    block/.style={rectangle, draw, 
        minimum width=5em, text centered, rounded corners, minimum
        height=4em,text width=5em}
    ]

    \matrix[matrix of nodes, row sep = 2em,
    nodes={anchor=center}
    ] (mx2){
        % First row:
        label1
        & 
          \node[block,right=1em,anchor=west](n1){node1}; 
          \node[block, right=of n1](n2){node2};         
        \\
        % Second row:
        label2
        &
            \node[block] (n3) {node 3};
            \node[block] (n4) [right=of n3] {node 4};
            \node[block] (n5) [right=of n4]  {node 5};
        \\
    };
    \draw (n1) -- (n4); % this fails
\end{tikzpicture}

\end{document} 

在此輸入影像描述

相關內容