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

View File

@ -176,17 +176,17 @@ def pow2txt(left, right):
>>> from ..MO import mo
>>> pow2txt(mo.MO(2), mo.MO(3))
'2 ^ 3'
'2^3'
>>> from ..tree import Tree
>>> t = Tree.from_str("1^2")
>>> pow2txt(t, mo.MO(3))
'1 ^ 2 ^ 3'
'1^2^3'
>>> t = Tree.from_str("1+2")
>>> pow2txt(t, mo.MO(3))
'(1 + 2) ^ 3'
'(1 + 2)^3'
>>> t = Tree.from_str("1*2")
>>> pow2txt(mo.MO(3), t)
'3 ^ (1 * 2)'
'3^(1 * 2)'
"""
try:
left_need_parenthesis = False
@ -211,7 +211,7 @@ def pow2txt(left, right):
else:
right_ = tree2txt(right)
return f"{left_} ^ {right_}"
return f"{left_}^{right_}"
OPERATOR2TXT = {
"+": plus2txt,