本文最后更新于:2025年4月28日 下午

公式识别越来越贵了,本文基于 Simpletex API 构建了自己的公式识别前端应用,记录过程。

实现思路

  • 核心为基于 Simpletex API 的公式识别,将其封装在后端
  • 用python 的flask 构建后端
  • html 前端
  • 开机自启动
  • hexo 支持

核心实现

直接上代码

文件结构:

1
2
3
4
5
6
7
8
9
.
├── app.py
├── index_outside.html
├── lib
│   ├── __init__.py
│   └── simpletex_ocr.py
├── templates
│   └── index.html
└── uploads

Simpeltex_ocr 实现

  • simpletex_ocr.py
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
48
49
50
51
52
53
54
import datetime
import json
import requests
from random import Random
import hashlib


class SimpleTexOcr:
SIMPLETEX_APP_ID = ""
SIMPLETEX_APP_SECRET = ""

def __init__(self):
pass

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


def get_req_data(self, req_data, appid, secret):
header = {}
header["timestamp"] = str(int(datetime.datetime.now().timestamp()))
header["random-str"] = self.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

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

pass

需要填入你的 Simpletex API 的 SIMPLETEX_APP_IDSIMPLETEX_APP_SECRET

  • __init__.py
1
from .simpletex_ocr import SimpleTexOcr

后端实现

  • app.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from flask import Flask, request, render_template, jsonify
from flask_cors import CORS
from werkzeug.utils import secure_filename
from lib import SimpleTexOcr
import tempfile
from waitress import serve
import vvdutils as vv
import os

app = Flask(__name__)
CORS(app) # 允许所有域的跨域请求

# 配置文件保存路径
UPLOAD_FOLDER = 'uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
simpletex_ocr_obj = SimpleTexOcr()

@app.route('/')
def index():
return render_template('index.html')

DayDict = dict()

@app.route('/upload', methods=['POST'])
def handle_upload():
if 'image' not in request.files:
return jsonify({'error': '未接收到图片'}), 400

day_string = vv.time_string(year_month_day=True)
if day_string not in DayDict:
DayDict[day_string] = 0

file = request.files['image']

if file.filename == '':
return jsonify({'error': '空文件名'}), 400

try:
with tempfile.NamedTemporaryFile(
suffix='.jpg', # 指定扩展名
delete=False, # 关闭后不自动删除(以便后续处理)
dir=os.getenv('TEMP', '.')) as tmp_file: # 使用系统临时目录

file_bytes = file.stream.read()
tmp_file.write(file_bytes)
tmp_path = tmp_file.name # 获取系统生成的唯一路径

ocr_result = simpletex_ocr_obj.query(tmp_path)

os.unlink(tmp_path)

result_dict = dict()

try:
result_dict['latex'] = ocr_result['res']['latex']
result_dict['conf'] = ocr_result['res']['conf']
DayDict[day_string] += 1
result_dict['call_num'] = DayDict[day_string]

except Exception as e:
result_dict['latex'] = "Error:" + str(e)
result_dict['conf'] = -1
result_dict['call_num'] = DayDict[day_string]

return jsonify({
'result': result_dict
})

except Exception as e:
# 异常时确保删除临时文件
if 'tmp_path' in locals() and os.path.exists(tmp_path):
os.unlink(tmp_path)
return jsonify({'error': str(e)}), 500


if __name__ == '__main__':
# app.run(host='0.0.0.0', port=5000, debug=True)
serve(app, host='0.0.0.0', port=5000, threads=5)

默认模板

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<!DOCTYPE html>
<html>
<head>
<title>图片粘贴上传示例</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 20px auto;
padding: 20px;
}
#dropzone {
border: 2px dashed #ccc;
border-radius: 10px;
padding: 40px;
text-align: center;
color: #666;
margin: 20px 0;
}
#result {
margin-top: 20px;
white-space: pre-wrap;
}
</style>
</head>
<body>
<h2>图片粘贴上传演示</h2>

<div id="dropzone">
在此页面任意位置粘贴图片(Ctrl+V 或右键粘贴)
</div>

<div id="result"></div>

<script>
// 监听粘贴事件
document.addEventListener('paste', async (e) => {
const items = e.clipboardData.items;

for (let item of items) {
if (item.type.startsWith('image')) {
const blob = item.getAsFile();
await uploadImage(blob);
}
}
});

// 上传图片到后端
async function uploadImage(blob) {
const formData = new FormData();
formData.append('image', blob, 'pasted-image.png');

try {
const response = await fetch('/upload', {
method: 'POST',
body: formData
});

const result = await response.json();
document.getElementById('result').textContent =
`上传成功!后端接收信息:\n${JSON.stringify(result, null, 2)}`;

} catch (error) {
console.error('上传失败:', error);
document.getElementById('result').textContent = '上传失败,请检查控制台';
}
}
</script>
</body>
</html>

适合本地调试使用

  • 使用示例

外部调用

  • index_outside.html
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>图片公式识别系统</title>
<!-- 引入 KaTeX 数学公式渲染 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css">
<style>
:root {
--primary-color: #2196F3;
--success-color: #4CAF50;
--error-color: #f44336;
--border-radius: 8px;
}

body {
font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
max-width: 800px;
margin: 2rem auto;
padding: 0 1rem;
line-height: 1.6;
color: #333;
}

.container {
background: #fff;
padding: 2rem;
border-radius: var(--border-radius);
box-shadow: 0 2px 15px rgba(0, 0, 0, 0.1);
}

#dropzone {
border: 2px dashed #ccc;
border-radius: var(--border-radius);
padding: 3rem 1rem;
text-align: center;
transition: all 0.3s ease;
background: #f8f9fa;
cursor: pointer;
position: relative;
min-height: 200px;
display: flex;
align-items: center;
justify-content: center;
}

#dropzone:hover,
#dropzone.dragover {
border-color: var(--primary-color);
background: rgba(33, 150, 243, 0.05);
}

.upload-icon {
font-size: 3rem;
color: var(--primary-color);
margin-bottom: 1rem;
}

.result-card {
margin-top: 2rem;
padding: 1.5rem;
border-radius: var(--border-radius);
background: #f8f9fa;
display: none;
}

.result-title {
margin: 0 0 1rem;
color: var(--primary-color);
font-size: 1.2rem;
}

.latex-output {
font-size: 1.4rem;
padding: 1rem;
background: white;
border-radius: 4px;
text-align: center;
overflow-x: auto;
}

.confidence {
margin-top: 1rem;
color: #666;
font-size: 0.9rem;
}
.called-today {
margin-top: 1rem;
color: #666;
font-size: 0.9rem;
}
.loading {
display: none;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(255, 255, 255, 0.9);
align-items: center;
justify-content: center;
flex-direction: column;
}

.spinner {
width: 40px;
height: 40px;
border: 4px solid #f3f3f3;
border-top: 4px solid var(--primary-color);
border-radius: 50%;
animation: spin 1s linear infinite;
}

@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}

.error-message {
color: var(--error-color);
padding: 1rem;
margin-top: 1rem;
border-radius: 4px;
background: #ffe6e6;
display: none;
}

.latex-source-card {
margin-top: 1.5rem;
border: 1px solid #e0e0e0;
border-radius: 8px;
overflow: hidden;
}

.source-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 12px;
background: #f5f5f5;
border-bottom: 1px solid #e0e0e0;
}

.latex-raw {
padding: 1rem;
margin: 0;
background: white;
font-family: 'Courier New', monospace;
white-space: pre-wrap;
word-break: break-all;
max-height: 200px;
overflow-y: auto;
}

.copy-button {
background: var(--primary-color);
color: white;
border: none;
padding: 6px 12px;
border-radius: 4px;
cursor: pointer;
transition: background 0.2s;
}

.copy-button:hover {
background: #1976d2;
}

.copy-button.copied {
background: var(--success-color);
}
</style>
</head>
<body>
<div class="container">
<h1>图片公式识别系统</h1>

<div id="dropzone">
<div class="upload-icon">📷</div>
<div>
<p>直接粘贴图片(Ctrl+V)或点击选择文件</p>
<p class="text-muted">支持 PNG/JPG 格式,建议公式图片尺寸小于 2000px</p>
</div>
<div class="loading">
<div class="spinner"></div>
<p>正在识别中...</p>
</div>
</div>

<input type="file" id="fileInput" hidden accept="image/*">

<div class="result-card">
<h3 class="result-title">识别结果</h3>
<div class="latex-output"></div>
<div class="latex-source-card">
<div class="source-header">
<span>LaTeX 源代码</span>
<button class="copy-button" onclick="copyLatex()">
📋 复制
</button>
</div>
<pre class="latex-raw" id="latexRaw"></pre>
</div>
<div class="confidence">置信度: <span class="confidence-value"></span></div>
<div class="called-today">今日调用: <span class="called-today-value"></span></div>
</div>

<div class="error-message"></div>
</div>

<script src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.js"></script>
<script>
const dropzone = document.getElementById('dropzone');
const fileInput = document.getElementById('fileInput');
const BASE_URL = 'https://your_link';

// 粘贴事件处理
document.addEventListener('paste', handleImage);

// 点击事件处理
dropzone.addEventListener('click', () => fileInput.click());

// 文件选择事件
fileInput.addEventListener('change', (e) => {
if (e.target.files[0]) handleImage(e.target.files[0]);
});

async function handleImage(event) {
let blob;
if (event instanceof ClipboardEvent) {
const items = event.clipboardData.items;
for (let item of items) {
if (item.type.startsWith('image')) {
blob = item.getAsFile();
break;
}
}
} else {
blob = event;
}

if (!blob) return;

showLoading(true);
clearError();

try {
const formData = new FormData();
formData.append('image', blob, 'pasted-image.png');

const response = await fetch(`${BASE_URL}/upload`, {
method: 'POST',
body: formData,
mode: 'cors'
});

if (!response.ok) throw new Error(`HTTP 错误! 状态码: ${response.status}`);
const { result, error } = await response.json();

if (error) throw new Error(error);
if (!result.latex) throw new Error('未能识别到公式');

showResult(result);
} catch (error) {
showError(error.message);
} finally {
showLoading(false);
}
}

function showResult({ latex, conf, call_num }) {
const resultCard = document.querySelector('.result-card');
const latexOutput = document.querySelector('.latex-output');
const confidenceValue = document.querySelector('.confidence-value');
const callnumValue = document.querySelector('.called-today-value');
const latexRaw = document.getElementById('latexRaw');

try {
// 渲染公式
katex.render(latex, latexOutput, {
throwOnError: false,
displayMode: true
});

// 显示原始代码(处理转义字符)
const cleanedLatex = latex.replace(/\\\\/g, '\\');
latexRaw.textContent = cleanedLatex;
latexRaw.dataset.raw = cleanedLatex; // 存储原始数据

} catch (e) {
latexOutput.textContent = latex;
}

confidenceValue.textContent = (conf * 100).toFixed(1) + '%';
callnumValue.textContent = call_num;
resultCard.style.display = 'block';
}

function showLoading(show) {
dropzone.querySelector('.loading').style.display = show ? 'flex' : 'none';
}

function showError(message) {
const errorEl = document.querySelector('.error-message');
errorEl.textContent = `错误: ${message}`;
errorEl.style.display = 'block';
}

function clearError() {
document.querySelector('.error-message').style.display = 'none';
}

function copyLatex() {
const button = document.querySelector('.copy-button');
const raw = document.getElementById('latexRaw').dataset.raw;

navigator.clipboard.writeText(raw).then(() => {
button.classList.add('copied');
button.textContent = '✅ 已复制';

setTimeout(() => {
button.classList.remove('copied');
button.textContent = '📋 复制';
}, 2000);
}).catch(err => {
console.error('复制失败:', err);
alert('自动复制失败,请手动选择文本复制');
});
}

// 新增双击复制功能
document.getElementById('latexRaw').addEventListener('dblclick', copyLatex);
</script>
</body>
</html>
  • BASE_URL = 'https://your_link'; 需要替换为你自己的链接
  • 使用示例:

暂时免费开放出来供大家使用,调用多了我再藏起来 …

加入 hexo

需要开机自启动,Nginx 映射(略),加入 fluid 主题

开机自启动

创建 /lib/systemd/system/formular-reco.service 文件,写入:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[Unit]
Description = Service to support formular reco for www.zywvvd.com
After = network.target

[Service]
ExecStart = /home/vvd/anaconda3/bin/python app.py
WorkingDirectory = /home/vvd/programs/formular_reco
StandardOutput = inherit
StandardError = inherit
Restart = always
User = vvd

[Install]
WantedBy=multi-user.target

开机自启:

1
sudo systemctl enable formular-reco.service

加入 fluid

创建 Hexo/source/navigates/formular_reco/index.md 文件

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
---
title: 图片公式识别
date: 2025-04-25 22:11:24
comment: 'waline'
---

<!-- 引入 KaTeX 数学公式渲染 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css">

<style>
:root {
--primary-color: #2196F3;
--success-color: #4CAF50;
--error-color: #f44336;
--border-radius: 8px;
}
.formular_container {
background: #fff;
padding: 2rem;
border-radius: var(--border-radius);
box-shadow: 0 2px 15px rgba(0, 0, 0, 0.1);
}
#dropzone {
border: 2px dashed #ccc;
border-radius: var(--border-radius);
padding: 3rem 1rem;
text-align: center;
transition: all 0.3s ease;
background: #f8f9fa;
cursor: pointer;
position: relative;
min-height: 200px;
display: flex;
align-items: center;
justify-content: center;
}
#dropzone:hover,
#dropzone.dragover {
border-color: var(--primary-color);
background: rgba(33, 150, 243, 0.05);
}
.upload-icon {
font-size: 3rem;
color: var(--primary-color);
margin-bottom: 1rem;
}
.result-card {
margin-top: 2rem;
padding: 1.5rem;
border-radius: var(--border-radius);
background: #f8f9fa;
display: none;
}
.result-title {
margin: 0 0 1rem;
color: var(--primary-color);
font-size: 1.2rem;
}
.latex-output {
font-size: 1.4rem;
padding: 1rem;
background: white;
border-radius: 4px;
text-align: center;
overflow-x: auto;
}
.confidence {
margin-top: 1rem;
color: #666;
font-size: 0.9rem;
}
.called-today {
margin-top: 1rem;
color: #666;
font-size: 0.9rem;
}
.loading {
display: none;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(255, 255, 255, 0.9);
align-items: center;
justify-content: center;
flex-direction: column;
}
.spinner {
width: 40px;
height: 40px;
border: 4px solid #f3f3f3;
border-top: 4px solid var(--primary-color);
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.error-message {
color: var(--error-color);
padding: 1rem;
margin-top: 1rem;
border-radius: 4px;
background: #ffe6e6;
display: none;
}
.latex-source-card {
margin-top: 1.5rem;
border: 1px solid #e0e0e0;
border-radius: 8px;
overflow: hidden;
}
.source-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 12px;
background: #f5f5f5;
border-bottom: 1px solid #e0e0e0;
}
.latex-raw {
padding: 1rem;
margin: 0;
background: white;
font-family: 'Courier New', monospace;
white-space: pre-wrap;
word-break: break-all;
max-height: 200px;
overflow-y: auto;
}
.copy-button {
background: var(--primary-color);
color: white;
border: none;
padding: 6px 12px;
border-radius: 4px;
cursor: pointer;
transition: background 0.2s;
}
.copy-button:hover {
background: #1976d2;
}
.copy-button.copied {
background: var(--success-color);
}
</style>
<div class="container">
<h5>图片公式识别系统</h5>
<p></p>
</p>
<div id="dropzone">
<div class="upload-icon">📷</div>
<div>
<p>直接粘贴图片(Ctrl+V)或点击选择文件</p>
<p class="text-muted">支持 PNG/JPG 格式,建议公式图片尺寸小于 2000px</p>
</div>
<div class="loading">
<div class="spinner"></div>
<p>正在识别中...</p>
</div>
</div>
<input type="file" id="fileInput" hidden accept="image/*">
<div class="result-card">
<h3 class="result-title">识别结果</h3>
<div class="latex-output"></div>
<div class="latex-source-card">
<div class="source-header">
<span>LaTeX 源代码</span>
<button class="copy-button" onclick="copyLatex()">
📋 复制
</button>
</div>
<pre class="latex-raw" id="latexRaw"></pre>
</div>
<div class="confidence">置信度: <span class="confidence-value"></span></div>
<div class="called-today">今日调用: <span class="called-today-value"></span></div>
</div>
<div class="error-message"></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.js"></script>
<script>
const dropzone = document.getElementById('dropzone');
const fileInput = document.getElementById('fileInput');
const BASE_URL = 'https://your-link';
// 粘贴事件处理
document.addEventListener('paste', handleImage);
// 点击事件处理
dropzone.addEventListener('click', () => fileInput.click());
// 文件选择事件
fileInput.addEventListener('change', (e) => {
if (e.target.files[0]) handleImage(e.target.files[0]);
});
async function handleImage(event) {
let blob;
if (event instanceof ClipboardEvent) {
const items = event.clipboardData.items;
for (let item of items) {
if (item.type.startsWith('image')) {
blob = item.getAsFile();
break;
}
}
} else {
blob = event;
}
if (!blob) return;
showLoading(true);
clearError();
try {
const formData = new FormData();
formData.append('image', blob, 'pasted-image.png');
const response = await fetch(`${BASE_URL}/upload`, {
method: 'POST',
body: formData,
mode: 'cors'
});
if (!response.ok) throw new Error(`HTTP 错误! 状态码: ${response.status}`);
const { result, error } = await response.json();
if (error) throw new Error(error);
if (!result.latex) throw new Error('未能识别到公式');
showResult(result);
} catch (error) {
showError(error.message);
} finally {
showLoading(false);
}
}
function showResult({ latex, conf, call_num }) {
const resultCard = document.querySelector('.result-card');
const latexOutput = document.querySelector('.latex-output');
const confidenceValue = document.querySelector('.confidence-value');
const callnumValue = document.querySelector('.called-today-value');
const latexRaw = document.getElementById('latexRaw');
try {
// 渲染公式
katex.render(latex, latexOutput, {
throwOnError: false,
displayMode: true
});
// 显示原始代码(处理转义字符)
const cleanedLatex = latex.replace(/\\\\/g, '\\');
latexRaw.textContent = cleanedLatex;
latexRaw.dataset.raw = cleanedLatex; // 存储原始数据
} catch (e) {
latexOutput.textContent = latex;
}
confidenceValue.textContent = (conf * 100).toFixed(1) + '%';
callnumValue.textContent = call_num;
resultCard.style.display = 'block';
}
function showLoading(show) {
dropzone.querySelector('.loading').style.display = show ? 'flex' : 'none';
}
function showError(message) {
const errorEl = document.querySelector('.error-message');
errorEl.textContent = `错误: ${message}`;
errorEl.style.display = 'block';
}
function clearError() {
document.querySelector('.error-message').style.display = 'none';
}
function copyLatex() {
const button = document.querySelector('.copy-button');
const raw = document.getElementById('latexRaw').dataset.raw;
navigator.clipboard.writeText(raw).then(() => {
button.classList.add('copied');
button.textContent = '✅ 已复制';
setTimeout(() => {
button.classList.remove('copied');
button.textContent = '📋 复制';
}, 2000);
}).catch(err => {
console.error('复制失败:', err);
alert('自动复制失败,请手动选择文本复制');
});
}
document.getElementById('latexRaw').addEventListener('dblclick', copyLatex);
</script>


const BASE_URL = 'https://your-link'; 需要换为你自己的 地址

  • _config.fluid.yml 文件中加入到 menu:

    1
    { key: "formular_reco", link: "/navigates/formular_reco", icon: "iconfont icon-a-checkformula" , event_key : "Navigation-Formular-Reco" },
  • Hexo/source/_data/languages/zh-CN.yml 中添加

    1
    2
    3
    4
    formular_reco:
    menu: '公式识别'
    title: '公式识别'
    subtitle: '公式识别'
  • 示例效果:

参考资料



文章链接:
https://www.zywvvd.com/notes/hexo/theme/fluid/my-formular-reco/my-formular-reco/


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

微信二维码

微信支付

支付宝二维码

支付宝支付

Fluid -46- 基于 Simpletex API 构建公式识别页面
https://www.zywvvd.com/notes/hexo/theme/fluid/my-formular-reco/my-formular-reco/
作者
Yiwei Zhang
发布于
2025年4月26日
许可协议