答案1
可能的解决方案forest
:
\documentclass[margin=3mm]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta,
shapes.multipart}
\newcommand\mpn[2]{\nodepart{one} #1
\nodepart{two} #2}
\newcommand\rsp{rectangle split parts}
\begin{document}
\tikzset{every label/.style = {font=\footnotesize\sffamily\bfseries}}
\begin{forest}
for tree = {
rectangle split,
draw,
%math content,
%
parent anchor=south,
child anchor=north,
edge = {semithick},
l sep=12mm,
s sep=11mm,
}
[,coordinate
[, coordinate
[123,\rsp=1, label=below:L1]
[,coordinate
[123,\rsp=1,label=below:L2]
[{\mpn{126}{346}},\rsp=2,label=below:L3]
]
]
[, coordinate
[{\mpn{126}{346}},\rsp=2,label=below:L4]
[245,\rsp=1, label=below:L5]
]
]
\end{forest}
\end{document}
答案2
这次我决定采用一种更“简约”的方式,将tikz
代码保持在最低限度。
输出
代码
\documentclass[margin=10pt, tikz]{standalone}
\usepackage{forest}
\tikzset{
fornode/.style={% node definition
draw, text width=1.2cm, line width=.5pt, inner xsep=0, align=center, execute at begin node=\setlength{\baselineskip}{10pt}},
mlabel/.style={% our label
label={[font=\sffamily\bfseries, anchor=north]below:#1}
}
}
\forestset{%
empty nodes/.style={% allows for straight edges when the node is empty
for tree={calign=fixed edge angles},
delay={where content={}{shape=coordinate,for siblings={anchor=north}}{}}
}
}
\newcommand\msep{% So we don't have to write it every time
\\\vspace{-6pt}\rule{\textwidth}{.5pt}\\
}
\begin{document}
\begin{forest}
for tree={%
fornode,
parent anchor=south,
child anchor=north,
s sep=1cm
}
[,draw=none
[,empty nodes
[134, mlabel=L1]
[,empty nodes
[123, mlabel=L2]
[126\msep 346, mlabel=L3]
]
]
[,empty nodes
[234\msep 456, mlabel=L4]
[245, mlabel=L5]
]
]
\end{forest}
\end{document}
答案3
您可以将终端节点实现为简单表。根据您希望节点的外观,您可以使用[b]
参数对齐表(如 L2/L3 节点所示)或不使用参数对齐表(如 L4/L5 节点所示)。如果确实使用了参数[b]
,则必须将整个表格环境包装在内,{...}
以保护[
和]
免受forest
树解析算法的影响。该forest
包不处理树中未标记的节点,因此如果您的树中有这样的节点,总是需要做出一些妥协。内置样式nice empty nodes
给出了不吸引人的结果,因此我改用了样式fairly nice empty nodes
。
\documentclass{article}
\usepackage[linguistics]{forest}
\forestset{fairly nice empty nodes/.style={
delay={where content={}{shape=coordinate,for siblings={
{anchor=north}}}{}}
}
}
\begin{document}
\begin{forest}fairly nice empty nodes
[
[[\begin{tabular}{|c|}\hline1 3 4\\\hline\multicolumn{1}{c}{\textbf{L1}}\end{tabular}
]
[ [{\begin{tabular}[b]{|c|}\hline1 2 3\\\hline\multicolumn{1}{c}{\textbf{L2}}\end{tabular}} % if you use the [b] argument you must wrap the whole tabular in {}
]
[{\begin{tabular}[b]{|c|}\hline1 2 6\\\hline3 4 6\\\hline\multicolumn{1}{c}{\textbf{L3}}\end{tabular}}
]
]
]
[[\begin{tabular}{|c|}\hline1 2 6\\\hline3 4 6\\\hline\multicolumn{1}{c}{\textbf{L4}}\end{tabular}
]
[\begin{tabular}{|c|}\hline2 4 5\\\hline\multicolumn{1}{c}{\textbf{L5}}\end{tabular}
]
]
]
\end{forest}
\end{document}