答案1
请注意,我认为我不应该回答代我完成的任务 - 尤其是那些错误地声称他们提供有问题的输出代码的代我完成的任务 - 但有时他们却做了我认为不应该做的事情。当我这样做时,我是为了我自己。如果我的代码恰好有用,那真是幸运;如果没有用,我其实并不在意。我通常不会对解释、改进、更改和微调的请求表示同情。如果它们让我感兴趣,我可能会回应,但不要指望它。如果您想要体面的帮助,您应该提供一个体面的起点。
森林或 Ti钾Z 绘图算法会自动布局节点。否则,你必须确保有足够的空间。如何做到这一点取决于你未能提供的代码。无论如何,我更喜欢 Forest。
\documentclass[border=10pt]{standalone}
\usepackage{forest}
\newcounter{mycounter}
\forestset{
declare count register=lefelau,
lefelau'=0,
twixis/.style={
lefelau'=#1,
delay={
tempcounta'=0,
do while={> RR< {tempcounta}{lefelau}}{
tempcounta'+=1,
where n children=0{
append={[\foresteoption{level}]},
if={> R_={tempcounta}{1}}{}{
append={[\foresteoption{level}]},
},
}{},
do dynamics,
},
},
before typesetting nodes={
circle, fill=blue!50!cyan, draw, text=white, content=X, baseline,
for descendants={content/.process={ Ow {level} {\setcounter{mycounter}{##1}\Alph{mycounter}}}},
where n=1{fill=black, draw, text=white}{draw},
for tree={font=\sffamily},
},
},
}
\begin{document}
\begin{forest}
twixis=3
[]
\end{forest}
\begin{forest}
twixis=4
[]
\end{forest}
\begin{forest}
twixis=5
[]
\end{forest}
\end{document}
答案2
仅作为练习和展示原则......很forest
简单:
\documentclass[margin=3mm]{standalone}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={% style of tree
l sep = 6mm,
s sep = 4mm
%
draw, minimum height=3ex, minimum width=1em, inner sep=0pt,
font = \bfseries
}
[x,for tree={circle, fill=blue,text=white}
[a,for tree={rectangle, fill=black,text=white}
[b
[c
[d]
[d,for tree={fill=white, text=black}]
]
[c,for tree={fill=white, text=black}
[d,for tree={fill=black,text=white}]
[d,for tree={fill=white, text=black}]
]
]
[b
[c,for tree={fill=black,text=white}
[d]
[d,for tree={fill=white, text=black}]
]
[c,for tree={fill=white, text=black}
[d,for tree={fill=black,text=white}]
[d,for tree={fill=white, text=black}]
]
]
]
]
\end{forest}
\end{document}
笔记:为了在树中添加奇特的东西,您首先需要提供树的代码。
编辑(1): 同时@cfr 提供了带有彩色节点的完整代码。所以现在我添加了我的更(粗鲁的)基本解决方案......
编辑(2): 并替代第一个解决方案,也许更直观......
\documentclass[margin=3mm]{standalone}
\usepackage{forest}
\begin{document}
\forestset{
bb/.style = {rectangle,
minimum height=3ex, minimum width=1em,
fill=black, text=white},
wb/.style = {% white box
bb, fill=white, text=black}
}
\begin{forest}
for tree={% style of tree
l sep = 6mm,
s sep = 4mm,
%
draw,
inner sep=0mm,
font = \bfseries
}
[x,for tree={circle, minimum size=3ex, fill=blue,text=white}
[a,bb
[b,bb
[c,bb
[d,bb]
[d,wb]
]
[c,wb
[d,bb]
[d,wb]
]
]
[b,wb
[c,bb
[d,bb]
[d,wb]
]
[c,wb
[d,bb]
[d,wb]
]
]
]
]
\end{forest}
\end{document}