Search code examples
graphvizdot

How to write long labels just once, and use references to them in the graph?


I have some long labels on nodes. Because nodes are interconnected (of course), and thus the exact same labels appear in several places, every time I need to edit one of them, I have to manually change them multiple times, which is time-consuming and error-prone (or copy once, then find+replace; which is only slightly less annoying).

So, instead of something like this:

digraph {
    "Some very long label" -> "Another label with elaborate text"
    "Some very long label" -> "Red blue green yellow"
    "Another label with elaborate text" -> "Blah bleh bleh blah"
    "Blah bleh bleh blah" -> "Sooo annoying! Really!"
}

I would like to do some like this (made-up syntax):

digraph {

    // Structure:
    #A -> #B
    #A -> #C
    #B -> #C
    #B -> #D
    #D -> #E

    // Label definitions:
    #A: "Some very long label"
    #B: "Another label with elaborate text"
    #C: "Red blue green yellow"
    #D: "Blah bleh bleh blah"
    #E: "Sooo annoying! Really!"

}

Is that possible?


Solution

  • I think you were on the good track, only with the wrong syntax. How about using id's for the nodes and set appropriate labels like:

    digraph G{
        N1 [label="Some very long label"]
        N2 [label="Another label with elaborate text"]
        N3 [label="Red blue green yellow"]
        N4 [label="Blah bleh bleh blah"]
        N5 [label="Sooo annoying! Really!"]
    
        N1 -> N3
        N2 -> N4
        N1 -> N2
        N4 -> N5
    
    }