我使用下面的 Latex 代码制作了下图。有人能解释一下如何使节点和文本变大并标记弧吗,例如将标签 3 添加到连接 n1 和 n2 的弧上吗?
\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[font=\sf]
\node[draw,circle,label=below:$n_1$] (n1) at (0,0){};
\node[draw,circle,label=above:$n_2$] (n2) at (2,2){};
\node[draw,circle,label=above:$n_3$] (n3) at (2,1){};
\draw[thick,-latex] (n1) -- (n2);
\end{tikzpicture}
\end{document}
我的目标是制作一幅像这样的图像。
答案1
编辑:在第一个版本的机器人示例中,我忘记了边缘标签。现在已添加。
像这样?
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
positioning,
quotes}
\begin{document}
\begin{tikzpicture}[
node distance = 8mm and 12mm,
V/.style = {circle, draw, font=\sffamily, minimum size=2em, inner sep =2pt},
every edge/.style = {draw, -{Straight Barb[scale=0.8]}, shorten > = 1mm},
every edge quotes/.style = {auto, font=\small\sffamily, sloped}
]
\node[V] (n1) {n1};
\node[V, above right=of n1] (n2) {n2};
\node[V, at={(n1 -| n2)}] (n3) {n3};
\node[V, below right=of n1] (n4) {n4};
\node[V, right=of n2] (n5) {t};
%
\path (n1) edge ["3"] (n2)
(n1) edge (n3)
(n1) edge (n4)
(n3) edge[red, "5"] (n5);
\end{tikzpicture}
\end{document}
附录:
对于定位,您可以使用选项on grid
。在这种情况下,节点之间的距离是通过它们的中心来测量的,即它们在网格上的距离由以下公式指定node distance
:
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
positioning,
quotes}
\begin{document}
\begin{tikzpicture}[
node distance = 11mm and 22mm, on grid,
V/.style = {circle, draw, font=\sffamily, minimum size=2em, inner sep =2pt},
every edge/.style = {draw, -{Straight Barb[scale=0.8]}, shorten > = 1mm},
every edge quotes/.style = {auto, font=\small\sffamily, sloped}
]
\node[V] (n1) {n1};
\node[V, above right=of n1] (n2) {n2};
\node[V, right=of n1] (n3) {n3};
\node[V, below right=of n1] (n4) {n4};
\node[V, right=of n2] (n5) {t};
%
\path (n1) edge ["3"] (n2)
(n1) edge (n3)
(n1) edge (n4)
(n3) edge[red, "5"] (n5);
\end{tikzpicture}
\end{document}
结果与以前类似: