【问题标题】:Using Python Regex to Simplify Latex Fractions使用 Python 正则表达式简化乳胶分数
【发布时间】:2015-03-30 20:38:54
【问题描述】:

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

示例输出:

%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} 是正则表达式应产生的分数。

我将如何在 Python 中使用正则表达式执行此操作?

【问题讨论】:

    标签: python regex math latex pdflatex


    【解决方案1】:

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

    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
    

    注意:我没有在您的原始表达式中包含空格。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-24
      • 2016-01-06
      • 1970-01-01
      • 2014-06-22
      • 1970-01-01
      • 2015-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多