我想要生长一棵有一些空的/缺失的节点的树,以便线直接连接:
\begin{tikzpicture}
\coordinate
child {
child {node {pAp} edge from parent node[left] {x}}
child {node {aaa} edge from parent node[left] {d}}
child {node {AAA} edge from parent node[right] {y}}
edge from parent node[left] {z}}
child {node {} edge from parent node[right] {w}};
\end{tikzpicture}
问题在于“z”和“w”的位置不是水平对齐的,因为 tikz 将空子节点的边缘视为更长,并降低了节点的位置。我尝试使用“父锚点”和“子锚点”等选项,但就是无法让它工作。我可以手动放置节点,但这在编辑树时会非常麻烦。
答案1
您可以将空更改node
为coordinate
:
\begin{tikzpicture}
\coordinate
child {
child {node {pAp} edge from parent node[left] {x}}
child {node {aaa} edge from parent node[left] {d}}
child {node {AAA} edge from parent node[right] {y}}
edge from parent node[left] {z}}
child {coordinate edge from parent node[right] {w}};
\end{tikzpicture}
答案2
和forest
:
\documentclass[margin=3mm]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={
inner xsep=1pt,
% tree
l sep = 12mm,
text height = 1.5ex, text depth = 0.3ex,
if level = 1{s sep = 2mm}{s sep = 12mm},
},
%% edge labels
/tikz/ELS/.style = {% Edge Label Style
pos=0.5, node font=\scriptsize, text height=1.5ex, text depth=0.3ex,
anchor=#1},
EL/.style = {if n=1{edge label={node[ELS=east]{$#1$}}}
{edge label={node[ELS=west]{$#1$}}}
}
[,coordinate
[,coordinate, EL=z
[pAp, EL=x]
[aaa, EL=d]
[AAA, EL=y]
]
[, coordinate, EL=w]
]
\end{forest}
\end{document}
答案3
我只是想增加这种可能性,因为这是我最终使用的。
\begin{tikzpicture}
[font=\vphantom{Ag}]
\coordinate
child {
child {node {pAp} edge from parent node[left] {x}}
child {node {aaa} edge from parent node[left] {d}}
child {node {AAA} edge from parent node[right] {y}}
edge from parent node[left] {z}}
child {node[anchor=north]{} edge from parent node[right] {w}};
\end{tikzpicture}
优点是文本与基线匹配,并且文本也可以添加到同一级别的部分节点(例如,在“w”之后)。对于具有更多级别的树,可能还需要进一步调整,因为如果在“w”之后的节点之后有更多分支,此解决方案将不起作用。