抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

GitHub传送门:

安装

使用 pip 安装:

1
pip install latexify-py

使用

@latexify.function

使用 @latexify.function 包装函数:

1
2
3
4
5
6
7
8
9
import latexify
import math


@latexify.function
def solve(a, b, c):
return (-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a)

solve # 打印渲染后的公式效果

直接输出渲染完成的数学公式:

solve(a,b,c)=b+b24ac2a\mathrm{solve}(a, b, c) = \frac{-b + \sqrt{ b^{2} - 4 a c }}{2 a}

也可以打印 字符串格式 的latex公式代码:

1
print(solve)  # 直接打印latex字符串

输出:

1
\mathrm{solve}(a, b, c) = \frac{-b + \sqrt{ b^{2} - 4 a c }}{2 a}

@latexify.expression

使用 @latexify.expression 包装函数,使用方法与 @latexify.function 一致,但输出的公式不包含函数名 solve() 部分:

1
2
3
4
5
6
7
8
9
10
import latexify
import math


@latexify.expression
def solve(a, b, c):
return (-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a)

print(solve) # 直接打印latex字符串
solve # 打印渲染后的公式效果

输出:

1
\frac{-b + \sqrt{ b^{2} - 4 a c }}{2 a}

b+b24ac2a\frac{-b + \sqrt{ b^{2} - 4 a c }}{2 a}


latexify.get_latex

使用 latexify.get_latex 方法直接返回与给定函数对应的LaTeX表达式:

1
2
3
4
def solve(a, b, c):
return (-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a)

latexify.get_latex(solve)

输出:

1
'\\mathrm{solve}(a, b, c) = \\frac{-b + \\sqrt{ b^{2} - 4 a c }}{2 a}'

@latexify.algorithmic

使用 @latexify.expression 包装函数,将函数转换为伪代码:

1
2
3
4
5
6
7
8
9
10
11
12

@latexify.algorithmic
def fib(x):
if x == 0:
return 0
elif x == 1:
return 1
else:
return fib(x-1) + fib(x-2)

fib

function fib(x)if x=0return 0elseif x=1return 1elsereturn fib(x1)+fib(x2)end ifend ifend function\begin{array}{l} \mathbf{function} \ \mathrm{fib}(x) \\ \hspace{1em} \mathbf{if} \ x = 0 \\ \hspace{2em} \mathbf{return} \ 0 \\ \hspace{1em} \mathbf{else} \\ \hspace{2em} \mathbf{if} \ x = 1 \\ \hspace{3em} \mathbf{return} \ 1 \\ \hspace{2em} \mathbf{else} \\ \hspace{3em} \mathbf{return} \ \mathrm{fib} \mathopen{}\left( x - 1 \mathclose{}\right) + \mathrm{fib} \mathopen{}\left( x - 2 \mathclose{}\right) \\ \hspace{2em} \mathbf{end \ if} \\ \hspace{1em} \mathbf{end \ if} \\ \mathbf{end \ function} \end{array}

1
2
3
4
5
6
7
8
9
10
11
12
@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

collatz

function collatz(x)n0while x>1nn+1if x%2=0xx2elsex3x+1end ifend whilereturn nend function\begin{array}{l} \mathbf{function} \ \mathrm{collatz}(x) \\ \hspace{1em} n \gets 0 \\ \hspace{1em} \mathbf{while} \ x > 1 \\ \hspace{2em} n \gets n + 1 \\ \hspace{2em} \mathbf{if} \ x \mathbin{\%} 2 = 0 \\ \hspace{3em} x \gets \left\lfloor\frac{x}{2}\right\rfloor \\ \hspace{2em} \mathbf{else} \\ \hspace{3em} x \gets 3 x + 1 \\ \hspace{2em} \mathbf{end \ if} \\ \hspace{1em} \mathbf{end \ while} \\ \hspace{1em} \mathbf{return} \ n \\ \mathbf{end \ function} \end{array}


参数

identifiers

类型:字典

作用:编写python代码时,函数名、变量名常采用英文词汇,而公式中往往采用简单的单个字母表示,参数 identifiers 支持传入一个字典,传入你想要替换的键值对,进而调整公式的输出。

1
2
3
4
5
6
7
8
9
10
11
identifiers = {
"my_function": "f", # 用 f 代替 my_function
"my_inner_function": "g", # 用 g 代替 my_inner_function
"my_argument": "x", # 用 x 代替 my_argument
}

@latexify.function(identifiers=identifiers)
def my_function(my_argument):
return my_inner_function(my_argument)

my_function

输出:

f(x)=g(x)\displaystyle f(x) = g \mathopen{}\left( x \mathclose{}\right)

如果不传参数,直接输出:

1
2
3
@latexify.function
def my_function(my_argument):
return my_inner_function(my_argument)

my_function(my_argument)=my_inner_function(my_argument) \displaystyle \mathrm{my\_function}(\mathrm{my\_argument}) = \mathrm{my\_inner\_function} \mathopen{}\left( \mathrm{my\_argument} \mathclose{}\right)


reduce_assignments

类型:bool

作用:消去中间变量,只呈现最简单的公式表达形式,例如在下面的函数中:

1
2
3
4
5
def f(a, b, c):
discriminant = b**2 - 4 * a * c
numerator = -b + math.sqrt(discriminant)
denominator = 2 * a
return numerator / denominator

如果直接输出,会逐行输出公式,得到以下结果:

discriminant=b24acnumerator=b+discriminantdenominator=2af(a,b,c)=numeratordenominator\displaystyle \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}

将参数 reduce_assignments 设为 True 时则会简化输出结果:

1
2
3
4
5
6
7
8
@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

f

f(a,b,c)=b+b24ac2a\displaystyle f(a, b, c) = \frac{-b + \sqrt{ b^{2} - 4 a c }}{2 a}

官方文档中指出,latexify 的当前版本仅识别赋值语句,分析具有其他控制流的函数可能会引发错误。


use_math_symbols

类型:bool

作用:将具有符号名称的变量转换为 LaTeX 符号,如 alpha 转换为 α\alpha

1
2
3
4
5
@latexify.function(use_math_symbols=True)
def greek(alpha, beta, gamma, Omega):
return alpha * beta + math.gamma(gamma) + Omega

greek

输出:

greek(α,β,γ,Ω)=αβ+Γ(γ)+Ω\displaystyle \mathrm{greek}(\alpha, \beta, \gamma, \Omega) = \alpha \beta + \Gamma \mathopen{}\left( \gamma \mathclose{}\right) + \Omega


use_set_symbols

类型:bool

作用:识别集合的二元运算符。

1
2
3
4
5
@latexify.function
def f(x, y):
return x & y, x | y, x - y, x ^ y, x < y, x <= y, x > y, x >= y

f

输出:

f(x,y)=(x&y,xy,xy,xy,x<y,xy,x>y,xy)\displaystyle f(x, y) = \mathopen{}\left( x \mathbin{\&} y, x \mathbin{|} y, x - y, x \oplus y, x < y, x \le y, x > y, x \ge y \mathclose{}\right)

设置参数后:

1
2
3
4
5
@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

f

输出:

f(x,y)=(xy,xy,xy,xy,xy,xy,xy,xy)\displaystyle 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)


use_signature

类型:bool

作用:是否输出函数名,默认 latexify.function 使用 True,而 latexify.expression 使用 False

1
2
3
4
5
@latexify.function(use_signature=False)  # 不带函数名
def f(a, b, c):
return (-b + math.sqrt(b**2 - 4 * a * c)) / (2 * a)

f

输出:

b+b24ac2a\displaystyle \frac{-b + \sqrt{ b^{2} - 4 a c }}{2 a}


更多案例

1
2
3
4
5
6
7
8
@latexify.function
def sinc(x):
if x == 0:
return 1
else:
return math.sin(x) / x

sinc

sinc(x)={1,if x=0sinxx,otherwise\displaystyle \mathrm{sinc}(x) = \left\{ \begin{array}{ll} 1, & \mathrm{if} \ x = 0 \\ \frac{\sin x}{x}, & \mathrm{otherwise} \end{array} \right.

1
2
3
4
5
6
7
8
9
10
11
12
# Elif or nested else-if are unrolled.
@latexify.function
def fib(x):
if x == 0:
return 0
elif x == 1:
return 1
else:
return fib(x-1) + fib(x-2)

fib

fib(x)={0,if x=01,if x=1fib(x1)+fib(x2),otherwise\displaystyle \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.

1
2
3
4
5
6
7
8
9
10
11
12
# Matrix support.
@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]])

transform

transform(x,y,a,b,θ,s,t)=[10s01t001][cosθsinθ0sinθcosθ0001][a000b0001][xy1]\displaystyle \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}

评论