def convert_atom_expr()

in src/latex2sympy2_extended/latex2sympy2.py [0:0]


    def convert_atom_expr(self, atom_expr):
        # find the atom's text
        atom_text = ''
        if atom_expr.LETTER_NO_E():
            atom_text = atom_expr.LETTER_NO_E().getText()
            if atom_text == "I":
                return sympy.I
        elif atom_expr.GREEK_CMD():
            atom_text = atom_expr.GREEK_CMD().getText()
        elif atom_expr.OTHER_SYMBOL_CMD():
            atom_text = atom_expr.OTHER_SYMBOL_CMD().getText()
        elif atom_expr.ACCENT():
            atom_text = atom_expr.ACCENT().getText()
            # Remove the command by striping first { and last }
            text_start = atom_text.index('{')
            accent_name = atom_text[1:text_start]
            accent_text = atom_text[text_start + 1:-1].replace(" ", "")
            # exception: check if bar or overline which are treated both as bar
            if accent_name in ["bar", "overline"]:
                accent_name = "bar"
            elif accent_name in ["vec", "overrightarrow"]:
                accent_name = "vec"
            elif accent_name in ["tilde", "widetilde"]:
                accent_name = "tilde"
            elif "text" in accent_name or "mbox" in accent_name:
                # We ignore text accents so that $C$ == $\\text{C}$
                accent_name = ""
                # Remove the parentheses
                accent_text = accent_text.replace("(", "").replace(")", "")
            elif "math" in accent_name:
                accent_name = "math"
            
            if accent_name:
                atom_text = f"{accent_name}{{{accent_text}}}"
            else:
                atom_text = accent_text

        # find atom's subscript, if any
        subscript_text = ''
        if atom_expr.subexpr():
            subexpr = atom_expr.subexpr()
            subscript = None
            if subexpr.expr():  # subscript is expr
                subscript = subexpr.expr().getText().strip()
            elif subexpr.atom():  # subscript is atom
                subscript = subexpr.atom().getText().strip()
            elif subexpr.args():  # subscript is args
                subscript = subexpr.args().getText().strip()
            subscript_inner_text = StrPrinter().doprint(subscript)
            if len(subscript_inner_text) > 1:
                subscript_text = '_{' + subscript_inner_text + '}'
            else:
                subscript_text = '_' + subscript_inner_text

        # construct the symbol using the text and optional subscript
        atom_symbol = get_symbol(atom_text.strip() + subscript_text, self.is_real, self.config.lowercase_symbols)
        # for matrix symbol
        matrix_symbol = None
        if atom_text + subscript_text in self.var:
            try:
                rh = self.var[atom_text + subscript_text]
                shape = sympy.shape(rh)
                matrix_symbol = sympy.MatrixSymbol(atom_text + subscript_text, shape[0], shape[1])
                self.variances[matrix_symbol] = self.variances[atom_symbol]
            except Exception:
                pass

        # find the atom's superscript, and return as a Pow if found
        if atom_expr.supexpr():
            supexpr = atom_expr.supexpr()
            func_pow = None
            if supexpr.expr():
                func_pow = self.convert_expr(supexpr.expr())
            else:
                func_pow = self.convert_atom(supexpr.atom())
            return sympy.Pow(atom_symbol, func_pow, evaluate=False)

        return atom_symbol if not matrix_symbol else matrix_symbol