如何在箭头尾部、箭头中间和箭头弯曲处添加文字

如何在箭头尾部、箭头中间和箭头弯曲处添加文字

请考虑以下代码:

\documentclass[a4,12pt]{article}

\usepackage{tikz}
\usetikzlibrary{automata,arrows,positioning,calc}
\usepgflibrary{arrows.meta}

\begin{document}

\begin{tikzpicture}
\tikzstyle{vertex} = [circle, draw=black]
\tikzstyle{edge} = [->, thick]

\node[vertex](i) at (3,4) {Facility $i$};
\node[vertex](j) at (8,4) {Facility $j$};

\path (i) edge [loop left] (i);
\path (j) edge [loop right] (j);

\draw[black,->,shifted path=from i to j by 10pt];
\draw[black,->,shifted path=from j to i by 10pt];

\draw [-Stealth](3,7) -- (3,5);
\draw [-Stealth](8,7) -- (8,5);

\draw [-Stealth](3,3) -- (3,1);
\draw [-Stealth](8,3) -- (8,1);

\draw[step=1cm, gray, very thin] (0,0) grid (13,9);
\foreach \x in {0,1,2,3,4,5,6,7,8,9,10,11,12,13}
   \draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\x$};
\foreach \y in {0,1,2,3,4,5,6,7,8,9}
    \draw (1pt,\y cm) -- (-1pt,\y cm) node[anchor=east] {$\y$};

\end{tikzpicture}

\end{document}

得出的结果为: 在此处输入图片描述

我只是将网格线用作参考,稍后会将其删除。但无论如何,这就是我想要实现的:

在此处输入图片描述

我该如何实现这一点?是否可以编辑我的代码来实现这一点,还是我需要重新考虑我的整个方法?

答案1

编辑:我忘记了垂直箭头...现在添加了。

两个例子:

  • 对于箭头上的标签使用quotes库:他们使用它的语法,例如:edge["$x_y$"]
  • 顶点之间有弯曲箭头
  • 顶点之间的直箭头,其中画布向上移动(对于顶部箭头)和向下移动(底部箭头):

在此处输入图片描述

\documentclass[tikz, 12pt, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta, automata,
                positioning,
                quotes}

\begin{document}
    \begin{tikzpicture}[
    node distance = 12mm and 24mm,
    vertex/.style = {circle, draw, text width=4.4em, align=center},
every edge/.style = {-Straight Barb, draw, semithick}
                        ]
% vertex
\node (i) [vertex]              {Facility $i$};
\node (j) [vertex, right=of i]  {Facility $j$};
%
\draw   (i) edge [loop  left,"$p_{ii}$"] (i)
        (i) edge [bend left,"$p_{ij}$"]  (j)
        (j) edge [bend left,"$p_{ji}$"]  (i)
        (j) edge [loop right,"$p_{jj}$"] (j);
%
\node (ri)  [above=of i] {$r_j$};
\node (out) [below=of i] {$\mathit{out}$};
    \draw   (ri) edge (i)
            (i)  edge (out);
\node (ri)  [above=of j] {$r_j$};
\node (out) [below=of j] {$\mathit{out}$};
    \draw   (ri) edge (j)
            (j)  edge (out);
    \end{tikzpicture}
%
    \begin{tikzpicture}[
    node distance = 12mm and 24mm,
    vertex/.style = {circle, draw, text width=4.4em, align=center},
every edge/.style = {-Straight Barb, draw, semithick}
                        ]
% vertex
\node (i) [vertex]              {Facility $i$};
\node (j) [vertex, right=of i]  {Facility $j$};
%
\draw   (i) edge [loop  left,"$p_{ii}$"] (i)
        (j) edge [loop right,"$p_{jj}$"] (j);
\draw[transform canvas={yshift=+1ex}]   (i) edge ["$p_{ij}$"]  (j);
\draw[transform canvas={yshift=-1ex}]   (j) edge ["$p_{ji}$"]  (i);
%
\node (ri)  [above=of i] {$r_j$};
\node (out) [below=of i] {$\mathit{out}$};
    \draw   (ri) edge (i)
            (i)  edge (out);
\node (ri)  [above=of j] {$r_j$};
\node (out) [below=of j] {$\mathit{out}$};
    \draw   (ri) edge (j)
            (j)  edge (out);
    \end{tikzpicture}
\end{document}

答案2

替换路径命令以获取自循环两侧的标签

\path (i) edge [loop left]node[](){a} (i);
\path (j) edge [loop right]node[](){b} (j);

在此处输入图片描述

相关内容