我如何从暴露的最右侧节点绘制箭头到右侧对齐的值。
\documentclass[landscape]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{forest}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\begin{document}
$$
\begin{array}{lr}
\begin{forest}
for tree={
l sep=2.5em
}
[$cn^2$
[$c\left(\frac{n}{4}\right)^2$
[$c\left(\frac{n}{16}\right)^2$
[]
[]
[]
]
[$c\left(\frac{n}{16}\right)^2$
[]
[]
[]
]
[$c\left(\frac{n}{16}\right)^2$
[]
[]
[]
]
]
[$c\left(\frac{n}{4}\right)^2$
[$c\left(\frac{n}{16}\right)^2$
[]
[]
[]
]
[$c\left(\frac{n}{16}\right)^2$
[]
[]
[]
]
[$c\left(\frac{n}{16}\right)^2$
[]
[]
[]
]
]
[$c\left(\frac{n}{4}\right)^2$
[$c\left(\frac{n}{16}\right)^2$
[]
[]
[]
]
[$c\left(\frac{n}{16}\right)^2$
[]
[]
[]
]
[$c\left(\frac{n}{16}\right)^2$
[]
[]
[]
]
]
]
\end{forest} &
\quad\begin{aligned}
&cn^2 \\ \\
&\frac{3}{16}cn^2 \\ \\
&\left(\frac{3}{16}\right)^2cn^2 \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\
\end{aligned}
\end{array}
$$
\end{document}
由此得出下图:
但我想要的是类似的东西:
我还希望右边的 3 个方程与它们对应的树上的级别对齐。
本质上,我正在尝试重新创建我的书中的图表 d):
答案1
像这样?
我命名了最右边的森林节点,并在它们右边放置了方程式。然后连接线的绘制很简单:
\documentclass[landscape]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{forest}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\begin{document}
\[
\begin{forest}
for tree={
l sep=2.5em
}
[$cn^2$,name=L1
[$c\left(\frac{n}{4}\right)^2$
[$c\left(\frac{n}{16}\right)^2$
[]
[]
[]
]
[$c\left(\frac{n}{16}\right)^2$
[]
[]
[]
]
[$c\left(\frac{n}{16}\right)^2$
[]
[]
[]
]
]
[$c\left(\frac{n}{4}\right)^2$
[$c\left(\frac{n}{16}\right)^2$
[]
[]
[]
]
[$c\left(\frac{n}{16}\right)^2$
[]
[]
[]
]
[$c\left(\frac{n}{16}\right)^2$
[]
[]
[]
]
]
[$c\left(\frac{n}{4}\right)^2$, name=L2
[$c\left(\frac{n}{16}\right)^2$
[]
[]
[]
]
[$c\left(\frac{n}{16}\right)^2$
[]
[]
[]
]
[$c\left(\frac{n}{16}\right)^2$, name=L3
[]
[]
[]
]
]
]
\node (a) [right=of L1 -| L3.east] {$cn^2$};
\node (b) [right=of L2 -| L3.east] {$\frac{3}{16}cn^2$};
\node (c) [right=of L3.east] {$\left(\frac{3}{16}\right)^2 cn^2$};
\draw[dashed,->] (L1) -- (a);
\draw[dashed,->] (L2) -- (b);
\draw[dashed,->] (L3) -- (c);
\end{forest}
\]
\end{document}
答案2
此代码定义了level markers
您希望标记级别的树的样式以及level mark
定义标记的样式。level mark
可以添加到相关级别的任何节点。代码要求该值需要数学模式。(如果您需要文本,这将很容易更改。)
新的 nodewalk 步骤,tier end
移动到当前 上的最后一个节点tier
。level markers
样式设置tier
以对应于树的级别。
\documentclass[border=10pt]{article}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\forestset{
define long step={tier end}{}{
group={
while nodewalk valid={next on tier}{next on tier},
current
}
},
level markers/.style={
tikz+={
\coordinate (tree marker) at ([xshift=10pt]current bounding box.east);
},
before typesetting nodes={
for tree={
tier/.option=level,
},
},
},
level mark/.style={
before drawing tree={
for tier end={
tikz+={
\node (b) [anchor=mid west] at (tree marker |- .mid) {$#1$};
\draw [densely dashed, -LaTeX] (.east) -- (b.west |- .center);
},
},
},
}
}
\begin{document}
\begin{forest}
for tree={
l sep=2.5em,
math content,
},
level markers,
[cn^2, level mark=cn^2
[c\left(\frac{n}{4}\right)^2, level mark=\frac{3}{16}cn^2
[c\left(\frac{n}{16}\right)^2, level mark=\left(\frac{3}{15}\right)^2n^2
[]
[]
[]
]
[c\left(\frac{n}{16}\right)^2
[]
[]
[]
]
[c\left(\frac{n}{16}\right)^2
[]
[]
[]
]
]
[c\left(\frac{n}{4}\right)^2
[c\left(\frac{n}{16}\right)^2
[]
[]
[]
]
[c\left(\frac{n}{16}\right)^2
[]
[]
[]
]
[c\left(\frac{n}{16}\right)^2
[]
[]
[]
]
]
[c\left(\frac{n}{4}\right)^2
[c\left(\frac{n}{16}\right)^2
[]
[]
[]
]
[c\left(\frac{n}{16}\right)^2
[]
[]
[]
]
[c\left(\frac{n}{16}\right)^2
[]
[]
[]
]
]
]
\end{forest}
\end{document}
编辑[根据修正后的水平标记]
此代码显示了如何自动绘制树。它定义了一个样式,auto level markers
该样式需要 3 个参数:每个父级拥有的子级数/分母、分母以及要填充和标记的级别数。例如,要求 3 个级别将产生一个 4 级树,其中第四级由没有内容且没有级别标记的节点组成。
\documentclass[border=10pt,tikz]{standalone}
\usepackage{forest}
\usetikzlibrary{arrows.meta}
\forestset{
define long step={tier end}{}{
group={
while nodewalk valid={next on tier}{next on tier},
current
}
},
level markers/.style={
tikz+={
\coordinate (tree marker) at ([xshift=10pt]current bounding box.east);
},
before typesetting nodes={
for tree={
tier/.option=level,
},
},
},
level mark/.style={
before drawing tree={
for tier end={
tikz+={
\node (b) [anchor=mid west] at (tree marker |- .mid) {$#1$};
\draw [densely dashed, -LaTeX] (.east) -- (b.west |- .center);
},
},
},
},
declare count register={auto numerator},
declare count register={auto denominator},
declare count register={auto levels},
declare count register={auto denominator squared},
Autoforward register={auto denominator}{
auto denominator squared=#1*#1,
},
auto numerator'=3,
auto denominator'=4,
auto levels'=3,
auto level markers/.style n args=3{
level markers,
auto numerator=#1,
auto denominator=#2,
auto levels=#3,
delay={
tempcounta'=0,
while={
> RR< {tempcounta}{auto levels}
}{
where n children=0{
repeat={
>R{auto numerator}
}{
append={[]}
}
}{},
do dynamics,
tempcounta'+=1,
}
},
before typesetting nodes={
where={
> OR< {level} {auto levels}
}{
if level=0{
content=cn^2,
math content
}{
content/.process={
OR w2+P w {level} {auto denominator} {int(##2^##1)} { c\left(\frac{n}{##1}\right)^2 }
},
math content
}
}{},
for nodewalk={
r,
while={
> ORw+n< {level} {auto levels} {##1-1}
}{l}
}{
if level>=2{
level mark/.process={ O R R w3 {level} {auto numerator} {auto denominator squared} {\left(\frac{##2}{##3}\right)^{##1}cn^2} }
}{
if level=1{
level mark/.process={ R R w2 {auto numerator} {auto denominator squared} {\frac{##1}{##2}cn^2} }
}{
level mark=cn^2
}
}
},
},
before packing={
for tree={
l sep'=2.5em
},
},
},
}
\begin{document}
\begin{forest}
auto level markers={3}{4}{3},
[]
\end{forest}
\begin{forest}
auto level markers={2}{6}{4},
[]
\end{forest}
\end{document}
第一棵树是上面绘制的树的修正版本:
第二个随机改变传递给样式的值,根据树所代表的内容,这可能是也可能没有意义的: