10

Consider a small example below. I'd like to automate the addition of the label-node along the edge through the use of styles. I've tried the naïve solution of just adding such a necessary node statement, but it makes sense why it wouldn't work like this. Can I add the labels another way?

(Since it's not reasonable to expect node placement to be automated, is it possible to also 'clip away' the part of the edge that would clash with the node?)

output


What I'd like to happen is simply be able to say

\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);

and have the graph forward edge style add an f in the middle of the edge to more clearly denote its role in the graph. (Similarly with graph back edge and graph cross edge.)

Full example

\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}
Sean Allred
  • 27,421

1 Answers1

9

The edge node key adds a node to an edge. By default, this added node is at the middle of the edge. With a small font (font=\scriptsize) and a white background (fill=white), the added node seems to clip the edge.

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}
Paul Gaborit
  • 70,770
  • 10
  • 176
  • 283