Mirzhan Irkegulov
Mirzhan Irkegulov

Reputation: 18055

Graphviz syntax shortcuts

Writing huge graphs in Graphviz is tedious. One day I will write my graphs in a custom format, which I will then transform into DOT format, but currently I want to find out the ways to write DOT files manually as succinctly as possible. I currently know of only one syntax shortcut: a -- {b c}; is equivalent to a -- b; a -- c;. In the DOT file currently there are many repeating patterns, for example many edges have the same label. Can I write something like:

// something that expands -m> into [label=meaning]
"English/cat" -m> "Meaning/cat_(Felidae)";
"English/cat" -m> "Meaning/domestic_cat";
"English/cat" -m> "Meaning/catfish";
"English/cat" -m> "Meaning/jazz_player";
"English/cat" -m> "Meaning/cat_(nautical)";

So that it would be equivalent to the below:

"English/cat" -> "Meaning/cat_(Felidae)" [label=meaning];
"English/cat" -> "Meaning/domestic_cat" [label=meaning];
"English/cat" -> "Meaning/catfish" [label=meaning];
"English/cat" -> "Meaning/jazz_player" [label=meaning];
"English/cat" -> "Meaning/cat_(nautical)" [label=meaning];

Is this possible? Are there any other possible syntax shortcuts in Graphviz that would make DOT files simpler and shorter? I would be happy if you could compile all such methods in the answers.

Upvotes: 0

Views: 694

Answers (2)

Mirzhan Irkegulov
Mirzhan Irkegulov

Reputation: 18055

To expand on marapet's answer, you can put default node/edge/graph attributes into a subgraph (see DOT language documentation), so the attribute changes will affect only the scope of the subgraph. Here, only edges c -> d and e -> f will have a label "meaning" attached, while edges a -> b and g -> h will have no label:

a -> b;
subgraph {
  edge[label="meaning"];
  c -> d;
  e -> f;
}
g -> h;

Upvotes: 2

marapet
marapet

Reputation: 56446

The dot language allows default attributes for graphs, nodes and edges. These default attributes are valid for any new graph/node/edge defined after the default attributes, or until other default attributes are defined.

Your example could be rewritten this way:

// label for all edges from here on
edge [label=meaning];
"English/cat" -> "Meaning/cat_(Felidae)";
"English/cat" -> "Meaning/domestic_cat";
"English/cat" -> "Meaning/catfish";
"English/cat" -> "Meaning/jazz_player";
"English/cat" -> "Meaning/cat_(nautical)";

// no/empty label from here on
edge[label=""];
"English/cat" -> "Other";

The same applies for nodes and graphs, just use graph [a=b, c=d, ...] and node [a=b, c=d, ...].

Upvotes: 2

Related Questions