例如(由另一个线程使用):
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}[level distance=1.5cm, grow=down,
every node/.style={draw, circle, thin},
edge from parent/.style={-latex, thick, draw}
]
\node (P) {P}
child {node (Q) {Q}
child {node (T) {T}}
child {node (U) {U}}
}
child {node (R) {R}}
child {node (S) {S}};
\path (P) -- coordinate[midway] (PQ) (Q);
\path (P) -- coordinate[midway] (PR) (R);
\draw (PQ) to[bend right=22] (PR);
\end{tikzpicture}
\end{document}
我怎样才能添加标签,例如,添加M
到行PQ
?
答案1
使用另一个节点放置标签很容易。问题是,使用draw
toevery node
样式,您的所有节点都绘制为圆圈,我认为您不希望带有标签的节点出现这种情况M
。您可以scope
在内部使用tikzpicture
to 分隔样式,就像我在这里所做的那样。
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{trees}
\begin{document}
\begin{tikzpicture}
\begin{scope}[level distance=1.5cm, grow=down,
every node/.style={draw, circle, thin},
edge from parent/.style={-latex, thick, draw}
]
\node (P) {P}
child {node (Q) {Q}
child {node (T) {T}}
child {node (U) {U}}
}
child {node (R) {R}}
child {node (S) {S}};
\path (P) -- coordinate[midway] (PQ) (Q);
\path (P) -- coordinate[midway] (PR) (R);
\draw (PQ) to[bend right=22] (PR);
\end{scope}
\node [above left] at (PQ) {M}; % Draws the node labeled M
\end{tikzpicture}
\end{document}
答案2
自pgf/TikZ 3.0 发布使用语法标记边更容易quotes
。如果您已经有了一条边,那么只需将“文本”添加到边选项即可。在这里,让我们在绘制树后进行标记:
\path (P) edge["M"'] (Q);
您的弯曲线看起来像一个角度。使用新angles
库,您可以添加
pic[draw,angle radius=0.8cm] {angle=Q--P--R}
到上面的路径,为此。现在您甚至不需要定义坐标 (PQ) 和 (PR)。此外,弯曲线更像是半径,而不仅仅是中点之间的连接。
完整代码,修改了hpesoj626的建议:
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{trees,quotes,angles}
\begin{document}
\begin{tikzpicture}
\begin{scope}[level distance=1.5cm, grow=down,
every node/.style={draw, circle, thin},
edge from parent/.style={-latex, thick, draw}
]
\node (P) {P}
child {node (Q) {Q}
child {node (T) {T}}
child {node (U) {U}}
}
child {node (R) {R}}
child {node (S) {S}};
\end{scope}
\path (P) edge["M"'] (Q) pic[draw,angle radius=0.8cm] {angle=Q--P--R};
\end{tikzpicture}
\end{document}