TikZ에 상태 트리 구조를 그릴 수 있는 라이브러리가 있나요?

TikZ에 상태 트리 구조를 그릴 수 있는 라이브러리가 있나요?

State tree structure (STS)state charts는 s 계열 에 속하는 특별한 종류의 다이어그램입니다 .

전형적인 모습은 STS아래와 같이 묘사될 수 있습니다:

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

가지의 수, 상태 라벨, 십자가 또는 결합 기호는 디자이너가 의도한 대로 임의의 구조로 형성될 수 있습니다.

대략적인 것일 수도 있지만 tikz-trees확인해 보니 일반적으로 그림과 같은 것을 생성할 수 있다고 생각하지 않았습니다.

에 어떤 종류의 것들을 위한 전문 라이브러리가 있습니까 TikZ?!

답변1

state tree다음은 이 유형의 나무 형식을 지정하기 위해 숲 스타일을 정의하는 버전입니다 . 예제를 기반으로 한 가정은 다음과 같습니다.

  • 아이들은 고르게 퍼져야 합니다.
  • 첫 번째 자식과 마지막 자식은 부모로부터 등거리에 있어야 합니다.
  • 가장자리는 첫 번째 자식과 마지막 자식에 대해서만 그려야 합니다.
  • 노드의 내용은 수학 모드에서 조판되어야 합니다.

이러한 가정 중 하나 이상에 수정이 필요한 경우 스타일을 수정할 수 있습니다.

다음은 트리에 대한 코드입니다.이그나시의:

\begin{forest}
  state tree,
  [x_0
    [x_1
      [x_{11}
        [x_{111}]
        [\dot{\cup}]
        [x_{112}]
      ]
      [\times]
      [x_{12}
        [x_{121}]
        [\dot{\cup}]
        [x_{122}]
      ]
    ]
    [\dot{\cup}]
    [x_2]
    [\dot{\cup}]
    [x_3
      [x_{31}]
      [\dot{\cup}]
      [x_{32}]
      [\dot{\cup}]
      [x_{33}]
    ]
  ]
\end{forest}

state tree스타일을 설정합니다. 나머지는 자동이므로 no edge등이 필요하지 않습니다.

그러면 다음과 같은 결과가 생성됩니다.

상태 트리

전체 코드:

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage{forest}
% ateb: addaswyd o ateb Ignasi: https://tex.stackexchange.com/a/351690/
\forestset{%
  state tree/.style={%
    for tree={
      math content,
      parent anchor=children,
      child anchor=parent,
      tier/.option=level,
      calign=center,
    },
    where={>On>{n children}{2}}{
      for nodewalk={
        filter={children}{>On=!On=!&{n}{1}{n'}{1}}
      }{no edge}
    }{},
    before computing xy={
      where={isodd(n_children)}{
        tempdima/.process={OOOw3+d{!n=1.s}{!n'=1.s}{n children}{(##2-##1)/(##3-1)}},
        tempdimb/.option={!n=1.s},
        for children={
          s/.process={RROw3+d{tempdima}{tempdimb}{n}{##2+(##1*(##3-1))}}
        },
      }{},
    },
  },
}
\begin{document}
\begin{forest}
  state tree,
  [x_0
    [x_1
      [x_{11}
        [x_{111}]
        [\dot{\cup}]
        [x_{112}]
      ]
      [\times]
      [x_{12}
        [x_{121}]
        [\dot{\cup}]
        [x_{122}]
      ]
    ]
    [\dot{\cup}]
    [x_2]
    [\dot{\cup}]
    [x_3
      [x_{31}]
      [\dot{\cup}]
      [x_{32}]
      [\dot{\cup}]
      [x_{33}]
    ]
  ]
\end{forest}
\end{document}

답변2

forest일부 어린이를 올바르게 배포하는 방법을 모르지만 로 시도해 볼 수도 있습니다 .

\documentclass [multi=forest, border=2mm]{standalone} 
\usepackage{forest}

\begin{document}
\begin{forest}
for tree={math content}
[x_0
    [x_1 
        [x_{11} 
            [x_{111}] 
            [\dot{\cup}, no edge] 
            [x_{112}]]
        [\times, no edge]
        [x_{12}
            [x_{121}]
            [\dot{\cup}, no edge]
            [x_{122}]]]
    [\dot{\cup}, no edge]
    [x_2, no edge]
    [\dot{\cup}, no edge]
    [x_3
        [x_{31}]
        [\dot{\cup}, no edge]
        [x_{32}, no edge]
        [\dot{\cup}, no edge]
        [x_{33}]]]
\end{forest}

\end{document}

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

업데이트: JLDiaz 제안으로 결과가 더 좋아 보이지만 여전히 완벽하지는 않지만 더 좋습니다.

\documentclass [multi=forest, border=2mm]{standalone} 
\usepackage{forest}

\begin{document}
\begin{forest}
for tree={math content}
[x_0
    [x_1 
        [x_{11} 
            [x_{111}] 
            [\dot{\cup}, no edge] 
            [x_{112}]]
        [\null, no edge]  %<-------------
        [\times, no edge]
        [x_{12}
            [x_{121}]
            [\dot{\cup}, no edge]
            [x_{122}]]]
    [\null, no edge]        %<-------------
    [\dot{\cup}, no edge]
    [x_2, no edge]
    [\dot{\cup}, no edge]
    [x_3
        [x_{31}]
        [\dot{\cup}, no edge]
        [x_{32}, no edge]
        [\dot{\cup}, no edge]
        [x_{33}]]]
\end{forest}

\end{document}

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

답변3

첫 번째 시도는 다음과 같습니다. 저는 나무를 그리는 데 경험이 많지 않으므로 이 작업을 수행하는 더 좋은 방법이 있을 수 있습니다. 그러나 핵심은 자식 사이에 노드를 배치하는 것이며 calc라이브러리는 이에 적합합니다.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\def\rootname{root}
\newcommand{\stnode}[3]{\node at ($(#1)!0.5!(#2)$) { #3 }}
\newcommand{\stnodec}[3]{\node at ($(#1.center)!0.5!(#2.center)$) { #3 }}
\newcommand{\stnoderootc}[3]{\node at ($(\rootname-#1.center)!0.5!(\rootname-#2.center)$) { #3 }}

\begin{document}
    \begin{tikzpicture}
        [
            level 1/.style = {sibling distance = 3cm},
            level 2/.style = {sibling distance = 2cm}
        ]
        \node (root) { $x_0$ }
            child { node { $x_1$ } 
                child { node { $x_3$ } }
                child { node { $x_4$ } }
            }
            child { node { $x_2$ } 
                child { node { $x_5$ } }
                child { node { $x_6$ } }
            }
        ;
        place the annotation nodes between the children
        \stnode{root-1.center}{root-2.center}{$\times$};
        \stnodec{root-1-1}{root-1-2}{$\times$};
        \stnoderootc{2-1}{2-2}{$\times$};
    \end{tikzpicture}
\end{document}

세 가지 매크로( \stnode,\stnodec (), \stnoderootc(자동 루트 노드, 중심))을 입력하여 주석 노드를 배치합니다. 루트 노드의 실제 이름을 나타내려면 루트 이름을 다시 정의해야 합니다. 유연성을 극대화하려면 간단히 \stnode.

인수는 #1:첫 번째 노드, #2: 두 번째 노드, #3: 노드 텍스트입니다.

결과는 다음과 같습니다. 결과

분명히 각 노드는 동일한 y 위치를 가져야 하며, 그렇지 않은 경우에도 여전히 작동하지만 이상하게 보일 것입니다.

관련 정보