我想在树枝下面放置花括号。下面的示例正是这样做的,但标签“Toss 1”和“Toss 2”的放置位置的硬编码值并不理想,因为它们需要在纸张/文本大小发生变化等时进行调整。有没有办法使用相对坐标/节点以更稳健的方式放置两个标签,而不需要硬编码长度(或者至少只有一个这样的长度,即从两个标签到树的垂直距离)?
\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{positioning, decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}[line width = 0.25mm, grow=right, sloped,
level 1/.style={level distance=1.8cm, sibling distance=2.4cm},
level 2/.style={level distance=2.1cm, sibling distance=1.2cm},
edge from parent/.style={draw, shorten >=1.4mm, shorten <=1.4mm},
mynodestyle/.style={inner sep=1mm},
myleafstyle/.style={inner sep=0mm}
]
\node[mynodestyle] (root) {Coin toss}
child {
node[mynodestyle] (toss1) {$T$}
child {
node[myleafstyle, coordinate, label={[xshift=-1mm]right: $(T,T)$}] (toss2) {}
edge from parent
node[above] {\tiny $1/2$}
}
child {
node[myleafstyle, coordinate, label={[xshift=-1mm]right: $(T,H)$}] {}
edge from parent
node[above] {\tiny $1/2$}
}
edge from parent
node[above] {\tiny $1/2$}
}
child {
node[mynodestyle] {$H$}
child {
node[myleafstyle, coordinate, label={[xshift=-1mm]right: $(H,T)$}] {}
edge from parent
node[above] {\tiny $1/2$}
}
child {
node[myleafstyle, coordinate, label={[xshift=-1mm]right: $(H,H)$}] {}
edge from parent
node[above] {\tiny $1/2$}
}
edge from parent
node[above] {\tiny $1/2$}
};
% Labels. How to place the curly braces below the branches labeled with 1/2
% in terms of nodes/relative coordinates instead of hard-coded lengths?
% Label "Toss 2"
\node[coordinate, below right = 6.2mm and 1mm of toss1] (A2) {};% hard-coded
\node[coordinate, below left = 2.6mm and 1mm of toss2] (E2) {};% hard-coded
\draw[decorate, decoration = {brace, mirror, amplitude = 2mm}] (A2)--node[below = 2mm, anchor = north] {Toss 2} (E2);
% Label "Toss 1"
\node[coordinate, below right = 18.2mm and -4mm of root] (A1) {};% hard-coded
\node[coordinate, below left = 6.2mm and 1mm of toss1] (E1) {};% hard-coded
\draw[decorate, decoration = {brace, mirror, amplitude = 2mm}] (A1)--node[below = 2mm, anchor = north] {Toss 1} (E1);
\end{tikzpicture}%
\end{document}
答案1
像这样?
与您的 MWE 相比,我添加了calligraphy
库(用于更花哨的括号)并重命名了节点。对于括号锚点的位置,使用垂直坐标。
\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,
calligraphy} % new, had to be load after decorations.pathreplacing}
\begin{document}
\begin{tikzpicture}[
BC/.style = {decorate,
decoration={calligraphic brace, amplitude=6pt,
raise=1mm,
mirror},% for mirroring of brace
ultra thick,
pen colour=red % select color according to your taste
},
%
grow=right,
level distance=2.2cm,
level 1/.style = {sibling distance=2.4cm},
level 2/.style = {sibling distance=1.2cm},
edge from parent/.style={draw,thick, shorten >=1.4mm, shorten <=1.4mm},
N/.style = {inner sep=1pt},
L/.style = {font=\tiny, above, sloped}
]
\node[N] (A) {Coin toss}
child {node[N] (B) {$T$}
child {node[N] (C) {$(T,T)$}
edge from parent node[L] {$1/2$}
}
child {node[N] {$(T,H)$}
edge from parent node[L] {$1/2$}
}
edge from parent node[L] {$1/2$}
}
child {node[N] {$H$}
child {node[N] {$(H,T)$}
edge from parent node[L] {$1/2$}
}
child {node[N] {$(H,H)$}
edge from parent node[L] {$1/2$}
}
edge from parent node[L] {$1/2$}
};
%
\draw[BC] (A.330 |- C.south) -- node[below=8pt] {Toss 1} (B.west |- C.south);
\draw[BC] (B.east |- C.south) -- node[below=8pt] {Toss 2} (C.south west);
\end{tikzpicture}%
\end{document}
附录:
使用以下forest
包可以使树的代码更加简洁、清晰:
\documentclass{scrartcl}
\usepackage{forest}
\usetikzlibrary{decorations.pathreplacing,
calligraphy} %had to be loaded after decorations.pathreplacing
\tikzset{
BC/.style = {% Brace Calligraphic
decorate,
decoration={calligraphic brace, amplitude=6pt,
raise=2pt,
mirror},% for mirroring of brace
ultra thick,
pen colour=red % select color according to your taste
},
ELS/.style = {%Edge Label Style
font=\scriptsize, sloped, pos=0.6,
inner sep=2pt, anchor=#1}
}
\begin{document}
\begin{forest}
for tree = {
% tree
grow = east,
edge = {thick},
l sep = 12mm,
s sep = 4mm,
L/.style = {if n=1{edge label={node[ELS=north]{$1/2$}}}
{edge label={node[ELS=south]{$1/2$}}}} % since all labels are the same
}
[Coin toss, name=A
[$T$, name=B, L
[{$(T,T)$}, name=C, L]
[{$(T,H)$},L]
]
[$H$, L
[{$(H,T)$},L]
[{$(H,H)$},L]
]
]
%
\draw[BC] (A.330 |- C.south) -- node[below=8pt] {Toss 1} (B.west |- C.south);
\draw[BC] (B.east |- C.south) -- node[below=8pt] {Toss 2} (C.south west);
\end{forest}%
\end{document}
编辑: 如果您希望所有边缘标签都位于边缘上方,则替换
L/.style = {if n=1{edge label={node[ELS=north]{$1/2$}}}
{edge label={node[ELS=south]{$1/2$}}}}
和
L/.style = {edge label={node[font=\scriptsize, sloped, pos=0.6,
inner sep=2pt, anchor=south]{$1/2$}}}
并删除了 中的 ELS 样式定义\tikzset
。
我更喜欢上图所示的解决方案。