from lxml import etree import latex2mathml.converter def mathml_to_operator_tree(node): """Convert MathML to an operator tree.""" # If the node is a leaf node, return the operator as a string if len(node) == 0: return node.text # Otherwise, recursively build the operator tree for each child node operator_tree = "" for child in node: operator_tree += str(child.tag).split('}')[-1].replace('m', '') +"(" + str(mathml_to_operator_tree(child)) + ')' return operator_tree def latex2tree(latex_text): mathml = latex2mathml.converter.convert(latex_text) root = etree.fromstring(mathml.encode()) operator_tree = mathml_to_operator_tree(root[0]) return operator_tree