本文最后更新于:2025年4月22日 晚上

写博客时候经常需要将图片公式转换为 Latex 格式,已知有几个平台,感觉比较贵,我试用了几个,本文记录相关信息。

简介

公式识别就是将图片转化为 Latex 文本,例如:

对应 Latex:

1
\alpha_i=(1-\exp(-\sigma_i\delta_i))\text{ and }T_i=\prod_{j=1}^{i-1}(1-\alpha_i).

Markdown 渲染公式:
$$
\alpha_i=(1-\exp(-\sigma_i\delta_i))\text{ and }T_i=\prod_{j=1}^{i-1}(1-\alpha_i).
$$
公式识别就是这个识别过程。

常用平台

名称 简介 链接
mathpix 早期有名的公式识别工具,又快又准,现在收费了 https://mathpix.com/
LaTeX-OCR Github 项目,需要有自己的服务器和算力 https://github.com/lukas-blecher/LaTeX-OCR
quicker 一款指上工具箱,其中包含公式识别的调用插件,客户端仅有 Windows版本 https://getquicker.net/
simpletex 新晋的公式识别工具,很好用,现在也收费了 https://simpletex.cn/

API 平台

名称 简介 链接
simpletex 识别准确,当前还在测试,价格不贵,性价比高 https://simpletex.cn/api
mathpix 识别准确,价格比 Simpletxt 贵很多 https://mathpix.com/pricing/api
科大讯飞公式识别 API 当前在内测,每天500次免费调用,但是效果不好 https://www.xfyun.cn/doc/words/formula-discern/API.html
百度公式识别 API **该接口的公有云服务即将下线,**感觉还处在业务调整阶段 https://ai.baidu.com/ai-doc/OCR/Ok3h7xxva
阿里印刷体数学公式识别 API 没有测试,相信阿里的结果应该是准的,就是价格不便宜 https://help.aliyun.com/zh/ocr/developer-reference/api-ocr-api-2021-07-07-recognizeeduformula

Simpletex

对比下来当前还是 Simpletex 比较合适,20元开启 API 功能,之后根据不同模型标注不同价格:

  • 轻量公式识别模型API

服务价格(轻量)

月调用量(次) 价格(元/次)
<1000 免费
1000-5000 0.04
>5000 0.01

API调用方法

接口地址:https://server.simpletex.cn/api/latex_ocr_turbo

  • 标准公式识别模型API

服务价格(标准)

月调用量(次) 价格(元/次)
<1000 免费
1000-5000 0.05
>5000 0.02

其中,每月1号凌晨0时清空记录,按阶梯次进行计费。轻量模型每日赠送2000次免费调用,标准模型赠送500次免费调用。

这里文档有时写的每日有的每月,我也没搞清具体是哪个,感觉每月的可能性大一点。

API 调用方法

接口地址:https://server.simpletex.cn/api/latex_ocr

调用方法

  • 创建令牌(需要充值20元)

  • 创建应用

  • 示例代码

    填入:SIMPLETEX_APP_ID

    填入:SIMPLETEX_APP_SECRET

    准备图片:./image/02.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import datetime
import json
import requests
from random import Random
import hashlib

SIMPLETEX_APP_ID = ""
SIMPLETEX_APP_SECRET = ""

def random_str(randomlength=16):
str = ''
chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
length = len(chars) - 1
random = Random()
for i in range(randomlength):
str += chars[random.randint(0, length)]
return str


def get_req_data(req_data, appid, secret):
header = {}
header["timestamp"] = str(int(datetime.datetime.now().timestamp()))
header["random-str"] = random_str(16)
header["app-id"] = appid
pre_sign_string = ""
sorted_keys = list(req_data.keys()) + list(header)
sorted_keys.sort()
for key in sorted_keys:
if pre_sign_string:
pre_sign_string += "&"
if key in header:
pre_sign_string += key + "=" + str(header[key])
else:
pre_sign_string += key + "=" + str(req_data[key])

pre_sign_string += "&secret=" + secret
header["sign"] = hashlib.md5(pre_sign_string.encode()).hexdigest()
return header, req_data


img_file = {"file": open("./image/02.png", 'rb')}
data = { } # 请求参数数据(非文件型参数),视情况填入,可以参考各个接口的参数说明
header, data = get_req_data(data, SIMPLETEX_APP_ID, SIMPLETEX_APP_SECRET)
res = requests.post("https://server.simpletex.cn/api/latex_ocr", files=img_file, data=data, headers=header)

print(json.loads(res.text))
pass

输出结果:

1
{'status': True, 'res': {'latex': '\\alpha_i=(1-\\exp(-\\sigma_i\\delta_i))\\text{ and }T_i=\\prod_{j=1}^{i-1}(1-\\alpha_i).', 'conf': 0.9386832118034363}, 'request_id': 'tr_17453305716849140986868559936'}

参考资料



文章链接:
https://www.zywvvd.com/notes/tools/simpletex-api/simpletex-api/


“觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭”

微信二维码

微信支付

支付宝二维码

支付宝支付

公式识别 API 简介 -- Simpletex 使用
https://www.zywvvd.com/notes/tools/simpletex-api/simpletex-api/
作者
Yiwei Zhang
发布于
2025年4月21日
许可协议