Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,21 +33,34 @@ plt.rcParams.update({
|
|
| 33 |
|
| 34 |
def show_latex(formula):
|
| 35 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
# 创建图像
|
| 37 |
fig = plt.figure()
|
| 38 |
plt.axis("off")
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# 保存到内存缓冲区
|
| 44 |
buf = io.BytesIO()
|
| 45 |
-
plt.savefig(buf, format="png", bbox_inches="tight", dpi=
|
| 46 |
plt.close(fig)
|
| 47 |
buf.seek(0)
|
| 48 |
return Image.open(buf)
|
| 49 |
except Exception as e:
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
def search(query,enable_latex):
|
| 53 |
output_results = output_collection.get(
|
|
|
|
| 33 |
|
| 34 |
def show_latex(formula):
|
| 35 |
try:
|
| 36 |
+
# 配置LaTeX渲染参数
|
| 37 |
+
plt.rcParams.update({
|
| 38 |
+
"text.usetex": True, # 启用LaTeX渲染
|
| 39 |
+
"font.family": "serif", # 使用衬线字体
|
| 40 |
+
"font.serif": ["Computer Modern"], # LaTeX默认字体
|
| 41 |
+
"axes.labelsize": 24, # 保持字体大小一致
|
| 42 |
+
})
|
| 43 |
+
|
| 44 |
# 创建图像
|
| 45 |
fig = plt.figure()
|
| 46 |
plt.axis("off")
|
| 47 |
+
|
| 48 |
+
# 使用LaTeX渲染公式(添加$符号包裹公式)
|
| 49 |
+
plt.text(0.5, 0.5, f"${remove1(formula)}$",
|
| 50 |
+
ha="center",
|
| 51 |
+
va="center",
|
| 52 |
+
fontsize=24,
|
| 53 |
+
math_fontfamily='cm') # 明确指定数学字体
|
| 54 |
|
| 55 |
# 保存到内存缓冲区
|
| 56 |
buf = io.BytesIO()
|
| 57 |
+
plt.savefig(buf, format="png", bbox_inches="tight", pad_inches=0.1, dpi=300)
|
| 58 |
plt.close(fig)
|
| 59 |
buf.seek(0)
|
| 60 |
return Image.open(buf)
|
| 61 |
except Exception as e:
|
| 62 |
+
# 出错时使用简单文本提示
|
| 63 |
+
return show_latex(r"\text{Sorry, LaTeX rendering failed. Please check your formula syntax.}")
|
| 64 |
|
| 65 |
def search(query,enable_latex):
|
| 66 |
output_results = output_collection.get(
|