\documentclass[11pt]{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\node[shape=circle,draw=black] (A) at (6,1.2) [label=] {};
\node[shape=circle,draw=black] (B) at (4.5,2) [label=] {};
\node[shape=circle,draw=black] (C) at (4.5,0.5)[label=] {};
\node[shape=circle,draw=black] (D) at (3,1.2) [label=] {}
\path [-](B) edge node[above = 0.1]{$a$} (D);
\path [-](A) edge node[above = 0.1]{$c$} (B);
\path [-](A) edge node[below = 0.1]{$d$} (C);
\path [-](C) edge node[below = 0.1]{$b$} (D);
\path [-](B) edge node[right]{$e$} (C);
\end{tikzpicture}
\end{document}
答案1
以tikz-cd
作为一个大矩阵:
\documentclass[11pt, margin=3mm]{standalone}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzcd}[cells={nodes={circle, draw=gray,
minimum size=1ex, inner sep=0pt, anchor=center}},
every arrow/.append style={dash},
]
&& & ~ \ar[dr,"c"]
\ar[dd,"e"] & &&& \\
&&~ \ar[ur,"a"]
& & ~ \ar[dl,"d", ""{name=C,right}]
&&& \\
&& & ~ \ar[ul,"b", ""{name=A,left}]
\ar[from=A, to=B, shorten <=3mm, "P_e" ']
&& & ~ \ar[dr,"c"] & \\
~ \ar[r,bend left,"a"]
& |[""{name=B}]| ~
\ar[r,bend left,"c"]
\ar[l,bend left,"b"]
&~ \ar[l,bend left,"d"]
& && ~ \ar[ur,"a", ""{name=D,left}]
\ar[from=C, to=D,
shorten <=3mm, shorten >=3mm, "1-P_e" ']
& & ~ \ar[dl,"d"] \\
& & & && & ~ \ar[ul,"b"] &
\end{tikzcd}
\end{document}
答案2
您可以使用\ppic
宏这里相对定位子图片。
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{positioning,quotes}
\newcommand{\ppic}[2][]{
\node[matrix,#1]{\pic{#2};\\ };}
\begin{document}
\begin{tikzpicture}[circ/.style={circle,inner sep=2pt,draw},auto,
pics/whatever/.style={code={#1}}]
\ppic[local bounding box=root] {whatever={\draw[pos=0.5]
foreach \X [count=\Y] in {a,b,d,c}
{(90+90*\Y:1) node[circ] (\X) {}}
foreach \X [remember=\X as \LastX (initially c)] in {a,b,d,c}
{(\X) to["$\X$"] (\LastX)}
(b) to["$e$"] (c);}}
\ppic[local bounding box=L,below left=of root]{whatever={\draw[pos=0.5] (-1,0) node[circ] (l){}
(0,0) node[circ] (m) {} (1,0) node[circ] (r){}
(l) to[out=90,in=135,"$a$"] (m)
to[out=-135,in=-90,"$b$"] (l)
(m) to[out=45,in=90,"$c$"] (r)
to[out=-90,in=-45,"$d$"] (m); }}
\ppic[local bounding box=R,below right=of root]{whatever={\draw[pos=0.5]
foreach \X [count=\Y] in {a,b,d,c}
{(90+90*\Y:1) node[circ] (\X) {}}
foreach \X [remember=\X as \LastX (initially c)] in {a,b,d,c}
{(\X) to["$\X$"] (\LastX)};}}
\draw (root) to["$P_e$"'](L) (root) to["$1-P_e$"](R);
\end{tikzpicture}
\end{document}
对于足够小的树,这种方法很好。
如果您的树较大,手动定位将变得非常麻烦,您可能需要切换到forest
。
\documentclass{article}
\usepackage[edges]{forest}
\usetikzlibrary{quotes}
\newsavebox\picA
\newsavebox\picB
\newsavebox\picC
\begin{document}
\savebox\picA{\begin{tikzpicture}[circ/.style={circle,inner sep=2pt,draw},auto]
\draw[pos=0.5] foreach \X [count=\Y] in {a,b,c,d}
{(90+90*\Y:1) node[circ] (\X) {}}
foreach \X [remember=\X as \LastX (initially d)] in {a,b,c,d}
{(\X) to["$\X$"] (\LastX)}
(b) to["$e$"] (d);
\end{tikzpicture}}%
\savebox\picB{\begin{tikzpicture}[circ/.style={circle,inner sep=2pt,draw},auto]
\draw[pos=0.5] (-1,0) node[circ] (l){}
(0,0) node[circ] (m) {} (1,0) node[circ] (r){}
(l) to[out=90,in=135,"$a$"] (m)
to[out=-135,in=-90,"$b$"] (l)
(m) to[out=45,in=90,"$c$"] (r)
to[out=-90,in=-45,"$d$"] (m);
\end{tikzpicture}}%
\savebox\picC{\begin{tikzpicture}[circ/.style={circle,inner sep=2pt,draw},auto]
\draw[pos=0.5] foreach \X [count=\Y] in {a,b,c,d}
{(90+90*\Y:1) node[circ] (\X) {}}
foreach \X [remember=\X as \LastX (initially d)] in {a,b,c,d}
{(\X) to["$\X$"] (\LastX)};
\end{tikzpicture}}%
\begin{forest}
for tree={s sep+=2em,l sep+=1em,anchor=center}
[\usebox\picA
[\usebox\picB,edge label={node[midway,left]{$P_e$}}]
[\usebox\picC,edge label={node[midway,right]{$1-P_e$}}]
]
\end{forest}
\end{document}