\documentclass[convert={size=640}]{standalone}
\usepackage{forest}
\newcommand{\difference}{
\begin{tabular}{c|c|c|c|c}
\hline
0&1&0&1&\ldots\\
\hline
\end{tabular}}
\newcommand{\differenceshort}{
\begin{tabular}{c|c|c}
\hline
0&1&\ldots\\
\hline
\end{tabular}}
\begin{document}
\begin{forest}
for tree={
parent anchor=south,
child anchor=north,
inner sep=1pt
}
[\ldots,rotate=90,parent anchor=west,calign=child edge
[\difference,node options={label=right:{\scriptsize Difference 3},
label=left:{\scriptsize Layer 3}}
[\differenceshort,node options={label=right:{\scriptsize Difference 1},
label=left:{\scriptsize Layer 2}}
[\differenceshort] [\differenceshort] [\ldots,base=top]]
[\differenceshort,label=right:{\scriptsize Difference 2}
[\differenceshort] [\differenceshort] [\ldots,label=right:{\scriptsize Inputs}]]]]
\end{forest}
\end{document}
最后一个终端节点和其父节点之间的边与它们的左兄弟节点相交。我如何以编程方式让这些边远离其他节点?
答案1
没有编程方法可以 100% 确保不发生冲突。请参阅文档了解原因。基本上,这样做成本太高了。但是,在这种情况下,情况可以得到很大改善。
在这种情况下,使用 Forest 的align
选项而不是独立指定的tabular
s 可以避免边与节点发生冲突,因为这会将 s 的顶部tabular
与baseline
s 可以避免边与节点发生冲突,因为这会默认Sašo 的评论):
\documentclass[tikz,multi,border=10pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
diff/.style={%
align=c|c|c|c|c,
delay={%
content={%
\hline
0&1&0&1&\ldots\\
\hline
}
}
},
diff short/.style={%
align=c|c|c,
delay={%
content={%
\hline
0&1&\ldots\\
\hline
}
}
},
for tree={
parent anchor=south,
child anchor=north,
inner sep=1pt
}
[\ldots, rotate=90, parent anchor=west, calign=child edge
[, diff, node options={label=right:{\scriptsize Difference 3}, label=left:{\scriptsize Layer 3}}
[, diff short, node options={label=right:{\scriptsize Difference 1}, label=left:{\scriptsize Layer 2}}
[, diff short]
[, diff short]
[\ldots]
]
[, diff short, label=right:{\scriptsize Difference 2}
[, diff short]
[, diff short]
[\ldots, label=right:{\scriptsize Inputs}]
]
]
]
\end{forest}
\end{document}
添加
for siblings={%
anchor=center
},
diff short
当他们有奇数个孩子时,将父母的边缘与他们的中间孩子的边缘对齐,我们得到
但也许最好的选择是添加
base=b
对于树和
for siblings={%
minimum size=1em,
},
和 的定义diff
,diff short
以确保在 之类的东西周围留出更多空间\ldots
。这产生了
完整代码:
\documentclass[tikz,multi,border=10pt]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
diff/.style={%
align=c|c|c|c|c,
for siblings={%
minimum size=1em,
},
delay={%
content={%
\hline
0&1&0&1&\ldots\\
\hline
}
}
},
diff short/.style={%
align=c|c|c,
for siblings={%
minimum size=1em,
},
delay={%
content={%
\hline
0&1&\ldots\\
\hline
}
}
},
for tree={
parent anchor=south,
child anchor=north,
base=b,
inner sep=1pt,
if={isodd(n_children())}{%
tempcounta/.wrap pgfmath arg={#1}{int((n_children()+1)/2)},
for n/.wrap pgfmath arg={{#1}{%
calign with current edge
}}{int((n_children()+1)/2)}
}{}
},
[\ldots, rotate=90, parent anchor=west, calign=child edge
[, diff, node options={label=right:{\scriptsize Difference 3}, label=left:{\scriptsize Layer 3}}
[, diff short, node options={label=right:{\scriptsize Difference 1}, label=left:{\scriptsize Layer 2}}
[, diff short]
[, diff short]
[\ldots]
]
[, diff short, label=right:{\scriptsize Difference 2}
[, diff short]
[, diff short]
[\ldots, label=right:{\scriptsize Inputs}]
]
]
]
\end{forest}
\end{document}