用Python优雅地写出数学表达式的LaTeX代码

用Python优雅地写出数学表达式的LaTeX代码

目录

Latexify

LaTeX

安装方法

版本要求

使用方法

实例一

实例二

实例三

实例四

实例五


Latexify

Latexify是一个Python库,它可以将数学表达式转换为LaTeX代码。通过使用latexify-py,可以将数学表达式、函数、方程和公式等等,都优雅地转换为LaTeX代码,方便人们在文档或幻灯片中以纯正的格式来呈现数学表达式。

LaTeX

音译“拉泰赫”,是一种基于ΤΕΧ的排版系统,由美国计算机学家莱斯利·兰伯特(Leslie Lamport)在20世纪80年代初期开发,利用这种格式,即使使用者没有排版和程序设计的知识也可以充分发挥由TeX所提供的强大功能,能在几天、甚至几小时内生成很多具有书籍质量的印刷品。对于生成复杂表格和数学公式,这一点表现得尤为突出。因此它非常适用于生成高印刷质量的科技和数学类文档。

安装方法

pip install latexify-py
Collecting latexify-py
  Downloading latexify_py-0.4.2-py3-none-any.whl (38 kB)
Collecting dill>=0.3.2
  Downloading dill-0.3.7-py3-none-any.whl (115 kB)
     |████████████████████████████████| 115 kB 16 kB/s
Installing collected packages: dill, latexify-py
Successfully installed dill-0.3.7 latexify-py-0.4.2

版本要求

当前版本为0.4.2,注意它要求Python版本不能是当前的最新版本3.12,但也不能低于3.7版本:

ERROR: Ignored the following versions that require a different python version: 0.0.4 Requires-Python >=3.6, <3.9; 0.0.5 Requires-Python >=3.6, <3.9; 0.0.6 Requires-Python >=3.6, <3.9; 0.0.7 Requires-Python >=3.6, <3.9; 0.1.0 Requires-Python <3.11,>=3.7; 0.1.1 Requires-Python <3.11,>=3.7; 0.2.0 Requires-Python <3.11,>=3.7; 0.2.0b1 Requires-Python <3.11,>=3.7; 0.2.0b2 Requires-Python <3.11,>=3.7; 0.3.0b1 Requires-Python <3.12,>=3.7; 0.3.1 Requires-Python <3.12,>=3.7; 0.4.0 Requires-Python <3.12,>=3.7; 0.4.1 Requires-Python <3.12,>=3.7; 0.4.2 Requires-Python <3.12,>=3.7
ERROR: Could not find a version that satisfies the requirement latexify-py (from versions: none)
ERROR: No matching distribution found for latexify-py

使用方法

实例一

先用一元二次方程的根来示例lateify的用法:

import math, latexify

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

# 输出Latex公式
print(solve)

# 输出函数的值
print(solve(1,2,1))
print(solve(1,0,-1))

输出结果:

\mathopen{}\left( \frac{-b + \sqrt{ b^{2} - 4 a c }}{2 a}, \frac{-b - \sqrt{ b^{2} - 4 a c }}{2 a} \mathclose{}\right)
(-1.0, -1.0)
(1.0, -1.0)

公式预览:

\mathopen{}\left( \frac{-b + \sqrt{ b^{2} - 4 a c }}{2 a}, \frac{-b - \sqrt{ b^{2} - 4 a c }}{2 a} \mathclose{}\right)

注:代码的关键就是在函数上标注@latexify.expression装饰器。

实例二

再举一个更复杂一点的函数例子,并导入numpy库的函数:

import latexify
import numpy as np

@latexify.function
def f(x):
    return 2*np.pi*x + np.sin(x) - np.cos(x) / (1 + np.exp(-x))

print(latex_code)

输出结果:

f(x) = 2 \mathrm{np}.\mathrm{pi} x + \sin x - \frac{\cos x}{1 + \exp \mathopen{}\left( -x \mathclose{}\right)}

公式预览:

\mathrm{f}(x) = 2 \mathrm{np}.\mathrm{pi} x + \sin x - \frac{\cos x}{1 + \exp \mathopen{}\left( -x \mathclose{}\right)}

这第二段代码标注的装饰器是 @latexify.function,与 @latexify.expression 的区别在于后者只显示函数表达式,而前者@latexify.function显示函数名(参数)=函数表达式

实例三

同理,上一个例子换成另一个装饰器,例如:

import math, latexify
a,b,c = 1,-3,2
@latexify.function
def solve1(x1):
    return (-b + math.sqrt(b**2-4*a*c))/(2*a)
@latexify.function
def solve2(x2):
    return (-b - math.sqrt(b**2-4*a*c))/(2*a)

print(solve1)
print(solve2)
print(solve1((a,b,c)))
print(solve2((a,b,c)))

输出结果:

\mathrm{solve1}(\mathrm{x1}) = \frac{-b + \sqrt{ b^{2} - 4 a c }}{2 a}
\mathrm{solve2}(\mathrm{x2}) = \frac{-b - \sqrt{ b^{2} - 4 a c }}{2 a}
2.0
1.0

公式预览:

\mathrm{solve1}(\mathrm{x1}) = \frac{-b + \sqrt{ b^{2} - 4 a c }}{2 a}

\mathrm{solve2}(\mathrm{x2}) = \frac{-b - \sqrt{ b^{2} - 4 a c }}{2 a}

实例四

斐波那契数列通项表达式

import math, latexify

@latexify.function
def Fibonacci(n):
    return (((1 + math.sqrt(5))/2)**n - ((1 - math.sqrt(5))/2)**n)/math.sqrt(5)

print(Fibonacci)

for i in range(10):
    print(int(Fibonacci(i)))

 输出结果:

\mathrm{Fibonacci}(n) = \frac{\mathopen{}\left( \frac{1 + \sqrt{ 5 }}{2} \mathclose{}\right)^{n} - \mathopen{}\left( \frac{1 - \sqrt{ 5 }}{2} \mathclose{}\right)^{n}}{\sqrt{ 5 }}
0
1
1
2
3
5
8
13
21
34

公式预览:

\mathrm{Fibonacci}(n) = \frac{\mathopen{}\left( \frac{1 + \sqrt{ 5 }}{2} \mathclose{}\right)^{n} - \mathopen{}\left( \frac{1 - \sqrt{ 5 }}{2} \mathclose{}\right)^{n}}{\sqrt{ 5 }}

网上多数文章都写成这样的格式:

\mathrm{Fibonacci}(n) = \frac{1}{\sqrt{ 5 }} \cdot \mathopen{}\left( \mathopen{}\left( \frac{1 + \sqrt{ 5 }}{2} \mathclose{}\right)^{n} - \mathopen{}\left( \frac{1 - \sqrt{ 5 }}{2} \mathclose{}\right)^{n} \mathclose{}\right) 

return语句改为以下表达式即可:

return (1/math.sqrt(5))*(((1 + math.sqrt(5))/2)**n - ((1 - math.sqrt(5))/2)**n)

实例五

分段函数,就以最简单的绝对值函数为例:

import latexify

@latexify.function
def f(x):
    return x if x>0 else (0 if x==0 else -x)

print(f)

结果输出:

f(x) = \left\{ \begin{array}{ll} x, & \mathrm{if} \ x > 0 \\ 0, & \mathrm{if} \ x = 0 \\ -x, & \mathrm{otherwise} \end{array} \right.

公式预览:

f(x) = \left\{ \begin{array}{ll} x, & \mathrm{if} \ x > 0 \\ 0, & \mathrm{if} \ x = 0 \\ -x, & \mathrm{otherwise} \end{array} \right.


用Matplotlib展示LaTex

简单的Latex表达式还能用matplotlib来展示:

import matplotlib.pyplot as plt  

latex = (r'$\mathrm{x1} = \frac{-b + \sqrt{ b^{2} - 4 a c }}{2 a}$', 
        r'$\mathrm{x2} = \frac{-b - \sqrt{ b^{2} - 4 a c }}{2 a}$',
        r'$E=mc^2$')

fig = plt.figure(num='LaTeX')
plt.rcParams['font.sans-serif'] = ['SimHei']
fonts = 24,24,60
axisy = 0.8,0.6,0.25
color = 'black','black','red'
for axis,text,font,color in zip(axisy, latex, fonts, color):
    plt.text(0.5, axis, text, fontsize=font, color=color, ha='center', va='center')
plt.title('LaTeXify输出展示')
plt.axis('off')
fig.savefig("formula.png", dpi=300) 
plt.show()

执行结果:

用Python优雅地写出数学表达式的LaTeX代码_第1张图片

显示更复杂的公式不知什么原因就报错,是字符\\和$相关的错还没搞清,有知道原因的请留言。


你可能感兴趣的:(Python,python)