我正在尝试使用 TikZ 创建家谱。我的家谱可以正常工作,但每个节点上的文本太长,与后面的行重叠(例如下面的“我的家谱”、“父亲的母亲”和“父亲的父亲”)。有办法解决这个问题吗?
平均能量损失
\documentclass{article}
\usepackage{calc}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\newlength\treeheight
\setlength{\treeheight}{\textheight-2cm}
\begin{tikzpicture}[grow=right,anchor=west,
every node/.style={},
level 1/.style={sibling distance=\treeheight/2},
level 2/.style={sibling distance=\treeheight/4},
level 3/.style={sibling distance=\treeheight/8}]
% parents
\node {My Family Tree}[edge from parent fork right]
child { node {Father}
child{ node {Paternal Gfather}
child{ node {Paternal GGfather}}
child{ node {Paternal GGmother}}
}
child{ node {Paternal Gmother}
child{ node {Maternal GGfather}}
child{ node {Maternal GGmother}}
}
}
child { node {Mother}
child{ node {Maternal Gfather}}
child{ node {Maternal Gmother}}
}
;
\end{tikzpicture}
\end{document}
答案1
MWE 中的level distance
是父节点中心与子节点左锚点之间的距离。如果有多个子节点,则边缘在 的一半处分割level distance
。这就是为什么如果节点文本的 (一半) 比 长,线条会与文本重叠的原因level distance
。
使用parent anchor=east
和growth parent anchor=east
是level distance
父节点和子节点边界之间的距离。
为了避免水平盒过满,我level distance
在以下示例中将 减小到 0.5cm。如果不需要观察,\textwidth
您可以删除此更改。
\documentclass{article}
\usepackage{calc}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\newlength\treeheight
\setlength{\treeheight}{\textheight-2cm}
\noindent\begin{tikzpicture}[
grow=right,
anchor=west,
growth parent anchor=east, % added code
parent anchor=east, % added code
level distance=0.5cm, % added code (not necessary)
level 1/.style={sibling distance=\treeheight/2},
level 2/.style={sibling distance=\treeheight/4},
level 3/.style={sibling distance=\treeheight/8}]
% parents
\node {My Family Tree}[edge from parent fork right]
child { node {Father}
child{ node {Paternal Gfather}
child{ node {Paternal GGfather}}
child{ node {Paternal GGmother}}
}
child{ node {Paternal Gmother}
child{ node {Maternal GGfather}}
child{ node {Maternal GGmother}}
}
}
child { node {Mother}
child{ node {Maternal Gfather}}
child{ node {Maternal Gmother}}
}
;
\end{tikzpicture}
\end{document}
要垂直对齐叉子,请在图片选项中添加align=center
或align=left
(它对齐节点中的文本),并将级别样式更改为
level 1/.style={sibling distance=\treeheight/2,text width=\widthof{Mother}},
level 2/.style={sibling distance=\treeheight/4,text width=\widthof{GGmother}},
level 3/.style={sibling distance=\treeheight/8}]
此外还节省了水平空间。
答案2
作为TikZ
树的替代品,你可以使用forest
无需再考虑计算节点之间的距离问题。该软件包可以帮您完成这些工作。
\documentclass{article}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={grow=0,parent anchor=east, child anchor=west, edge path={\noexpand\path[\forestoption{edge}] (\forestOve{\forestove{@parent}}{name}.parent anchor)-- +(0:2mm)|-(\forestove{name}.child anchor)\forestoption{edge label};}}
[My Family Tree
[father
[Paternal Gfather
[Paternal GGfather]
[Paternal GGmother]]
[Paternal Gmother
[Maternal GGfather]
[Maternal GGmother]]]
[mother
[Maternal Gfather]
[Maternal Gmother]]]
\end{forest}
\end{document}
答案3
level distance
尝试在你的级别设置中添加:
level 1/.style={sibling distance=\treeheight/2,level distance=\treeheight/6},
level 2/.style={sibling distance=\treeheight/4,level distance=\treeheight/8},
level 3/.style={sibling distance=\treeheight/8,level distance=\treeheight/4}]