latexify 对 ast 包进行了重新实现, 可将 Python 源代码或 AST 编译为 LaTeX 数学表达式,支持Python 3.7 到 3.11的语法。
替换标识符键值对
identifiers = { 'my_function': 'f', 'my_inner_function': 'g', 'my_argument': 'x',}@latexify.function(identifiers=identifiers)def my_function(my_argument): return my_inner_function(my_argument)my_function
组合 return 语句之前定义的所有变量
@latexify.function(reduce_assignments=True)def f(a, b, c): discriminant = b**2 - 4 * a * c numerator = -b + math.sqrt(discriminant) denominator = 2 * a return numerator / denominatorf
自动将带有符号名称的变量转换为 LaTeX
@latexify.function(use_math_symbols=True)def greek(alpha, beta, gamma, Omega): return alpha * beta + math.gamma(gamma) + Omegagreek
使用原始字符串作为函数名
@latexify.function(use_raw_function_name=True)def quadratic_solution(a, b, c): return (-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a)f
使用二元运算符进行集合运算
@latexify.function(use_set_symbols=True)def f(x, y): return x & y, x | y, x - y, x ^ y, x < y, x <= y, x > y, x >= yf
直接获取 LaTeX 表达式
latexify.get_latex 接受一个函数并直接返回与给定函数对应的 LaTeX 表达式
def solve(a, b, c): return (-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a)latexify.get_latex(solve)
mathrm{f}(n) = \\frac{-b + \\sqrt{b^{{2}} - {4}ac}}{{2}a}
$ pip install latexify-py
联系客服