作为图形导出功能的一部分,我尝试在 TikZ 中渲染一棵树,其中所有节点位置都准确给出并且边是叉形的。
在没有树木包要求我根据给定的节点坐标自己计算边缘“角”,这是我想避免的。
向 TikZ 树添加明确定位的节点会弄乱布局,因为(根据手册),TikZ 正在更改每个子节点的坐标系。
没有树木:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\node at (10, 10) (root) {root} ;
\node at (10, 7) (lvl1middle) {lvl1middle} ;
\node at (8, 6) (lvl2left) {lvl2left} ;
\node at (13, 5) (lvl2right) {lvl2right} ;
\node at (10, 5) (lvl2middle) {lvl2middle} ;
\draw (root) -- (lvl1middle);
\draw (lvl1middle) -- (lvl2left);
\draw (lvl1middle) -- (lvl2middle);
\draw (lvl1middle) -- (lvl2right);
\end{tikzpicture}
\end{figure}
\end{document}
有樹:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\node at (10, 10) (root) {root}
[edge from parent fork down]
child { node at (10, 7) (lvl1middle) {lvl1middle}
child { node at (8, 6) (lvl2left) {lvl2left} }
child { node at (13, 5) (lvl2right) {lvl2right} }
child { node at (10, 5) (lvl2middle) {lvl2middle} }
};
\end{tikzpicture}
\end{figure}
\end{document}
答案1
只是安装了一个我们可以用于的edge from parent fork down
特殊函数。原始定义是edge from parent path
to path
\tikzset{edge from parent fork down/.style={
edge from parent path={(\tikzparentnode\tikzparentanchor) -- +(0pt,-.5\tikzleveldistance)
-| (\tikzchildnode\tikzchildanchor)}}}
默认\tikzleveldistance
值为15mm
。
一个解决方案to path
是
\tikzset{
edge down and up again/.style={
to path={
|- ([shift={(\tikztotarget.south)}] +0pt,+-2.5mm) -- (\tikztotarget) }}}
在您的示例中,我必须切换回默认路径line to
(即--
),因为边缘必须以这种方式绕过目标节点。
代码
\documentclass[tikz,convert=false]{standalone}
\tikzset{
edge down and up again/.style={
to path={
|- ([shift={(\tikztotarget.south)}] +0pt,+-2.5mm) -- (\tikztotarget) }}}
\begin{document}
\begin{tikzpicture}
\node at (10, 10) (root) {root} ;
\node at (10, 7) (lvl1middle) {lvl1middle} ;
\node at (8, 6) (lvl2left) {lvl2left} ;
\node at (13, 5) (lvl2right) {lvl2right} ;
\node at (10, 5) (lvl2middle) {lvl2middle} ;
\path[edge down and up again] (root) edge [line to] (lvl1middle)
(lvl1middle) edge (lvl2left)
edge [line to] (lvl2middle)
(lvl2middle) edge (lvl2right);
\end{tikzpicture}
\end{document}