Tikz - 如何向由“图形”创建的现有路径添加标签

Tikz - 如何向由“图形”创建的现有路径添加标签

我正在尝试向路径添加一些标签,但我无法计算出已使用graphs和创建的现有路径的坐标matrix。我找到了一种解决方法,即对坐标进行硬编码并添加新标签。下面的代码是 MWE,我后来添加的标签位于注释“新标签”之后(见下面的代码)。

问题是,如果我改变角度out=XXXin=XXX和,这就会中断looseness=XXX。我一直在寻找是否可以从现有节点建立坐标。

谢谢

这是我的 MWE:

\documentclass{standalone}
\usepackage{tikz}

\usetikzlibrary{%
    arrows.meta,
    calc,
    graphs,
    matrix,
    positioning,
}

\begin{document}

\begin{tikzpicture}
  \matrix (bn) [matrix of nodes, column sep=14mm, row sep=1mm, nodes={draw, circle, fill=white, very thick, minimum width=8mm}]
  {
    1 & 2 & 3 & 4 & 5 & 6 & 7 & 8\\
  };
  \graph[edges={>=Latex, semithick, color=black, out=30, in=155}] {
    (bn-1-1) -> (bn-1-2) -> (bn-1-3) -> (bn-1-4) -> (bn-1-5) -> (bn-1-6) -> (bn-1-7) -> (bn-1-8)
  };
  \graph[edges={>=Latex, semithick, color=black, out=210, in=335}] {
    (bn-1-8) -> (bn-1-7) -> (bn-1-6) -> (bn-1-5) -> (bn-1-4) -> (bn-1-3) -> (bn-1-2) -> (bn-1-1)
  };
  \foreach \x in {1,...,8}
    \path[->,>=Latex]
      (bn-1-\x) edge [out=110,in=70,looseness=12] node[above] {} (bn-1-\x);

  % New labels
  \node (pnq) at ([yshift=7.5mm]$(bn-1-3)!0.5!(bn-1-4)$) {$p(1-q)$};
  \node (npq) at ([yshift=-7.5mm]$(bn-1-3)!0.5!(bn-1-2)$) {$q(1-p)$};
  \node (npq) at ([yshift=16.5mm]$(bn-1-3)!0.5!(bn-1-3)$) {$pq$};
\end{tikzpicture}

\end{document}

图片: 在此处输入图片描述

答案1

\graph[edges={>=Latex, semithick, color=black, out=30, in=155}] {
    (bn-1-1) ->[ pos=0.48,edge label=\scriptsize A] (bn-1-2) -> (bn-1-3) -> (bn-1-4) -> (bn-1-5) -> (bn-1-6) -> (bn-1-7) -> (bn-1-8)
  };
  \graph[edges={>=Latex, semithick, color=black, out=210, in=335}] {
    (bn-1-8) -> (bn-1-7) -> (bn-1-6) -> (bn-1-5) -> (bn-1-4) -> (bn-1-3) -> (bn-1-2) ->[pos=0.48,edge label=\scriptsize B] (bn-1-1)
  };

以及循环

  \foreach \x in {1,...,8}
    \path[->,>=Latex]
      (bn-1-\x) edge [out=110,in=70,looseness=12] node[yshift=-5pt,  above, label=\scriptsize bn-1-\x] {} (bn-1-\x);

平均能量损失

\documentclass{standalone}
\usepackage{tikz}

\usetikzlibrary{%
    arrows.meta,
    calc,
    graphs,
    matrix,
    positioning,
}

\begin{document}

\begin{tikzpicture}
  \matrix (bn) [matrix of nodes, column sep=14mm, row sep=1mm, nodes={draw, circle, fill=white, very thick, minimum width=8mm}]
  {
    1 & 2 & 3 & 4 & 5 & 6 & 7 & 8\\
  };
  \graph[edges={>=Latex, semithick, color=black, out=30, in=155}] {
    (bn-1-1) ->[ pos=0.48,edge label=\scriptsize A] (bn-1-2) -> (bn-1-3) -> (bn-1-4) -> (bn-1-5) -> (bn-1-6) -> (bn-1-7) -> (bn-1-8)
  };
  \graph[edges={>=Latex, semithick, color=black, out=210, in=335}] {
    (bn-1-8) -> (bn-1-7) -> (bn-1-6) -> (bn-1-5) -> (bn-1-4) -> (bn-1-3) -> (bn-1-2) ->[pos=0.48,edge label=\scriptsize B] (bn-1-1)
  };
  \foreach \x in {1,...,8}
    \path[->,>=Latex]
      (bn-1-\x) edge [out=110,in=70,looseness=12] node[yshift=-5pt,  above, label=\scriptsize bn-1-\x] {} (bn-1-\x);

  % New labels
%  \node (pnq) at ([yshift=7.5mm]$(bn-1-3)!0.5!(bn-1-4)$) {$p(1-q)$};
%  \node (npq) at ([yshift=-7.5mm]$(bn-1-3)!0.5!(bn-1-2)$) {$q(1-p)$};
%  \node (npq) at ([yshift=16.5mm]$(bn-1-3)!0.5!(bn-1-3)$) {$pq$};


\end{tikzpicture}

\end{document}

在此处输入图片描述

编辑将标签添加到单循环

平均能量损失

    \documentclass{standalone}
\usepackage{tikz}

\usetikzlibrary{%
    arrows.meta,
    calc,
    graphs,
    matrix,
    positioning,
}

\begin{document}

\begin{tikzpicture}
  \matrix (bn) [matrix of nodes, column sep=14mm, row sep=1mm, nodes={draw, circle, fill=white, very thick, minimum width=8mm}]
  {
    1 & 2 & 3 & 4 & 5 & 6 & 7 & 8\\
  };
  \graph[edges={>=Latex, semithick, color=black, out=30, in=155}] {
    (bn-1-1) ->[ pos=0.48,edge label=\scriptsize A] (bn-1-2) -> (bn-1-3) -> (bn-1-4) -> (bn-1-5) -> (bn-1-6) -> (bn-1-7) -> (bn-1-8)
  };
  \graph[edges={>=Latex, semithick, color=black, out=210, in=335}] {
    (bn-1-8) -> (bn-1-7) -> (bn-1-6) -> (bn-1-5) -> (bn-1-4) -> (bn-1-3) -> (bn-1-2) ->[pos=0.48,edge label=\scriptsize B] (bn-1-1)
  };
  \foreach \x in {1,2,4,5,6,7,8}
    \path[->,>=Latex]
      (bn-1-\x) edge [out=110,in=70,looseness=12] node[] {} (bn-1-\x);


\foreach \x in {3}
\path[->,>=Latex]
(bn-1-\x) edge [out=110,in=70,looseness=12] node[yshift=-5pt,  above, label=\scriptsize pq] {} (bn-1-\x);

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容