Fix: Clean tree2tex by copying tree2txt

This commit is contained in:
Bertrand Benjamin 2019-06-28 11:30:54 +02:00
parent aeb8fa4321
commit f037d5545f

View File

@ -29,21 +29,11 @@ def plus2tex(left, right):
>>> plus2tex(MO.factory(3), t) >>> plus2tex(MO.factory(3), t)
'3 - 2 \\times 3' '3 - 2 \\times 3'
""" """
display_plus = True
try:
left.node
except AttributeError:
left_ = left.__tex__
else:
left_ = tree2tex(left)
try: left_ = render_with_parenthesis(left, "+")
right.node right_ = render_with_parenthesis(right, "+")
except AttributeError:
right_ = right.__tex__ display_plus = True
else:
right_ = tree2tex(right)
finally:
if right_.startswith("-"): if right_.startswith("-"):
display_plus = False display_plus = False
@ -97,6 +87,8 @@ def mul2tex(left, right):
>>> a = MO.factory('x') >>> a = MO.factory('x')
>>> mul2tex(MO.factory(3), a) >>> mul2tex(MO.factory(3), a)
'3x' '3x'
>>> mul2tex(MO.factory(-3), a)
'- 3x'
>>> mul2tex(a, a) >>> mul2tex(a, a)
'x \\times x' 'x \\times x'
""" """
@ -104,9 +96,15 @@ def mul2tex(left, right):
right_ = render_with_parenthesis(right, "*") right_ = render_with_parenthesis(right, "*")
display_time = True display_time = True
if (right_[0].isalpha() and (left_.isnumeric() or left_.isdecimal())) or right_[ # if (right_[0].isalpha() and (left_.isnumeric() or left_.isdecimal())) or right_[
0 # 0
] == "(": # ] == "(":
# display_time = False
if right_[0].isalpha():
# TODO: C'est bien beurk en dessous... |ven. déc. 21 12:03:07 CET 2018
if type(left).__name__ == "MOnumber":
display_time = False
elif right_[0] == "(":
display_time = False display_time = False
if display_time: if display_time:
@ -133,11 +131,11 @@ def div2tex(left, right):
""" """
try: try:
left_ = tree2tex(left) left_ = tree2tex(left)
except AttributeError: except (AttributeError, ValueError):
left_ = left.__tex__ left_ = left.__tex__
try: try:
right_ = tree2tex(right) right_ = tree2tex(right)
except AttributeError: except (AttributeError, ValueError):
right_ = right.__tex__ right_ = right.__tex__
return "\\frac{" + left_ + "}{" + right_ + "}" return "\\frac{" + left_ + "}{" + right_ + "}"
@ -226,6 +224,10 @@ def tree2tex(tree):
>>> tree2tex(t) >>> tree2tex(t)
'2 + 3 \\times 4' '2 + 3 \\times 4'
""" """
from ..tree import Tree
if not isinstance(tree, Tree):
raise ValueError(f"Can only render a Tree (got {type(tree).__name__}: {tree})")
return OPERATOR2TEX[tree.node](tree.left_value, tree.right_value) return OPERATOR2TEX[tree.node](tree.left_value, tree.right_value)