Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- def v_norm(val,center):
- scale_f = 10 / center
- d = center / 2
- print(val-d)
- return (val - d) * scale_f
- def process_node(n):
- for x in ['label','center','source','target','x','y']:
- n = n.replace(x,'"%s"' %x)
- return json.loads(n)
- def to_dict(node_s):
- node_s = node_s.strip('{}')
- node_s = node_s.strip('[]')
- node_s = node_s.replace('},{','},E,{')
- node_l = node_s.split(',E,')
- return [process_node(n) for n in node_l]
- def extract_features(inp):
- # returns tuple of (height,width,node_s,edge_s)
- node_s, edge_s = inp.split('nodes=')[1].split(' edges=')
- edge_s = edge_s.strip(' />')
- height = int(inp.split('height="')[1].split('"')[0])
- width = int(inp.split('width="')[1].split('"')[0])
- return height,width,to_dict(node_s),to_dict(edge_s)
- def str_to_latex(inp,directed=False):
- out = '''\\begin{tikzpicture}
- \\tikzset{vertex/.style = {shape=circle,draw,minimum size=1.5em}}\n'''
- height,width,n,e = extract_features(inp)
- for node in n:
- out += '\\node[vertex] (%s) at (%f,%f) {%s};\n' % \
- (node['label'], v_norm(node['center']['x'],width), v_norm(node['center']['y'],height), node['label'])
- out += '\\path[%s]\n' % ('->' if directed else '-')
- for edge in e:
- s,t = str(edge['source']), str(edge['target'])
- out += '(%s) edge (%s)\n' % (s, t)
- out += '\\end{tikzpicture}'
- return out
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement