Python 代码生成 LaTeX 数学公式:latexify 参数 parameters
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-NC-SA 版权协议,转载请附上原文出处链接和本声明。
latexify
是一个用于将 Python 源代码片段编译为对应 LaTeX\LaTeXLATEX 表达式的 Python 包:
latexify
提供以下功能:
- 将 Python 源代码或其 AST 编译为 LaTeX\LaTeXLATEX 的库。
- 用于美观打印已编译函数的 IPython 类。
入门指南
本文档介绍了如何在你的 Python 代码中使用 latexify
。
安装
目前 latexify
仅依赖 Python 标准库。你可以通过 pip
直接安装:
pip install latexify-py
注意:必须安装 latexify-py
,而不是 latexify
。
在 Jupyter 中使用 latexify
latexify.function
装饰器会将你的函数包装起来,并以对应的 LaTeX 公式进行美观打印。Jupyter 会识别这种包装,并尝试输出 LaTeX 而非原始函数。
以下示例:
@latexify.function
def solve(a, b, c):return (-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a)solve
将会在输出中打印以下公式:
solve(a,b,c)=−b+b2−4ac2a \mathrm{solve}(a, b, c) = \frac{-b + \sqrt{b^2 - 4ac}}{2a} solve(a,b,c)=2a−b+b2−4ac
调用被包装的函数时,其行为与原始函数完全一致:
solve(1, 2, 1)
-1.0
对包装后的函数使用 str
会返回底层 LaTeX 源码:
print(solve)
f(n) = \\frac{-b + \\sqrt{b^{2} - 4ac}}{2a}
latexify.expression
与 latexify.function
类似,但不会打印函数签名:
@latexify.expression
def solve(a, b, c):return (-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a)solve
−b+b2−4ac2a \frac{-b + \sqrt{b^2 - 4ac}}{2a} 2a−b+b2−4ac
直接获取 LaTeX 表达式
你也可以使用 latexify.get_latex
,它接收一个函数并直接返回对应的 LaTeX 表达式。
latexify.get_latex
支持与 latexify.function
相同的参数。
def solve(a, b, c):return (-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a)latexify.get_latex(solve)
f(n) = \\frac{-b + \\sqrt{b^{2} - 4ac}}{2a}
latexify
参数
本文档列出了用于控制 latexify
行为的参数列表。
identifiers: dict[str, str]
用于替换的标识符键值对。
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
f(x)=g(x) f(x) = \mathrm{g}\left(x\right) f(x)=g(x)
reduce_assignments: bool
是否在 return
语句之前,将所有已定义的变量进行合并。
当前版本的 latexify
仅识别赋值语句。分析包含其他控制流程的函数时,可能会引发错误。
@latexify.function(reduce_assignments=True)
def f(a, b, c):discriminant = b**2 - 4 * a * cnumerator = -b + math.sqrt(discriminant)denominator = 2 * areturn numerator / denominatorf
f(a,b,c)=−b+b2−4ac2a f(a, b, c) = \frac{-b + \sqrt{b^{2} - 4 a c}}{2 a} f(a,b,c)=2a−b+b2−4ac
use_math_symbols: bool
是否自动将具有符号名称的变量转换为 LaTeX 符号。
@latexify.function(use_math_symbols=True)
def greek(alpha, beta, gamma, Omega):return alpha * beta + math.gamma(gamma) + Omegagreek
greek(α,β,γ,Ω)=αβ+Γ(γ)+Ω \mathrm{greek}({\alpha}, {\beta}, {\gamma}, {\Omega}) = {\alpha} {\beta} + \Gamma\left({{\gamma}}\right) + {\Omega} greek(α,β,γ,Ω)=αβ+Γ(γ)+Ω
use_set_symbols: bool
是否使用集合运算的二元运算符。
@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
f(x,y)=(x∩y , x∪y , x∖y , x△y , x⊂y , x⊆y , x⊃y , x⊇y) f(x, y) = \left( x \cap y\space,\space x \cup y\space,\space x \setminus y\space,\space x \mathbin{\triangle} y\space,\space {x \subset y}\space,\space {x \subseteq y}\space,\space {x \supset y}\space,\space {x \supseteq y}\right) f(x,y)=(x∩y , x∪y , x∖y , x△y , x⊂y , x⊆y , x⊃y , x⊇y)
use_signature: bool
是否输出函数签名。
该标志的默认值取决于前端函数。latexify.function
中默认为 True
,而 latexify.expression
中默认为 False
。
@latexify.function(use_signature=False)
def f(a, b, c):return (-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a)f
−b+b2−4ac2a \frac{-b + \sqrt{b^{2} - 4 a c}}{2 a} 2a−b+b2−4ac
风险提示与免责声明
本文内容基于公开信息研究整理,不构成任何形式的投资建议。历史表现不应作为未来收益保证,市场存在不可预见的波动风险。投资者需结合自身财务状况及风险承受能力独立决策,并自行承担交易结果。作者及发布方不对任何依据本文操作导致的损失承担法律责任。市场有风险,投资须谨慎。