13

I have the following code, and it results in the image below. As you can see, it's a little crowded around the edges and edge labels, especially around the "^a". What is the best way to create just a tad more space, so that one can clearly see which label belongs to which edge?

digraph finite_state_machine {                                                                                                                                                                                  
    pad=0.2;
    {
        rank=same;
        node [shape = point, style = invis]; q_0;
        node [shape = doublecircle, style = solid]; q_5;
        node [shape = circle];
        q_1 [ label = <<i>q<sub>1</sub></i>> ];
        q_2 [ label = <<i>q<sub>2</sub></i>> ];
        q_3 [ label = <<i>q<sub>3</sub></i>> ];
        q_4 [ label = <<i>q<sub>4</sub></i>> ];
        q_5 [ label = <<i>q<sub>5</sub></i>> ];
        q_0 -> q_1;
        q_1 -> q_2 [ label = "." ];
        q_1 -> q_2 [ label = <&epsilon;>, constraint=false ];
        q_2 -> q_1 [ label = <&epsilon;>, constraint=false ];
        q_2 -> q_3 [ label = <<i>a</i>> ];
        q_3 -> q_4 [ label = <<i>^a</i>> ];
        q_3 -> q_4 [ label = <&epsilon;>, constraint=false ];
        q_4 -> q_3 [ label = <&epsilon;>, constraint=false ];
        q_4 -> q_5 [ label = <<i>b</i>> ];
    }
}

enter image description here

oskarkv
  • 2,329
  • 2
  • 21
  • 29
  • 2
    I think you can try to set `minlen=5` when you define your edges. For example - `q_1 -> q_2 [ minlen=5,label = <ε>, constraint=false ];` – Raj Oct 25 '12 at 05:57
  • That did not work out so great. I tried it on the epsilon edge from q4 to q3. The egde became longer, increasing the distance between the nodes, but did not actually go any higher, thus the problem remains. – oskarkv Dec 19 '12 at 13:23
  • 1
    You could use ports to curve the edges more, as in: http://stackoverflow.com/questions/18515529/graphviz-place-edge-label-on-the-other-side-ii/18698294#18698294 – Richard EB Dec 02 '13 at 11:04

3 Answers3

13

There is no attribute in Graphviz to adjust the margin/padding around edge labels. The closest you can probably get to the effect you require is to use \n to introduce blank lines above/below your label to force space.

Obviously, this will not scale to anything automatic.

Alternately, you could try to use the ranksep attribute to force in some additional space.

Pekka
  • 3,529
  • 27
  • 45
9

If xlabel doesn't solve it, then wrapping the label in a table can sometimes be a work-around. For example:

q_1 -> q_2 [ label = <<table cellpadding="10" border="0" cellborder="0">
                        <tr><td>&epsilon;</td></tr>
                      </table>>, 
             constraint = false ];

To add more space on one side than on another, you can add an empty cell. The code then quickly becomes (more) unreadable, but you could use a simple sed script to pre-process your dot file.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Tilo
  • 3,255
  • 26
  • 31
5

I know this is an old question, but this approach below might also be helpful, if this is what you are looking for. see the image below. i added the following to your code:

  • minlen=2 (to extend gaps between nodes)

  • tailport=n/s (to change location of the tail of the arrow to north/south)

  • headport=n/s (to change location of the head to the arrow to north or south)


digraph finite_state_machine {                                                                                                                                                                                  
    pad=0.2;
    {
        rank=same;
        node [shape = point, style = invis]; q_0;
        node [shape = doublecircle, style = solid]; q_5;
        node [shape = circle];
        q_1 [ label = <<i>q<sub>1</sub></i>> ];
        q_2 [ label = <<i>q<sub>2</sub></i>> ];
        q_3 [ label = <<i>q<sub>3</sub></i>> ];
        q_4 [ label = <<i>q<sub>4</sub></i>> ];
        q_5 [ label = <<i>q<sub>5</sub></i>> ];
        q_0 -> q_1;
        q_1 -> q_2 [ label = "." ];
        q_1 -> q_2 [ label = <&epsilon;>, constraint=false, minlen=2, tailport=n, headport=n];
        q_2 -> q_1 [ label = <&epsilon;>, constraint=false, minlen=2, tailport=s, headport=s];
        q_2 -> q_3 [ label = <<i>a</i>> ];
        q_3 -> q_4 [ label = <<i>^a</i>> ];
        q_3 -> q_4 [ label = <&epsilon;>, constraint=false, minlen=2, tailport=n, headport=n];
        q_4 -> q_3 [ label = <&epsilon;>, constraint=false, minlen=2, tailport=s, headport=s];
        q_4 -> q_5 [ label = <<i>b</i>> ];
    }
}

image

colidyre
  • 4,170
  • 12
  • 37
  • 53
GSA
  • 751
  • 8
  • 12
  • This is equivalent to the explicit ports suggestion [Graphviz: Place edge label on the other side (II)](https://stackoverflow.com/questions/18515529/graphviz-place-edge-label-on-the-other-side-ii/18698294#18698294) by [Richard EB](https://stackoverflow.com/users/897059/richard-eb). – Pekka Jun 26 '21 at 08:29