如何在箭头的开头放置标签?

如何在箭头的开头放置标签?

我使用以下代码来显示下面的图表:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{positioning}

\tikzset{
   actor/.style={
     rectangle,
     draw=black,
     minimum size=10mm
   },
   arrow/.style={
     -latex,
     thick,
     shorten <=2pt,
     shorten >=2pt
   }
}

\begin{document}

    \begin{tikzpicture}[node distance=0mm and 20mm]
      \node (A)[actor] {A};
      \node (B)[actor,right=of A] {B};
      \node (C)[actor, right=of B] {C};
      \node (Fake) [right=of C] {};
      \node [right=of A,  xshift=-20mm, yshift=2mm] {1};
      \draw[arrow] (A) -- node[font=\small, auto] {$200/s$} (B);
      \draw[arrow] (B) -- node[font=\small, auto] {$100/s$} (C);
      \draw[arrow] (C) -- node[font=\small, auto] {$100/s$} (Fake);
    \end{tikzpicture}

\end{document}

结果

正如你所看到的,我用了一个肮脏的黑客将“1”值放在 A 和 B 之间箭头的起点上方。我该如何正确地做到这一点?

答案1

您可以使用 x 介于 0 和 1 之间的值来实现此目的node[pos=x],该值表示路径起点和终点之间的节点位置。您可以选择更高的值,甚至将其移动到该值之前/之后。

如果您希望它更高/更低,您可以给出上面的参数(above = x,,,... 也)。leftright

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\tikzset{
   actor/.style={
     rectangle,
     draw=black,
     minimum size=10mm
   },
   arrow/.style={
     -latex,
     thick,
     shorten <=2pt,
     shorten >=2pt
   }
}

\begin{tikzpicture}[node distance=3cm, thick]

\node[actor] (1) {A};
\node[actor] (2) [right of=1] {B};
\node[actor] (3) [right of=2] {C};
\node[] (4) [right of=3] {};

\draw[arrow]    (1) to node[pos=0.1,above]{1}node [font=\small,above] {$200/s$} (2);
\draw[arrow]    (2) to node [font=\small,above] {$100/s$} (3);
\draw[arrow]    (3) to node [font=\small,above] {$100/s$} (4);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容