![标记 Tikz 有向边](https://linux22.com/image/333465/%E6%A0%87%E8%AE%B0%20Tikz%20%E6%9C%89%E5%90%91%E8%BE%B9.png)
我正在努力标记 Tikz 有向图中的边。我尝试在序言中使用 tikz 库引号,但没有成功。
我目前有代码
\begin{tikzpicture}
\tikzset{vertex/.style = {shape=circle,draw,minimum size=1.5em}}
\tikzset{edge/.style = {->,> = latex'}}
% vertices
\node[vertex] (1) at (0,0) {$1$};
\node[vertex] (2) at (2,0) {$2$};
\node[vertex] (3) at (0,-2) {$3$};
\node[vertex] (4) at (2,-2) {$4$};
\node[vertex] (5) at (-2, -2) {$5$};
%edges
\draw[edge] ["1/3"] (1) to (2);
\draw[edge] (1.260) to (3.100);
\draw[edge] (1.325) to (4.125);
\draw[edge] (2) to (3);
\draw[edge] (2) to (4);
\draw[edge] (3.80) to (1.280);
\draw[edge] (4) to (3);
\draw[edge] (4.145) to (1.305);
\draw[edge] (3.170) to (5.10);
\draw[edge] (5.350) to (3.190);
\end{tikzpicture}
任何帮助都值得感激!
答案1
我对您的代码做了一些更改,以便您可以沿路径使用节点。
midway
将节点置于中间。pos=
表示更精确的位置,从 0 (at start
) 到 1 (at end
)。midway
相当于pos=.5
。sloped
将节点与其所在路径的角度对齐。我应用的旋转是使其与路径正交。right
,,,和above
是节点相对于路径的位置。left
below
输出
代码
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\tikzset{
vertex/.style={circle,draw,minimum size=1.5em},
edge/.style={->,> = latex'}
}
\begin{document}
\begin{tikzpicture}
% vertices
\node[vertex] (1) at (0,0) {$1$};
\node[vertex] (2) at (2,0) {$2$};
\node[vertex] (3) at (0,-2) {$3$};
\node[vertex] (4) at (2,-2) {$4$};
\node[vertex] (5) at (-2,-2) {$5$};
%edges
\draw[edge] (1) -- (2) node[midway, above] {$\frac{1}{3}$};
\draw[edge] (1.260) -- (3.100);
\draw[edge] (1.325) -- (4.125) node[pos=.3, right, sloped, rotate=90] {$\frac{1}{3}$};
\draw[edge] (2) -- (3);
\draw[edge] (2) -- (4) node[midway, right] {$\frac{1}{2}$};
\draw[edge] (3.80) -- (1.280);
\draw[edge] (4) -- (3);
\draw[edge] (4.145) -- (1.305);
\draw[edge] (3.170) -- (5.10);
\draw[edge] (5.350) -- (3.190);
\end{tikzpicture}
\end{document}
答案2
我认为你把quotes
字符串放错了位置,然后立即添加它就to
可以了。只需对位置和旋转进行一些调整即可。
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{quotes}
\begin{document}
\begin{tikzpicture}
\tikzset{vertex/.style = {shape=circle,draw,minimum size=1.5em}}
\tikzset{edge/.style = {->,> = latex}}
% vertices
\node[vertex] (1) at (0,0) {$1$};
\node[vertex] (2) at (2,0) {$2$};
\node[vertex] (3) at (0,-2) {$3$};
\node[vertex] (4) at (2,-2) {$4$};
\node[vertex] (5) at (-2, -2) {$5$};
%edges
\draw[edge] (1) to["$\frac{1}{3}$"] (2);
\draw[edge] (1.260) to["$\frac{1}{2}$"] (3.100);
\draw[edge] (1.325) to["$\frac{1}{3}$" {sloped,rotate=90,anchor=west,pos=0.3}] (4.125);
\draw[edge] (2) to["$\frac{1}{3}$" {sloped,rotate=-90,anchor=east,pos=0.7}] (3);
\draw[edge] (2) to["$\frac{1}{2}$"] (4);
\draw[edge] (3.80) to["$\frac{1}{3}$"] (1.280);
\draw[edge] (4) to["$\frac{1}{2}$"] (3);
\draw[edge] (4.145) to["$\frac{1}{2}$" {sloped,rotate=90,anchor=east,pos=0.3}] (1.305);
\draw[edge] (3.170) to["$1$"] (5.10);
\draw[edge] (5.350) to["$\frac{1}{2}$"] (3.190);
\end{tikzpicture}
\end{document}