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보다TikZ 스타일을 정의하려면 \tikzset 또는 \tikzstyle을 사용해야 합니까?.

답변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 nodenamerow sepcolumn sep

여기에 이미지 설명을 입력하세요


편집하다Alttermundus가 언급했듯이 미리 정의된 좌표 그리드를 중심으로 행을 쌓을 수 있습니다.

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

여기에 이미지 설명을 입력하세요

관련 정보