我正在尝试在 latex 中绘制如下所示的图表。我对 TikZ 包了解不多?有没有简单的方法可以在 latex 中绘制此图表?
如果您能就此事提供帮助,我将不胜感激。
答案1
% arara: pdflatex
\documentclass{article}
\usepackage{forest}
\begin{document}
\begin{forest} for tree={%
draw=gray, minimum width=1cm, minimum height=.5cm, rounded corners=3,
text height=1.5ex, text depth=0ex,
grow=east,reversed,
edge={gray},
parent anchor=east,
child anchor=west,
if n children=0{tier=last}{}
}
[A
[b
[l]]
[c
[e[m]][f[n]][g[o]][h[p]]]
[d
[j[r]][k[s]]]
]
\end{forest}
\end{document}
答案2
如果你想要一些更花哨的东西,这里有一个变种LaRiFaRi 的回答:
\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{forest}
\usetikzlibrary{shadows}
\begin{document}
\tikzset{
my node/.style={
draw=gray,
inner color=gray!5,
outer color=gray!10,
thick,
minimum width=1cm,
rounded corners=3,
text height=1.5ex,
text depth=0ex,
font=\sffamily,
drop shadow,
}
}
\begin{forest}
for tree={%
my node,
l sep+=5pt,
grow'=east,
edge={gray, thick},
parent anchor=east,
child anchor=west,
if n children=0{tier=last}{},
edge path={
\noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(10pt,0) |- (.child anchor)\forestoption{edge label};
},
if={isodd(n_children())}{
for children={
if={equal(n,(n_children("!u")+1)/2)}{calign with current}{}
}
}{}
}
[A
[b
[l]]
[c
[e[m]][f[n]][g[o]][h[p]]]
[d
[j[r]][k[s]]]
]
\end{forest}
\end{document}
只是为了好玩,这里有一个版本,显示它forest
不使用与我们其他人相同的字母表;)...
\newcounter{mynode}
\setcounter{mynode}{0}
\begin{forest}
for tree={%
my node,
l sep+=5pt,
grow'=east,
edge={gray, thick},
parent anchor=east,
child anchor=west,
if n children=0{tier=last}{},
edge path={
\noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(10pt,0) |- (.child anchor)\forestoption{edge label};
},
if={isodd(n_children())}{
for children={
if={equal(n,(n_children("!u")+1)/2)}{calign with current}{}
}
}{},
delay={content={\stepcounter{mynode}\alph{mynode}}}
},
[
[
[]]
[
[[]][[]][[]][[]]]
[
[[]][[]]]
]
\end{forest}
或者,如果您想要一些色彩,您可以尝试以下代码:
\documentclass[tikz,border=10pt,multi]{standalone}
\usepackage{forest}
\usetikzlibrary{shadows}
% xcolor manual: 34
\definecolorseries{colours}{hsb}{grad}[hsb]{.575,1,1}{.987,-.234,0}
\resetcolorseries[12]{colours}
\begin{document}
\tikzset{
my node/.style={
draw=gray,
inner color=gray!5,
outer color=gray!10,
thick,
minimum width=12mm,
rounded corners=3,
text height=1.5ex,
text depth=0ex,
font=\sffamily,
drop shadow,
minimum height=6mm,
}
}
\forestset{
my tree/.style={
my node,
l sep+=5pt,
grow'=east,
edge={gray, thick},
parent anchor=east,
child anchor=west,
edge path={
\noexpand\path [draw, \forestoption{edge}] (!u.parent anchor) -- +(10pt,0) |- (.child anchor)\forestoption{edge label};
},
if={isodd(n_children())}{
for children={
if={equal(n,(n_children("!u")+1)/2)}{calign with current}{}
}
}{},
}
}
\newcounter{mynode}
\setcounter{mynode}{0}
\begin{forest}
for tree={
my tree
},
before typesetting nodes={
for tree={
my tree,
content={\color{colours!!+}\stepcounter{mynode}\alph{mynode}},
font=\bfseries\sffamily,
}
},
before packing={
for tree={
if n children=0{tier=last}{},
}
}
[
[[]]
[, delay={repeat=4{append={[[]]}}}
]
[, delay={repeat=2{append={[[]]}}}
]
]
\end{forest}
\end{document}