我必須畫有根(向上)的樹(有葉子)。我找到了包裹森林很有用,儘管我採用的解決方法很可能在任何意義上都不是最佳的。這種樹的範例如下。
\documentclass[a4paper,10pt]{article}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{forest}
%opening
\title{}
\author{}
\date{}
\begin{document}
\begin{center}
\begin{forest}
for tree={grow'=north, circle, draw, minimum width=0.7cm,anchor=center,fit=rectangle,
edge={>=stealth,-,shorten >=1pt,shorten <=1pt,line width=0.5pt},
scale=0.75,font=\sffamily\small\bfseries}
%
[, node options'
[$l$, edge label={node[midway,right,font=\scriptsize]{a}}
[$l'$, edge label={node[midway,left,font=\scriptsize]{b}}
[, node options', edge label={node[midway,left,font=\scriptsize]{e}}]
[, node options', no edge
]
[, node options', edge label={node[midway,right,font=\scriptsize]{f}}]
]
[, node options', no edge
]
[, node options', edge label={node[midway,left,font=\scriptsize]{c}}
]
[, node options', no edge
]
[$l''$, edge label={node[midway,right,font=\scriptsize]{d}}
]
]
]
\end{forest}\end{center}
\end{document}
問題。如何繪製從一棵有根樹到另一棵樹的箭頭,正如本文 [1] 第 7 頁所做的那樣(使用 xypic)?
答案1
另一種選擇,也許可以更好地控制箭頭,以防萬一CFR的解決方案不能直接使用,而是將樹放置在 TikZ 中\node
,然後使用節點的名稱來繪製箭頭。
更安全的方法是先將樹木裝箱,然後在tikzpicture
;內使用這些箱子。在下面的範例中,我使用同一棵樹三次,但是可以將相同的想法應用於不同的樹,為每棵樹使用不同的盒子。
代碼:
\documentclass[a4paper,10pt]{article}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{forest}
\usetikzlibrary{positioning}
\newsavebox\boxa% we define a new box
% we fill the box
\savebox\boxa{%
\begin{forest}
for tree={grow'=north, circle, draw, minimum width=0.7cm,anchor=center,fit=rectangle,
edge={>=stealth,-,shorten >=1pt,shorten <=1pt,line width=0.5pt},
scale=0.75,font=\sffamily\small\bfseries}
%
[, node options'
[$l$, edge label={node[midway,right,font=\scriptsize]{a}}
[$l'$, edge label={node[midway,left,font=\scriptsize]{b}}
[, node options', edge label={node[midway,left,font=\scriptsize]{e}}]
[, node options', no edge
]
[, node options', edge label={node[midway,right,font=\scriptsize]{f}}]
]
[, node options', no edge
]
[, node options', edge label={node[midway,left,font=\scriptsize]{c}}
]
[, node options', no edge
]
[$l''$, edge label={node[midway,right,font=\scriptsize]{d}}
]
]
]
\end{forest}%
}
\begin{document}
\begin{center}
\begin{tikzpicture}[node distance=0.5cm and 0.5cm]
\node[inner sep=0pt] (uc)
{\usebox\boxa};
\node[inner sep=0pt,below left=of uc] (ll)
{\usebox\boxa};
\node[inner sep=0pt,below right=of uc] (lr)
{\usebox\boxa};
\begin{scope}[shorten >=10pt,shorten <=10pt]
\draw[->]
(ll.north) -- node[auto] {$\partial_{v}$} (uc);
\draw[->]
(lr.north) -- node[swap,auto] {$\partial_{w}$} (uc);
\end{scope}
\end{tikzpicture}
\end{center}
\end{document}
答案2
一種方法是將樹繪製為具有虛擬根節點的單棵樹:
\documentclass[tikz,border=5pt]{standalone}
\usepackage[utf8]{inputenc}% just for this example as my editor uses this encoding
\usepackage[T1]{fontenc}
\usepackage{forest}
\begin{document}
\begin{forest}
for tree={grow'=north, circle, draw, minimum width=0.7cm,anchor=center,fit=rectangle,
edge={>=stealth,-,shorten >=1pt,shorten <=1pt,line width=0.5pt},
scale=0.75,font=\sffamily\small\bfseries}
%
[,phantom, l sep=0pt, s sep+=20pt
[, node options'
[$l$, edge label={node[midway,right,font=\scriptsize]{a}}
[$l'$, edge label={node[midway,left,font=\scriptsize]{b}}
[, node options', edge label={node[midway,left,font=\scriptsize]{e}}]
[, node options', no edge
]
[, node options', edge label={node[midway,right,font=\scriptsize]{f}}]
]
[, node options', no edge
]
[, node options', edge label={node[midway,left,font=\scriptsize]{c}}
]
[, node options', no edge
]
[$l''$, edge label={node[midway,right,font=\scriptsize]{d}}, name=node1
]
]
]
[, node options'
[$l$, edge label={node[midway,right,font=\scriptsize]{a}}
[$l'$, edge label={node[midway,left,font=\scriptsize]{b}}, name=node2
[, node options', edge label={node[midway,left,font=\scriptsize]{e}}]
[, node options', no edge
]
[, node options', edge label={node[midway,right,font=\scriptsize]{f}}]
]
[, node options', no edge
]
[, node options', edge label={node[midway,left,font=\scriptsize]{c}}
]
[, node options', no edge
]
[$l''$, edge label={node[midway,right,font=\scriptsize]{d}}
]
]
]
]
\draw [->, >=stealth, shorten >=10pt, shorten <=10pt, thick] (node1.east) -- (node2.west);
\end{forest}
\end{document}