如何通过 TikZ 样式添加节点?

如何通过 TikZ 样式添加节点?

考虑下面的一个小例子。我想通过使用样式来自动添加沿边缘的标签节点。我尝试过简单的解决方案,即只添加这样一个必要的node语句,但它为什么不能这样工作是有道理的。我可以用另一种方式添加标签吗?

(由于期望节点放置自动化是不合理的,是否也可以“剪掉”与节点冲突的边缘部分?)

output


我希望能够说

\path[graph forward edge]
  (F) edge (E);

\path[graph back edge]
  (C) edge (B)
  (G) edge (A);

\path[graph cross edge]
  (D) edge (C)
  (G) edge (B)
  (G) edge (F);

并在graph forward edge样式中添加一个f在边缘中间,以更清楚地表示其在图中的作用。 (和 类似graph back edgegraph cross edge

完整示例

\documentclass[tikz]{standalone}
\tikzset{
  node distance = 1.5 cm,
  graph vertex/.style={
    circle,
    draw,
  },
  graph directed edge/.style={
    ->,
    >=stealth,
    thick,
  },
  graph tree edge/.style={
    graph directed edge
  },
  graph forward edge/.style={
    graph directed edge,
    loosely dotted,
    % I'd like to put the magic here...
  },
  graph back edge/.style={
    graph directed edge,
    densely dotted,
  },
  graph cross edge/.style={
    graph directed edge,
    dotted,
  },
}
\begin{document}
\begin{tikzpicture}
  \node[graph vertex] (A)                    {A};
  \node[graph vertex] (B) [      right of=A] {B};
  \node[graph vertex] (C) [below right of=B] {C};
  \node[graph vertex] (D) [below       of=C] {D};
  \node[graph vertex] (E) [below  left of=D] {E};
  \node[graph vertex] (F) [       left of=E] {F};
  \node[graph vertex] (G) [above  left of=F] {G};
  \node[graph vertex] (H) [above       of=G] {H};

  \path[graph tree edge]
    (A) edge (B)
    (B) edge (F)
    (F) edge (C)
    (F) edge (D)
    (D) edge (E)
    (A) edge (H)
    (H) edge (G);

  \path[graph forward edge]
    (F) edge node [      below] {f} (E);

  \path[graph back edge]
    (C) edge node [above right] {b} (B)
    (G) edge node [      right] {b} (A);

  \path[graph cross edge]
    (D) edge node [      right] {c} (C)
    (G) edge node [below right] {c} (B)
    (G) edge node [above right] {c} (F);
\end{tikzpicture}
\end{document}

答案1

edge node图例向一条边添加一个节点。默认情况下,添加的节点位于边的中间。使用小字体 ( font=\scriptsize) 和白色背景 ( fill=white),添加的节点似乎会剪裁边。

enter image description here

\documentclass[tikz]{standalone}
\tikzset{
  graph vertex/.style={
    circle,
    draw,
  },
  graph directed edge/.style={
    ->,
    >=stealth,
    thick,
  },
  graph tree edge/.style={
    graph directed edge
  },
  graph forward edge/.style={
    graph directed edge,
    every edge/.style={
      edge node={node [fill=white,font=\scriptsize] {f}},
      loosely dotted,
      draw,
    },
  },
  graph back edge/.style={
    graph directed edge,
    every edge/.style={
      edge node={node [fill=white,font=\scriptsize] {b}},
      densely dotted,
      draw,
    },
  },
  graph cross edge/.style={
    graph directed edge,
    every edge/.style={
      edge node={node [fill=white,font=\scriptsize] {c}},
      dotted,
      draw,
    },
  },
}
\begin{document}
\begin{tikzpicture}
  \foreach \letter [count=\c] in {A,B,C,D,E,F,G,H} {
    \node[graph vertex] (\letter) at ({-45*\c+157.5}:2cm) {\letter};
  }

  \path[graph tree edge]
    (A) edge (B)
    (B) edge (F)
    (F) edge (C)
    (F) edge (D)
    (D) edge (E)
    (A) edge (H)
    (H) edge (G);

  \path[graph forward edge]
    (F) edge (E);

  \path[graph back edge]
    (C) edge (B)
    (G) edge (A);

  \path[graph cross edge]
    (D) edge (C)
    (G) edge (B)
    (G) edge (F);
\end{tikzpicture}
\end{document}

相关内容