首页 > 解决方案 > 使用 Python 将 LaTeX 字符串与内联方程拆分

问题描述

如何通过保留顺序来拆分包含文本、内联方程和方程块的 LaTeX 字符串?

我想分开

Functions $f(x)$ and $g(x)$ is related as $$f(x)-g(x)=h(x)$$

进入

['Functions ','f(x)','and','g(x)','is related as ','f(x)-g(x)=h(x)']

标签: python

解决方案


如何使用列表理解:

text_in = "Functions $f(x)$ and $g(x)$ is related as $$f(x)-g(x)=h(x)$$"
foo = [x.strip() for x in text_in.split("$") if x]
print(foo)

这给了你:

['Functions', 'f(x)', 'and', 'g(x)', 'is related as', 'f(x)-g(x)=h(x)']

推荐阅读