Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. import json
  2.  
  3. def v_norm(val,center):
  4. scale_f = 10 / center
  5. d = center / 2
  6. print(val-d)
  7. return (val - d) * scale_f
  8. def process_node(n):
  9. for x in ['label','center','source','target','x','y']:
  10. n = n.replace(x,'"%s"' %x)
  11. return json.loads(n)
  12.  
  13. def to_dict(node_s):
  14. node_s = node_s.strip('{}')
  15. node_s = node_s.strip('[]')
  16. node_s = node_s.replace('},{','},E,{')
  17. node_l = node_s.split(',E,')
  18. return [process_node(n) for n in node_l]
  19.  
  20. def extract_features(inp):
  21. # returns tuple of (height,width,node_s,edge_s)
  22. node_s, edge_s = inp.split('nodes=')[1].split(' edges=')
  23. edge_s = edge_s.strip(' />')
  24. height = int(inp.split('height="')[1].split('"')[0])
  25. width = int(inp.split('width="')[1].split('"')[0])
  26. return height,width,to_dict(node_s),to_dict(edge_s)
  27.  
  28. def str_to_latex(inp,directed=False):
  29. out = '''\\begin{tikzpicture}
  30. \\tikzset{vertex/.style = {shape=circle,draw,minimum size=1.5em}}\n'''
  31. height,width,n,e = extract_features(inp)
  32. for node in n:
  33. out += '\\node[vertex] (%s) at (%f,%f) {%s};\n' % \
  34. (node['label'], v_norm(node['center']['x'],width), v_norm(node['center']['y'],height), node['label'])
  35. out += '\\path[%s]\n' % ('->' if directed else '-')
  36. for edge in e:
  37. s,t = str(edge['source']), str(edge['target'])
  38. out += '(%s) edge (%s)\n' % (s, t)
  39. out += '\\end{tikzpicture}'
  40. return out
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement