[Solved] Using Python Regex to Simplify Latex Fractions


Here is a regex that should work. Note that I made a change in your LaTeX expression because I think there was an error (* sign).

astr="\frac{(\ChebyU{\ell}@{x})^2}{\frac{\pi}{2}}"

import re
pat = re.compile('\\frac\{(.*?)\}\{\\frac\{(.*?)\}\{(.*?)\}\}')
match = pat.match(astr)
if match:
    expr="\\frac{{{0}*{2}}}{{{1}}}".format(*match.groups())
    print expr

NB: I did not include the spaces in your original expression.

0

solved Using Python Regex to Simplify Latex Fractions