Install
openclaw skills install formula-pdf将包含数学公式的内容渲染为PDF文档。使用HTML+MathJax渲染公式,通过Edge无头模式转为PDF。当用户要求整理文档、生成报告、制作笔记且内容含有公式时触发。不要用Word导出(公式会变成代码),必须用此方法。
openclaw skills install formula-pdf当用户说 "整理成文档"、"发给我"、"弄成PDF" 且内容含有数学公式时触发。 必须优先使用此方法,不要使用Word导出。
内容优先写中文(文件名用英文),公式用MathJax语法。
写文件的正确方式(重要!):
write 工具直接写HTML(含大量引号和反斜杠时参数会解析失败)@'
<html>
<head>
<meta charset="UTF-8">
<script>MathJax={tex:{inlineMath:[['$','$']],displayMath:[['$$','$$']]}};</script>
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>
</head>
<body>
<!-- 内容 -->
</body>
</html>
'@ | Out-File -FilePath filename.html -Encoding UTF8
公式写法:
$E = mc^2$$$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$$\frac 不是 frac使用Edge无头模式打印为PDF。不要杀Edge进程,可直接共存运行。
& 'edge' `
--headless=new `
--virtual-time-budget=30000 `
--print-to-pdf="output.pdf" `
--no-margins `
"file:///C:/path/to/input.html"
关键参数解释:
--headless=new:无界面模式--virtual-time-budget=30000:等待MathJax加载和渲染的时间(毫秒)--print-to-pdf:输出PDF路径--no-margins:无边距"file:///":必须用文件协议,路径用双引号包起来用pymupdf检查PDF文本中是否含有原始LaTeX代码:
import fitz
doc = fitz.open(pdf_path)
text = ""
for page in doc:
text += page.get_text()
# 如果含有这些关键词,说明公式没渲染
latex_patterns = [r'\frac', r'\sqrt', r'\lim', r'\sum', r'\int', r'\dfrac', r'$$']
for p in latex_patterns:
if p in text:
print("警告: 公式未渲染,含LaTeX原始代码")
print("验证完成")
用message工具将PDF发送给用户:
message(action="send", filePath="PDF路径", message="公式已渲染")
scripts/html_to_pdf.py:一键转换脚本,支持参数assets/template.html:HTML模板,可直接用作起点--virtual-time-budget 的值(如50000)font-family: 'Microsoft YaHei', 'Segoe UI', sans-serif;formula-pdf/
├── SKILL.md
├── scripts/
│ └── html_to_pdf.py # 一键转换工具
├── references/ # (可选)更多使用参考
└── assets/
└── template.html # HTML模板