
graphs
TikZ의 라이브러리를 알고리즘과 함께 사용하여 graphdrawing
많은 노드가 있는 그래프를 만들고 나중에 만들고 레이블을 지정할 연결을 만들고 싶습니다 .
더 복잡한 버전이 있습니다.
\tikz [nodes={fill=white, font=\footnotesize},
>={Stealth[round,sep]}]
\graph[layered layout, branch down sep, nodes=draw, edges=rounded corners]
{
"cat" -- "dog" -- {"cow", "goat", "sheep"};
"cow" -- "bull";
"dog" -- "rooster" -- "duck";
};
내가하고 싶은 것은 노드를 나열하는 것입니다미리, 예를 들어 a, b, c로 레이블을 지정합니다. 그런 다음 체인을 생성할 때 참조 라벨을 사용하고 싶습니다.
내가 이렇게 하고 싶은 이유는 각 노드의 실제 텍스트가 "cat"보다 훨씬 길고, 새로운 연결을 만들고 싶을 때마다 "supercalifragalistic"이라고 입력하는 번거로움을 피하고 싶기 때문입니다.
이상적으로는 다음과 같은 것이 있습니다.
\begin{tikzpicture}
\tikz [nodes={fill=white, font=\footnotesize},
>={Stealth[round,sep]}]
\node (a) {cat};
\node (b) {dog};
\node (c) {cow};
\node (d) {goat};
\node (e) {sheep};
\node (f) {bull};
\node (g) {rooster};
\node (i) {duck};
\graph[use existing nodes = true, layered layout, branch down sep, nodes=draw, edges=rounded corners]
{
"cat" -- "dog" -- {"cow", "goat", "sheep"};
"cow" -- "bull";
"dog" -- "rooster" -- "duck";
(a) -- (b) -- { (c), (d), (e)};
(c) -- (f);
(b) -- (g) -- (i);
};
\end{tikzpicture}
하지만 이 접근 방식에서는 오류가 발생합니다 attempting to create edge between nodes that are not in the graph
.
이것이 가능한가? 나는 다음과 같이 작동하는 약간 다른 접근 방식을 보았습니다.
\begin{tikzpicture}[new set=import nodes]
\begin{scope}[nodes={set=import nodes}] % make all nodes part of this set
\node [red] (a) at (0,1) {$a$};
\node [red] (b) at (1,1) {$b$};
\node [red] (d) at (2,1) {$d$};
\end{scope}
\graph{
(import nodes);
% "import" the nodes
a -> b -> c -> d -> e;
};
\end{tikzpicture}
하지만 여기에는 그래프 그리기 알고리즘( )이 없으며 layered layout
내가 피하고 싶은 노드의 위치를 지정해야 합니다.
답변1
다음을 사용하여 연결하지 않고 선언할 수 있습니다.
<node name>/<node text>
통사론.
그런 다음 <node name>
실제로 노드를 연결할 때 이를 참조합니다.
암호
\documentclass[tikz,convert]{standalone}
\usetikzlibrary{arrows.meta,graphs,graphdrawing}
\usegdlibrary{layered}
\begin{document}
\tikz[
nodes={fill=white, font=\footnotesize},
>={Stealth[round,sep]}
]
\graph[
layered layout,
branch down sep,
nodes={draw, text height=height("a"), text depth=+0pt},
edges=rounded corners
] {
a/cat, b/dog, c/cow, d/goat, e/sheep, f/bull, g/rooster, i/duck,
a -- b -- {c, d, e},
c -- f,
b -- g -- i,
};
\end{document}