
我發現如果我使用圖形佈局,相對定位不起作用。考慮以下範例:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{
graphdrawing
, graphs
, positioning
}
\usegdlibrary{trees}
\begin{document}
\begin{tikzpicture}[binary tree layout]
\graph {
n0 -> {n1, n2};
};
\node [left = of n0] {left of n0};
\end{tikzpicture}
\end{document}
它會產生以下意外輸出。
幾乎所有與放置相關的東西都不起作用。你可以說at (n0.center)
,最後一個節點將在同一個地方結束。如何相對於圖中的節點放置節點?
答案1
你正在傳遞binary tree layout
給tikzpicture
環境。如果您將其移至傳遞給 的選項\graph
,如下所示:\graph[binary tree layout]
,您的程式碼將有效。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{
graphdrawing
, graphs
, positioning
}
\usegdlibrary{trees}
\begin{document}
\begin{tikzpicture}%[binary tree layout]
\graph[binary tree layout] {
n0 -> {n1, n2};
};
\node[left= of n0] {left of n0};
\end{tikzpicture}
\end{document}
答案2
您可以使用樹木庫來實現這一點。您可以透過為節點提供節點名稱來定位節點。
代碼是
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[level distance=1.5cm,
level 1/.style={sibling distance=3cm}]
\node (n0) {n0}
child {node (n1) {n1}}
child {node (n2) {n2}
};
\node [left of=n0, xshift=-1cm] {left of n0};
\end{tikzpicture}
\end{document}
輸出如下