Modify the way ^ is rendering

This commit is contained in:
Bertrand Benjamin 2018-03-14 11:40:57 +03:00
parent eff438b930
commit 6763ae6074
2 changed files with 16 additions and 17 deletions

View File

@ -161,17 +161,20 @@ def pow2tex(left, right):
>>> from ..MO import mo >>> from ..MO import mo
>>> pow2tex(mo.MO(2), mo.MO(3)) >>> pow2tex(mo.MO(2), mo.MO(3))
'2 ^ 3' '2^{3}'
>>> from ..tree import Tree >>> from ..tree import Tree
>>> t = Tree.from_str("1^2") >>> t = Tree.from_str("1^2")
>>> pow2tex(t, mo.MO(3)) >>> pow2tex(t, mo.MO(3))
'1 ^ 2 ^ 3' '1^{2}^{3}'
>>> t = Tree.from_str("1^2")
>>> pow2tex(mo.MO(3), t)
'3^{1^{2}}'
>>> t = Tree.from_str("1+2") >>> t = Tree.from_str("1+2")
>>> pow2tex(t, mo.MO(3)) >>> pow2tex(t, mo.MO(3))
'(1 + 2) ^ 3' '(1 + 2)^{3}'
>>> t = Tree.from_str("1*2") >>> t = Tree.from_str("1*2")
>>> pow2tex(mo.MO(3), t) >>> pow2tex(mo.MO(3), t)
'3 ^ (1 \\times 2)' '3^{1 \\times 2}'
""" """
try: try:
left_need_parenthesis = False left_need_parenthesis = False
@ -184,19 +187,15 @@ def pow2tex(left, right):
left_ = f"({tree2tex(left)})" left_ = f"({tree2tex(left)})"
else: else:
left_ = tree2tex(left) left_ = tree2tex(left)
try: try:
right_need_parenthesis = False right.node
if OPERATORS[right.node]["precedence"] < OPERATORS['^']["precedence"]:
right_need_parenthesis = True
except AttributeError: except AttributeError:
right_ = right.__tex__ right_ = right.__tex__
else:
if right_need_parenthesis:
right_ = f"({tree2tex(right)})"
else: else:
right_ = tree2tex(right) right_ = tree2tex(right)
return f"{left_} ^ {right_}" return f"{left_}^{{{right_}}}"
OPERATOR2TEX = { OPERATOR2TEX = {
"+": plus2tex, "+": plus2tex,