我有一棵茂密的树。我用森林包画了它。代码如下。它可能效率低下,但我希望以尽可能少的代码中断继续前进。我的问题是:
1.树是不对称的。所以,默认对齐是按级别进行的。如何将所有叶子对齐在同一级别?
2.如何使某些节点变成正方形,而其他节点保持圆形?
3.如何从根到特定叶子为路径着色不同的颜色?
4.我使用 \resizebox 使树适合页面。假设看到树的人将页面转为横向模式,是否有一个全局选项可以设置将所有节点文本垂直转动?
欢迎提出有关如何最好地展示这棵树的任何其他建议/想法。
\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\geometry{paper=a5paper}
\usepackage{graphics}
\usepackage[T1]{fontenc}
\usepackage{epsfig}
\usepackage{amsmath}
\usepackage{lipsum}
\usepackage[toc,page]{appendix}
\usepackage{tikz}
\usetikzlibrary{shapes, positioning}
\usepackage{color,soul}
\usepackage{url}
\usepackage{tikz-qtree}
\usepackage{forest}
\begin{document}
\resizebox{!}{\textheight}{%
\begin{forest} %Figure out how to align edge labels with the edge
for tree={grow=east,circle,draw, align=center}
[\tiny{HP}
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{NB}} %not built
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}} % not built and closed
[\tiny{C},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{HP}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{M}}
[\tiny{HP} %1st line copied for NB and closed. No IP involved here
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
] %Last line copied for NB and closed. No Ip.
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{HP} %1st line copied for NB and closed. No IP involved here
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
]
[\tiny{C},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{HP} %1st line copied for NB and closed. No IP involved here
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{M}}
[\tiny{HP} %1st line copied for NB and closed. No IP involved here
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{HP} %1st line copied for NB and closed. No IP involved here
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}} % not built and open
[\tiny{C},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{HP}
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{M}}
[\tiny{HP} %last line I added
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
] %last line I added
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{HP} %last line I added
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
] %last line I added
]
]
[\tiny{C},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{HP} %last line I added
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
] %last line I added
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{M}}
[\tiny{HP} %last line I added
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
] %last line I added
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{HP} %last line I added
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
] %last line I added
]
]
]
]
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{B}} % built
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}} % built and closed
[\tiny{C},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{G}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{M}}
[\tiny{G}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{G}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
[\tiny{C},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{G}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{M}}
[\tiny{G}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{G}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}} % built and Open
[\tiny{C},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{IP}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{M}}
[\tiny{IP}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{IP}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
]
[\tiny{C},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{IP}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{M}}
[\tiny{IP}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{IP}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
]
]
]
]
\end{forest}
}
\end{document}
答案1
更新代码无需修改即可与新版本 (2) 一起使用森林。
- 树是不对称的。因此,默认对齐方式是按级别对齐。如何将所有叶子对齐在同一级别?
tier=<tier name>
这可以通过设置所有终端节点来实现。要自动执行此操作,请将此代码添加到for tree
:
if n children=0{
tier=terminal
}{}
- 如何使某些节点变成正方形,而其他节点保持为圆形?
您可以添加shape=rectangle
那些您想要的非圆形。如果您想强制它们成为正方形,您可以输入它们的宽度和高度,但这可能会产生奇怪的结果。或者您想要成为正方形的和您想要成为圆形的是否有规律?
- 如何将一条从根到特定叶子的路径涂成不同的颜色?
您可以使用node walk
它来追溯从叶子到根的路径。例如,这里有一个样式,它接受一个参数(颜色),并用该颜色为回到根的路径着色:
\forestset{
colour my roots/.style={
before typesetting nodes={
edge=#1,
for ancestors={
edge=#1,
#1,
},
#1,
}
}
}
要使用它,只需添加colour my roots=<colour>
到您选择的节点即可。(不必是叶子 - 它将从其所在的位置开始。请注意,如果您多次使用此样式,则根节点将以最后使用的颜色结束,任何常见路径段也是如此。
这是一个简单的例子:
\documentclass[tikz, border=10pt, multi]{standalone}
\usepackage{forest}
\forestset{
colour my roots/.style={
before typesetting nodes={
edge=#1,
for ancestors={
edge=#1,
#1,
},
#1,
}
},
my edge label/.style={
edge label={
node [midway, fill=white, font=\scriptsize] {#1}
}
}
}
\begin{document}
\begin{forest}
for tree={
circle,
inner sep=1pt,
l sep+=5pt,
draw,
align=center,
if n children=0{
tier=terminal
}{},
}
[HP
[IP, my edge label=NB
[ABC
[DEF]
[GEH, colour my roots=blue]
]
[HIJ
[LMN, colour my roots=red
[OPQ]
]
[RST]
]
]
]
\end{forest}
\end{document}
以下是示例结果:
- 我使用 \resizebox 来让树适合页面。假设看到树的人将页面转为横向模式,是否有一个全局选项可以让我设置将所有节点文本垂直转?
不要。这会让事情变得不必要地复杂化。相反,用默认的生长方向绘制树并将其放在环境中landscape
。(或者,sidewaysfigure
如果您不想翻页,请使用。
完整代码:
\documentclass[a5paper]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage[T1]{fontenc}
\usepackage{forest,pdflscape}
\forestset{
colour my roots/.style={
before typesetting nodes={
edge=#1,
for ancestors={
edge=#1,
#1,
},
#1,
}
}
}
\begin{document}
\newlength\myheight
\setlength\myheight{\textheight}%
\begin{landscape}
\noindent
\resizebox{\myheight}{!}{%
\begin{forest} %Figure out how to align edge labels with the edge
for tree={
circle,
draw,
align=center,
if n children=0{
tier=terminal
}{}
}
[\tiny{HP}
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{NB}} %not built
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}} % not built and closed
[\tiny{C},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{HP}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{M}}, for tree={shape=rectangle}
[\tiny{HP} %1st line copied for NB and closed. No IP involved here
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}, colour my roots=blue]
]
] %Last line copied for NB and closed. No Ip.
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{HP} %1st line copied for NB and closed. No IP involved here
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
]
[\tiny{C},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{HP} %1st line copied for NB and closed. No IP involved here
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{M}}
[\tiny{HP} %1st line copied for NB and closed. No IP involved here
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{NB}}, shape=rectangle
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}, colour my roots=red]
]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{HP} %1st line copied for NB and closed. No IP involved here
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}} % not built and open
[\tiny{C},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{HP}
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{M}}
[\tiny{HP} %last line I added
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
] %last line I added
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{HP} %last line I added
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
] %last line I added
]
]
[\tiny{C},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{HP} %last line I added
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
] %last line I added
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{M}}
[\tiny{HP} %last line I added
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
] %last line I added
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{HP} %last line I added
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{NB}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{B}}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
] %last line I added
]
]
]
]
[\tiny{IP},edge label={node[midway,fill=white,font=\scriptsize]{B}} % built
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}} % built and closed
[\tiny{C},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{G}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{M}}
[\tiny{G}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{G}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
[\tiny{C},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{G}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{M}}
[\tiny{G}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{G}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}} % built and Open
[\tiny{C},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{IP}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{M}}
[\tiny{IP}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{IP}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
]
[\tiny{C},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{L}}
[\tiny{IP}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}, colour my roots=green!50!black]
]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{M}}
[\tiny{IP}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
[\tiny{25},edge label={node[midway,fill=white,font=\scriptsize]{H}}
[\tiny{IP}
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{C}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
[\tiny{G},edge label={node[midway,fill=white,font=\scriptsize]{O}}
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{L}}]
[\tiny{35},edge label={node[midway,fill=white,font=\scriptsize]{H}}]
]
]
]
]
]
]
]
\end{forest}%
}%
\end{landscape}
\end{document}
这对代码的改动很小。但请注意,你可以让生活简单得多如果您使用几种样式而不是经常重复代码,那就为自己吧。
事实上,只需添加font=\tiny
就可以节省每个节点内容的for tree
书写。\tiny{}
如果您也能管理样式,标签可以大大简化。如果标签或内容有模式,您可以进一步自动化树。如果没有,您将得到如下结果:
\documentclass[a5paper]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage[T1]{fontenc}
\usepackage{forest,pdflscape}
\forestset{
colour my roots/.style={
before typesetting nodes={
edge=#1,
for ancestors={
edge=#1,
#1,
},
#1,
}
},
my edge label/.style={
edge label={
node [midway, fill=white, font=\scriptsize] {#1}
}
}
}
\begin{document}
\newlength\myheight
\setlength\myheight{\textheight}%
\begin{landscape}
\noindent
\resizebox{\myheight}{!}{%
\begin{forest} %Figure out how to align edge labels with the edge
for tree={
circle,
draw,
align=center,
if n children=0{
tier=terminal
}{},
font=\tiny
}
[HP
[IP, my edge label=NB %not built
...
]
]
\end{forest}%
}%
\end{landscape}
\end{document}
它仍然没有那么混乱,而且肯定也没有那么重复的打字!
最后,如果你的实际树与此类似,你需要重新考虑呈现这些信息的方式。
此刻没人能阅读任何东西。即使人们只阅读电子书籍,并可以快速浏览(但既然如此,何必拘泥于一小页呢?真正大尺寸的电子纸仍然很便宜!),也根本不会明显看出有什么值得快速浏览的东西。
最好的情况是,你会激怒你的读者。最坏的情况是,他们会沮丧地放弃。
答案2
添加到序言:(
where n children=0{tier=bottom}{}
不要忘记前面的逗号!)rectangle
向这些节点添加选项在那片叶子上,添加
for ancestors={edge=red}
使用
tikz
的键rotate
来旋转节点。 在您的例子中,将其添加for tree={rotate=90}
到前导码,以及rotate=90
所有边缘标签节点。
我实际上建议定义一种样式来指定边缘标签。将其放在你的序言中:
my edge label/.style={edge label={node[midway,fill=white,font=\scriptsize]{#1}}},
my edge label=NB
然后通过书写等来指定边标签。