Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
I am not sure I understand what you are trying to achieve. Could you please explain in more detail? –  Gonzalo Medina Feb 9 at 3:19
    
@GonzaloMedina See my edit. cmhughes, my ideal would be \path[graph forward edge] (A) edge (B); and automagically have an f (per path style) appear in the middle of the edge (with the edge clipped)—hopefully my edit explains this better :-) –  Sean Allred Feb 9 at 3:25
    
The library decoration.markings is your friend: you can exploit my insert node (see for example TikZ: using the pos = x in a node) and put it in a high level style graph forward edge. –  Claudio Fiandrino Feb 9 at 7:51

1 Answer 1

up vote 8 down vote accepted

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}
share|improve this answer
    
Which version of TikZ/PGF are you using? Mine does not know the edge node key; was this something added in v3? –  Sean Allred Feb 9 at 17:59
    
[After updating to TikZ v3, this example is compilable.] Thanks!! –  Sean Allred Feb 9 at 19:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.