使用Python Regex简化乳胶分数 [英] Using Python Regex to Simplify Latex Fractions

查看:56
本文介绍了使用Python Regex简化乳胶分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

LaTeX中的示例代码:

Sample code in Latex:

%Chebyshev of second kind
\begin{equation}
\sum_{\ell \hiderel{=} 0}^n\frac{( \ChebyU{\ell}@{x}  )^2}*{ \frac{\pi}{2}  }*=\frac{ 2^n  }{ \frac{\pi}{2}   2^n+1  }\frac{ \ChebyU{n+1}@{x}   \ChebyU{n}@{y}  - \ChebyU{n}@{x}   \ChebyU{n+1}@{y}  }{x-y}
\end{equation}

\ frac {(\ ChebyU {\ ell} @ {x})^ 2} * {\ frac {\ pi} {2}} 是我正在寻找的特定分数案件.由于它是另一个分数的分母,因此我想使用Python正则表达式将其从 a/(b/c)更改为(ac)/b .

\frac{(\ChebyU{\ell}@{x})^2}*{\frac{\pi}{2}} is the fraction I am looking at for this specific case. Since it is a fraction in the denominator of another fraction I would like to use Python regex to change this from a/(b/c) to (ac)/b.

示例输出:

%Chebyshev of second kind
\begin{equation}
\sum_{\ell \hiderel{=} 0}^n\frac{(2 \ChebyU{\ell}@{x}  )^2}{\pi}*=\frac{ 2^n  }{ \frac{\pi}{2}   2^n+1  }\frac{ \ChebyU{n+1}@{x}   \ChebyU{n}@{y}  - \ChebyU{n}@{x}   \ChebyU{n+1}@{y}  }{x-y}
\end{equation}

最终结果: \ frac {(2 \ ChebyU {\ ell} @ {x})^ 2} {\ pi} 是正则表达式应产生的分数.

End result: \frac{(2\ChebyU{\ell}@{x})^2}{\pi} is the fraction that the regex should result in.

我该如何在python中使用正则表达式来做到这一点?

How would I go about doing this with regex in Python?

推荐答案

这是一个应该有效的正则表达式.请注意,我对您的 LaTeX 表达式进行了更改,因为我认为存在错误(* 符号).

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.

这篇关于使用Python Regex简化乳胶分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆