以下代码
\documentclass[border=10pt,varwidth]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc, shapes, backgrounds}
\usepackage{amsmath, amssymb}
\begin{figure*}
\centering
\tikzset{
head/.style = {label = center:\textsf{\Large H}},
tail/.style = {label = center:\textsf{\Large T}}
}
\begin{tikzpicture}[
scale = 1.5, transform shape, thick,
every node/.style = {draw, circle, minimum size = 10mm},
grow = down, % alignment of characters
level 1/.style = {sibling distance=4cm},
level 2/.style = {sibling distance=3cm},
level 3/.style = {sibling distance=2cm},
level distance = 1.25cm
]
\node[circle, draw, line width = 1pt,
minimum size = 10mm, inner sep = 0mm, font = \sffamily\large] (Start)
{root}
child { node [head] (A) {}
child { node [head] (B) {}}
child { node [tail] (C) {}}
}
child { node [tail] (D) {}
child { node [head] (E) {}}
child { node [tail] (F) {}}
};
% Labels
\begin{scope}[nodes = {draw = none}]
\path (Start) -- (A) node [near start, left] {$0.5$};
\path (A) -- (B) node [near start, left] {$0.5$};
\path (A) -- (C) node [near start, right] {$0.5$};
\path (Start) -- (D) node [near start, right] {$0.5$};
\path (D) -- (E) node [near start, left] {$0.5$};
\path (D) -- (F) node [near start, right] {$0.5$};
\end{scope}
\end{tikzpicture}
\end{figure*}
产生以下输出
如何修改代码以获得某些节点的分支的不同方向?例如,如下所示:
此外,如果我想在不同的边上写不同的标签怎么办?例如在分支 TT 上写 0.6 而不是 0.5?
答案1
与森林相对简单:-):
第一张图片:
\documentclass[border=10pt]{standalone} \usepackage[edges]{forest} \begin{document} \begin{forest} for tree={ circle, draw, minimum size=8mm, font=\sffamily\Large, inner sep=1pt, l sep = 18pt, s sep = 6pt, % labels on edges EL/.style 2 args={ edge label/.wrap value={node[midway, font=\sffamily\footnotesize, #1] {#2}}, }, }, % end for tree [root [H,edge label=left [H,EL={left}{0.1} [H,EL={left}{0.3}] [T,EL={right}{0.7}] ] [T,EL={right}{0.9}, %calign=first [H,EL={left}{0.6}] [T,EL={right}{0.4}] ] ] \end{forest} \end{document}
第二张图片:
\documentclass[border=10pt]{standalone} \usepackage[edges]{forest} \begin{document} \begin{forest} for tree={ circle, draw, minimum size=8mm, font=\sffamily\Large, inner sep=1pt, l sep = 18pt, s sep = 6pt, % labels on edges EL/.style 2 args={ edge label/.wrap value={node[midway, font=\sffamily\footnotesize, #1] {#2}}, }, }, % end for tree [H,EL={left}{0.1} [H,EL={left}{0.5}] [T,EL={right}{0.5}] ] [T,EL={right}{0.9}, calign=first % change branch direction [H,EL={left}{0.5}] [T,EL={right}{0.5}] ] ] \end{forest} \end{document}
编辑: 在这两种情况下,现在都认为每个分支标签可以具有不同的值。