latexify_py是一个 Google开源的Python 包,可以将Python源代码片段编译为相应的LaTeX表达式。
latexify_py 的使用非常简单,只需安装该库(pip install latexify-py)并导入相应模块即可。下面介绍的是一些简单的使用方法。
目录
将Python函数转化为LaTex公式
import latexify import math @latexify.function def solve(a, b, c): return (-b + math.sqrt(b**2 - 4*a*c)) / (2*a) print(solve) solve
输出的LaTex的公式为:
$$\mathrm{solve}(a, b, c) = \frac{-b + \sqrt{ b^{2} – 4 a c }}{2 a}$$
可以看到上述代码中使用了装饰器,如果不使用装饰器,可以使用latexify.get_latex()来获取公式:
def solve(a, b, c): return (-b + math.sqrt(b**2 - 4*a*c)) / (2*a) latexify.get_latex(solve)
输出的内容为:’\\mathrm{solve}(a, b, c) = \\frac{-b + \\sqrt{ b^{2} – 4 a c }}{2 a}’
一些更为复杂的输出案例:
分段函数
@latexify.function def sinc(x): if x == 0: return 1 else: return math.sin(x) / x print(sinc) sinc
输出内容为:
$$\mathrm{sinc}(x) = \left\{ \begin{array}{ll} 1, & \mathrm{if} \ x = 0 \\ \frac{\sin x}{x}, & \mathrm{otherwise} \end{array} \right.$$
分段递归函数
@latexify.function def fib(x): if x == 0: return 0 elif x == 1: return 1 else: return fib(x-1) + fib(x-2) print(fib) fib
输出内容为:
$$\mathrm{fib}(x) = \left\{ \begin{array}{ll} 0, & \mathrm{if} \ x = 0 \\ 1, & \mathrm{if} \ x = 1 \\ \mathrm{fib} \mathopen{}\left( x – 1 \mathclose{}\right) + \mathrm{fib} \mathopen{}\left( x – 2 \mathclose{}\right), & \mathrm{otherwise} \end{array} \right.$$
Latexify的参数设定
Latexify除了以上简单的使用外,还支持输入部分参数来完善整体的公式输出。
函数名称、参数的重定义
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) print(my_function) my_function
输出内容为:
$$f(x) = g \mathopen{}\left( x \mathclose{}\right)$$
自动转化数学符号
@latexify.function(use_math_symbols=True) def greek(alpha, beta, gamma, Omega): return alpha * beta + math.gamma(gamma) + Omega print(greek) greek
输出内容为:
$$\mathrm{greek}(\alpha, \beta, \gamma, \Omega) = \alpha \beta + \Gamma \mathopen{}\left( \gamma \mathclose{}\right) + \Omega$$
赋值语句简化
不设置简化的代码:
@latexify.function() def f(a, b, c): discriminant = b**2 - 4 * a * c numerator = -b + math.sqrt(discriminant) denominator = 2 * a return numerator / denominator print(f) f
输出内容为:
$$\begin{array}{l} \mathrm{discriminant} = b^{2} – 4 a c \\ \mathrm{numerator} = -b + \sqrt{ \mathrm{discriminant} } \\ \mathrm{denominator} = 2 a \\ f(a, b, c) = \frac{\mathrm{numerator}}{\mathrm{denominator}} \end{array}$$
如果进行简化设置:
@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 / denominator print(f) f
输出内容为:
$$ f(a, b, c) = \frac{-b + \sqrt{ b^{2} – 4 a c }}{2 a}$$
使用二进制操作符号
@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 >= y print(f) f
输出内容为:
$$f(x, y) = \mathopen{}\left( x \cap y, x \cup y, x \setminus y, x \mathbin{\triangle} y, x \subset y, x \subseteq y, x \supset y, x \supseteq y \mathclose{}\right)$$
不输出函数只输出表达式
@latexify.function(use_signature=False) def f(a, b, c): return (-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a) print(f) f
输出内容为:
$$ \frac{-b + \sqrt{ b^{2} – 4 a c }}{2 a}$$
此方法也可以使用 @latexify.expression装饰器进行输出:最终输出内容完全一致。
import latexify import math @latexify.expression def solve(a, b, c): return (-b + math.sqrt(b**2 - 4*a*c)) / (2*a) print(solve) solve
更加复杂的示例:矩阵计算支持:
import numpy as np @latexify.function(reduce_assignments=True, use_math_symbols=True) def transform(x, y, a, b, theta, s, t): cos_t = math.cos(theta) sin_t = math.sin(theta) scale = np.array([[a, 0, 0], [0, b, 0], [0, 0, 1]]) rotate = np.array([[cos_t, -sin_t, 0], [sin_t, cos_t, 0], [0, 0, 1]]) move = np.array([[1, 0, s], [0, 1, t], [0, 0, 1]]) return move @ rotate @ scale @ np.array([[x], [y], [1]]) print(transform) transform
输出内容为:
$$ \mathrm{transform}(x, y, a, b, \theta, s, t) = \begin{bmatrix} 1 & 0 & s \\ 0 & 1 & t \\ 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} \cos \theta & -\sin \theta & 0 \\ \sin \theta & \cos \theta & 0 \\ 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} a & 0 & 0 \\ 0 & b & 0 \\ 0 & 0 & 1 \end{bmatrix} \cdot \begin{bmatrix} x \\ y \\ 1 \end{bmatrix}$$
Latexify生成伪代码
Latexify通过装饰器 @latexify.algorithmic 可生成伪代码,示例:
@latexify.algorithmic def fib(x): if x == 0: return 0 elif x == 1: return 1 else: return fib(x-1) + fib(x-2) print(fib) fib
输出内容为:
更多示例:
@latexify.algorithmic def collatz(x): n = 0 while x > 1: n = n + 1 if x % 2 == 0: x = x // 2 else: x = 3 * x + 1 return n print(collatz) collatz