
我正在 tikz 中繪製一棵樹,但是,似乎子節點已經旋轉,使得進入它們的線偏移並穿過。參見例如這張圖片。特別注意線條如何偏離中心以及如何穿過藍色球。。
我用以下方法繪製了圖片:
\includepackage{tikz}
\tikzstyle planar forest=[scale=1, sibling distance=0, level distance=0, semithick]
\tikzstyle planar forest node=[scale=1, shape=circle, semithick, draw]
\tikzstyle b=[style=planar forest node, fill=black]
\tikzstyle bb=[style=planar forest node, fill=blue]
定義環境且:
\tikz[planar forest] {
\node [b, label=right:] at (0.0, 0.0) {}
child {node [bb, label=right:] at (-0.5, 1.0) {}
}
child {node [b, label=right:] at (0.5, 1.0) {}
}
;
}
繪製實際圖形。有人知道為什麼它看起來這麼難看?
答案1
據我所知,基本問題是您將樹結構與所有節點的手動放置混合在一起。所以,如果我必須冒險猜測的話,我會猜測你的代碼 --- 重現問題的代碼,而不是您共享的代碼 --- 您正在告訴 TikZ 表示樹朝一個方向生長,同時手動強制節點的位置,就好像它朝另一個方向生長一樣。
使用一棵樹然後將sibling distance
和都設為level distance
零是沒有意義的。至少,我可能只是缺乏想像力,但我想不出你想要這樣做的充分理由。
這是樹的一個版本,已更新以刪除已棄用的內容\tikzstyle
並修改為實際上的樹。
\tikzset{
planar forest node/.style={shape=circle, semithick, draw},
b/.style={style=planar forest node, fill=black},
bb/.style={style=planar forest node, fill=blue},
}
\tikz {
\node [b, label=right:A] {}
child {node [bb, label=right:B] {}
}
child {node [b, label=right:C] {}
}
;
}
或者,與grow=up
:
如果我努力嘗試,我可以得出你的輸出的近似值。
但我透過歸零level
和sibling distance
設定得到了這個yscale=-.75
:
\tikz [sibling distance=0, level distance=0, yscale=-.75] {
\node [b] {}
child {node [bb] at (-.5,1) {}
}
child {node [b] at (.5,1) {}
}
;
}
這並不是一件顯而易見的事。
為了完整起見,這裡有一個forest
:
\begin{forest}
[, b
[, bb
]
[, b
]
]
\end{forest}
它根據更簡潔的規範生成一棵樹。
完整程式碼:
\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{forest}
\begin{document}
\tikzset{
planar forest node/.style={shape=circle, semithick, draw},
b/.style={style=planar forest node, fill=black},
bb/.style={style=planar forest node, fill=blue},
}
\tikz {
\node [b, label=right:A] {}
child {node [bb, label=right:B] {}
}
child {node [b, label=right:C] {}
}
;
}
\tikz [grow=up] {
\node [b, label=right:A] {}
child {node [bb, label=right:B] {}
}
child {node [b, label=right:C] {}
}
;
}
\tikz [sibling distance=0, level distance=0, yscale=-.75] {
\node [b] {}
child {node [bb] at (-.5,1) {}
}
child {node [b] at (.5,1) {}
}
;
}
\begin{forest}
[, b
[, bb
]
[, b
]
]
\end{forest}
\end{document}