【Python】用Python生成LaTeX公式!
公众号:尤而小屋
作者:Peter
编辑:Peter
本文介绍了一种方法: 如何使用Python代码来生成LaTeX数学公式 。
安装Latexify
这个工具
Google
出品的,
github
地址:https://github.com/google/latexify_py

使用之前需要安装Latexify:
pip install latexify-py

使用Latexify
import math
import latexify # 先导入
二次方程求根公式
@latexify.with_latex # 加上一个装饰器(必须)
def solve1(a, b, c):
return (-b + math.sqrt(b**2 - 4*a*c)) / (2*a)
def solve2(a, b, c):
return (-b - math.sqrt(b**2 - 4*a*c)) / (2*a) 1
solve1
如果是Markdown敲击的话,solve1源码是:

带入数值进行求解:
solve1(5,4,0)
0.0
Sigmoid函数
@latexify.with_latex
def sigmoid(x):
return 1 / (1 + e**(-x))
sigmoid
分段函数
@latexify.with_latex
def f(x):
if x < 0:
return 0
else:
return 5*x
f
带入不同的数值:
f(-2) # 带入负数
0
f(2) # 带入正数
10
三角函数公式

@latexify.with_latex
def f(a,b):
return math.sin(a - b) == math.sin(a) * math.cos(b) - math.cos(a) * math.sin(b)
f
Markdown的源码为:

希腊公式
包含希腊字母的公式也能编码:
@latexify.with_latex
def greek(alpha, beta, gamma, Omega):
return alpha * beta + math.gamma(gamma) + Omega
greek
greek(𝛼,𝛽,𝛾,Ω)≜𝛼𝛽+Γ(𝛾)+Ω

往期 精彩 回顾
- 适合初学者入门人工智能的路线及资料下载
- (图文+视频)机器学习入门系列下载
- 机器学习及深度学习笔记等资料打印
- 《统计学习方法》的代码复现专辑
- 机器学习交流qq群955171419,加入微信群请 扫码
评论