def _fix_fracs()

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


def _fix_fracs(text: str) -> str:
    """
    Fix the formatting of fractions in the given text.
    Copied from: https://github.com/hendrycks/math/blob/357963a7f5501a6c1708cf3f3fb0cdf525642761/modeling/math_equivalence.py#L1

    Args:
        text (str): The input text.

    Returns:
        str: The text with properly formatted fractions.

    Examples:
        >>> _fix_fracs("\\frac12")
        "\\frac{1}{2}"
        >>> _fix_fracs("\\frac{3}{4}")
        "\\frac{3}{4}"
        >>> _fix_fracs("\\frac1{2}")
        "\\frac{1}{2}"
    """
    substrs = text.split("\\frac")
    new_str = substrs[0]
    if len(substrs) > 1:
        for substr in substrs[1:]:
            # This allows use to have \\frac{1}{2} and \\ frac1{2}
            substr = substr.lstrip()
            new_str += "\\frac"
            if len(substr) > 0 and substr[0] == "{":
                new_str += substr

            elif len(substr) < 2:
                return text
            else:
                a = substr[0]
                b = substr[1]
                if b != "{":
                    if len(substr) > 2:
                        post_substr = substr[2:]
                        new_str += "{" + a + "}{" + b + "}" + post_substr
                    else:
                        new_str += "{" + a + "}{" + b + "}"
                else:
                    if len(substr) > 2:
                        post_substr = substr[2:]
                        new_str += "{" + a + "}" + b + post_substr
                    else:
                        new_str += "{" + a + "}" + b
    text = new_str
    return text