-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawTikz.py
executable file
·48 lines (36 loc) · 1.24 KB
/
drawTikz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3
import sys
import pydot
from tikz import Figure
from tikz import Node
from tikz import Path
def posToList(s):
def f(s): tuple([float(k) for k in s.split(",")])
return [f(i) for i in s[1:-1].split(" ")]
def tikzFromPos(g):
f = Figure()
tikzNodes = dict()
for n in g.get_nodes():
tn = Node(posToList(n.get_attributes()["pos"])[0], n.get_name())
tikzNodes[n.get_name()] = tn
f.addNode(tn)
# Graphviz pos attribute requires 3n+1 points, drawing a cubic splines for each triple of points
for e in g.get_edges():
color = ""
if "color" in e.get_attributes():
color = e.get_attributes()["color"]
p = Path(tikzNodes[e.get_source()], style=f"draw,{color}")
if "pos" in e.get_attributes():
controls = posToList(e.get_attributes()["pos"])
p.to(controls[0])
for i in range(1, len(controls), 3):
p.curveTo(controls[i + 2], controls[i], controls[i + 1])
p.to(tikzNodes[e.get_destination()])
f.addPath(p)
return f
def main():
with sys.stdin as dot:
g = pydot.graph_from_dot_data(dot.read())
print(tikzFromPos(g[0]).compile())
if __name__ == "__main__":
main()