From 6f6b481b575ef275f334fee164aabdfedbd6ca38 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Mon, 13 Jul 2015 18:40:03 -0500 Subject: [PATCH] TikZ vis: use roman numerals for control sequence names --- boxtree/visualization.py | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/boxtree/visualization.py b/boxtree/visualization.py index 2a07746..46fdc33 100644 --- a/boxtree/visualization.py +++ b/boxtree/visualization.py @@ -26,6 +26,29 @@ THE SOFTWARE. """ +def int_to_roman(inp): + """ + Convert an integer to Roman numerals. + """ + # stolen from + # https://code.activestate.com/recipes/81611-roman-numerals/ + + if not isinstance(inp, int): + raise TypeError("expected integer, got %s" % type(inp)) + if inp == 0: + return "Z" + if not 0 < inp < 4000: + raise ValueError("Argument must be between 1 and 3999 (got %d)" % inp) + ints = (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1) + nums = ('M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I') + result = "" + for i in range(len(ints)): + count = int(inp / ints[i]) + result += nums[i] * count + inp -= ints[i] * count + return result + + class TreePlotter: """Assumes that the tree has data living on the host. See :meth:`boxtree.Tree.get`. @@ -115,11 +138,11 @@ class TreePlotter: r"\coordinate (boxc%d) at (%r, %r);" % (ibox, float(c[0]), float(c[1]))) lines.append( - r"\def\boxsize%d{%r}" - % (ibox, float(eh[0]-eh[1]))) + r"\def\boxsize%s{%r}" + % (int_to_roman(ibox), float(eh[0]-el[0]))) lines.append( - r"\def\boxlevel%d{%r}" - % (ibox, self.tree.box_levels[ibox])) + r"\def\boxlevel%s{%r}" + % (int_to_roman(ibox), self.tree.box_levels[ibox])) lines.append( r"\def\boxpath#1{(boxl#1) rectangle (boxh#1)}") -- GitLab