math-doc Skill
Purpose
Generate professional mathematical documents for any scenario — lecture notes, exercise sets, summaries, proofs, reports, papers, or anything formula-heavy. Output can be .docx, LaTeX, or Markdown.
Trigger
Use this skill when:
- user requests any mathematical document (notes, exercises, summaries, proofs, reports)
- user requests a formula-heavy Word document
- user requests formula formatting or OMML conversion
- user requests Markdown/LaTeX to .docx generation
- user requests checking the format of a generated math document
Decision Priority
- User explicit requirement
- Existing project/file/template constraints
- Domain skill rules
- General best practice
Working Mode
Classify the request:
- A. Creation: full pipeline, build from scratch.
- B. Modification: inspect structure first, then modify.
- C. Analysis: explain document structure or formula, do not regenerate.
- D. Debugging: inspect generated file, find root cause, then fix.
- E. Research: gather references or conventions before output.
Ask only when missing information affects the result. Otherwise use reasonable defaults.
Task Router
帮我写证明、写讲义、整理笔记、出习题集 -> math-doc -> ask 需要Word吗?; if yes, use docx pipeline.
帮我改公式 -> Formula mode: return LaTeX/Markdown unless user asks for .docx.
检查论文格式 -> Validator mode: run scripts/validator.py with an appropriate level.
- 其他任何数学文档请求 -> 同一流水线,按 Working Mode 分类处理。
- Existing document with a template or baseline -> template/baseline wins.
Pipeline
Markdown -> Parser -> LaTeX extraction -> MathML -> OMML -> DOCX
For .docx output, always prefer latex2mathml -> MML2OMML.XSL -> OMML over manual OMML construction. Manual OMML is only for edge cases documented in references/omml.md.
Mandatory Rules
Formula
- Use
latex2mathml -> OMML for all math.
- 单一入口
latex_to_omml(latex, alttext=None, fix_limits=True):默认已完成 aligned 改写、n-ary 限位与 naryPr 归一化。不要绕过它直接调 latex2mathml.converter.convert,也不要手写 limLoc 修补。
- Never use Unicode composed subscripts/superscripts, plain text formulas, upright variables, or
|x| as plain text for absolute value.
- LaTeX 绝对值/范数必须写
\left|...\right|;独立公式里的裸 |X|^2 会被 latex2mathml 解析成空 m:e。手动构造 OMML 时用 mabs 分隔符结构。
- 大算符限位:
∫ 用 limLoc="subSup",∑/∏ 用 limLoc="undOvr"(规则表在 specs.NARY_LIM_LOC,validator 用同一份断言);lim 用 m:limLow,不用 m:sSub。
- 限位的已知边界与结构级改法、
\underbrace 与 mstyle 的处理,见 references/omml.md。
Styles
以下字体/排版规则均为用户未要求时的默认值:用户明确指定格式(字体、字号、颜色、间距、对齐等)时,以用户要求为准,默认规则不得覆盖用户格式。
- 字体名以
scripts/specs.py 的 FONT_* 为单一真相源:正文宋体、标题黑体(Heading 2 必须加粗)、大标题方正小标宋简体、拉丁 Times New Roman。样式级必须显式设 eastAsia,只设 run 字体不足以通过 validator。
- 通过
doc.styles 设置一次,不逐 run 写字体。
- 表格标题(表N xxx):表格下方居中、常规字体不加粗、不用黑体,space_before 4 / space_after 6。
- 字号、页边距、表格几何、列宽算法、对齐规则见
references/docx-style.md。
- No decorative literal spaces. Strip text segments, no spaces around
=/+/-; minus sign U+2212.
Validation
- BEFORE generation: batch-verify all formulas new to this document with
scripts/formula_check.py (0 failures before writing the generator).
- After generation, run
scripts/validator.py on the saved .docx.
- Level 1: basic open/equation/markdown residue checks(含代码块豁免:Consolas 段落里的
**/$ 不判残留)。
- Level 2: academic font/table checks. Level 3: publication checks plus page size.
- level 2 的字体断言以
specs.py 默认值为准。若项目以既有 docx 作样式基线且基线另有约定,以基线为准,此时该断言不适用 —— 详见 references/validator.md。
- LibreOffice/Poppler unavailable: explicitly state
渲染未验证。
Rendering
外部工具按「环境变量 → PATH → 常见安装路径」定位(实现在 specs.find_soffice / specs.find_pdftoppm),不要写死本机路径:
- LibreOffice:
MATHDOC_SOFFICE 覆盖,否则查 soffice / 常见安装目录。
- poppler:
PDFTOPPM 覆盖,否则查 PATH。Windows 上必须指向原生 pdftoppm.exe,不要用 .cmd 包装器(本环境会报 The system cannot find the path specified)。
soffice --headless --convert-to pdf --outdir out input.docx # 渲染 PDF
python scripts/render_check.py out/input.docx 关键词1 关键词2 # 文本冒烟
python scripts/render_diff.py before.pdf after.pdf --out out/ # 像素 diff(--out 落盘差异图)
render_check 的 probe 必须与文档措辞逐字节一致(含空格);中间 PDF 默认写临时目录,需要保留时加 --keep-pdf。
Auto-Learning(动态 skill)
Skill 不是静态的:每次任务自动读经验、自动沉淀新经验,用户无需手动追加。
任务开始前(自动读取):读取 ~/.config/math-doc/user-lessons.md(路径可用 python scripts/mathdoc_learn.py --path 确认)。该文件存个人经验库(用户格式要求、踩坑、验证过的新语法),生成时必须遵循其中的条目,优先级:用户明确要求 > user-lessons 条目 > 内置默认规则。
任务完成后(自动追加):若本次任务产生了新经验——踩坑(含根因/修复)、用户明确给出的格式要求、新验证通过的 LaTeX 语法——用 mathdoc_learn.py --add 自动追加,无需用户手动操作:
python scripts/mathdoc_learn.py --add "教训一句话" --root-cause "根因" --fix "修复" --verify "验证" --task "项目名"
追加前用 --list 检查去重;经验文件在 skill 目录之外,sync_install.sh 不会覆盖;references/lessons.md 保持只读(内置通用经验),个人经验一律进 user-lessons.md。
Failure Handling
latex2mathml or MML2OMML.XSL unavailable: report environment missing; do not silently switch to Unicode or manual OMML.
- Font missing: detect first, fallback second, note fallback in delivery.
- Template exists: template wins.
- Formula conversion fails: preserve the source formula and report the error instead of dropping it.
- Validator reports
empty m:e:优先检查含裸 |...| 的公式,改为 \left|...\right| 后重新生成。
References
Load the relevant reference before generating:
references/docx-style.md: fonts, headings, spacing, page, tables, baseline extraction.
references/omml.md: latex2mathml -> XSLT -> OMML, lim/underbrace handling, manual OMML edge cases, accessibility.
references/markdown-parser.md: Markdown placeholder parsing, bold across $...$, literal $, spacing, mixed paragraphs.
references/validator.md: validation levels, table grid checks, PDF rendering.
references/performance.md: formula cache, XSLT single-pass, global styles, deferred attach, batch validation.
references/lessons.md: 批量章节文档生成实战(公式预验证、渲染冒烟 probes、GBK 假警报、模板继承、逐章流水线)。
~/.config/math-doc/user-lessons.md: 个人经验库,任务开始前自动读取、完成后自动追加(见 Auto-Learning)。
CHANGELOG.md: version history and past failures.
Scripts
scripts/specs.py: 共享常量与外部工具定位(NARY_LIM_LOC、FONT_*、find_soffice、find_pdftoppm)。零依赖叶子模块,生成侧与校验侧共用。
scripts/latex_to_omml.py: LaTeX -> OMML 管线。入口 latex_to_omml,旧名 latex_to_omml_alt/_fixed/_fixed_alt 保留为等价别名。
scripts/omml_helpers.py: 手工 OMML builder(mpara_mix、mnary、mlim、mabs 等),供 latex2mathml 表达不了的边缘公式。
scripts/mathdoc_cli.py: --template proof|notes|derivation skeleton generator.
scripts/validator.py: post-generation .docx validator with --level 1|2|3.
scripts/render_diff.py: pixel diff between rendered PDFs(--out 写差异图)。
scripts/formula_check.py: batch-verify LaTeX formulas against latex_to_omml before generating.
scripts/render_check.py: lightweight render smoke test (LibreOffice -> PDF -> text probes).
scripts/publish_report.py: delivery report — validate a docx and write validation-report.md (equation count, checks, OMML engine), producing the source/result/report triplet.
scripts/mathdoc_learn.py: 动态 skill 经验管理 — --add 自动追加教训到个人经验库(自动带日期与问题/根因/修复/验证格式)、--list 查看、--path 定位。
python scripts/mathdoc_cli.py --template proof --title 证明 --output proof.docx
python scripts/validator.py proof.docx --level 2
python scripts/formula_check.py --file new_formulas.txt
python scripts/render_check.py doc.docx 定理 定义 性质
python scripts/mathdoc_learn.py --add "本次教训" --root-cause "根因" --fix "修复" --verify "验证"
python -m pytest tests/ -v
Equation Numbering
Default: plain visible text (1), (2), (3) with a right tab stop. Use SEQ fields only when the document needs cross-references, and warn that Word requires Ctrl+A F9 to refresh.
1---2name: math-doc3description: Generate mathematical Word documents for any scenario — notes, exercise sets, summaries, reports, proofs, papers — in .docx, LaTeX, or Markdown. Covers OMML rendering via python-docx, Chinese typography and font rules, Markdown-to-docx conversion, formula formatting, equation numbering, and cross-references. Use when the user asks to produce or format any mathematical document or formula-heavy output.4---56# math-doc Skill78## Purpose910Generate professional mathematical documents for any scenario — lecture notes, exercise sets, summaries, proofs, reports, papers, or anything formula-heavy. Output can be .docx, LaTeX, or Markdown.1112## Trigger1314Use this skill when:1516- user requests any mathematical document (notes, exercises, summaries, proofs, reports)17- user requests a formula-heavy Word document18- user requests formula formatting or OMML conversion19- user requests Markdown/LaTeX to .docx generation20- user requests checking the format of a generated math document2122## Decision Priority23241. User explicit requirement252. Existing project/file/template constraints263. Domain skill rules274. General best practice2829## Working Mode3031Classify the request:3233- A. Creation: full pipeline, build from scratch.34- B. Modification: inspect structure first, then modify.35- C. Analysis: explain document structure or formula, do not regenerate.36- D. Debugging: inspect generated file, find root cause, then fix.37- E. Research: gather references or conventions before output.3839Ask only when missing information affects the result. Otherwise use reasonable defaults.4041## Task Router4243- `帮我写证明`、`写讲义`、`整理笔记`、`出习题集` -> math-doc -> ask `需要Word吗?`; if yes, use docx pipeline.44- `帮我改公式` -> Formula mode: return LaTeX/Markdown unless user asks for .docx.45- `检查论文格式` -> Validator mode: run `scripts/validator.py` with an appropriate level.46- 其他任何数学文档请求 -> 同一流水线,按 Working Mode 分类处理。47- Existing document with a template or baseline -> template/baseline wins.4849## Pipeline5051```text52Markdown -> Parser -> LaTeX extraction -> MathML -> OMML -> DOCX53```5455For .docx output, always prefer `latex2mathml -> MML2OMML.XSL -> OMML` over manual OMML construction. Manual OMML is only for edge cases documented in `references/omml.md`.5657## Mandatory Rules5859### Formula6061- Use `latex2mathml -> OMML` for all math.62- **单一入口 `latex_to_omml(latex, alttext=None, fix_limits=True)`**:默认已完成 aligned 改写、n-ary 限位与 naryPr 归一化。不要绕过它直接调 `latex2mathml.converter.convert`,也不要手写 limLoc 修补。63- Never use Unicode composed subscripts/superscripts, plain text formulas, upright variables, or `|x|` as plain text for absolute value.64- LaTeX 绝对值/范数必须写 `\left|...\right|`;独立公式里的裸 `|X|^2` 会被 latex2mathml 解析成空 `m:e`。手动构造 OMML 时用 `mabs` 分隔符结构。65- 大算符限位:`∫` 用 `limLoc="subSup"`,`∑`/`∏` 用 `limLoc="undOvr"`(规则表在 `specs.NARY_LIM_LOC`,validator 用同一份断言);`lim` 用 `m:limLow`,不用 `m:sSub`。66- 限位的已知边界与结构级改法、`\underbrace` 与 `mstyle` 的处理,见 `references/omml.md`。6768### Styles6970以下字体/排版规则均为**用户未要求时的默认值**:用户明确指定格式(字体、字号、颜色、间距、对齐等)时,以用户要求为准,默认规则不得覆盖用户格式。7172- 字体名以 `scripts/specs.py` 的 `FONT_*` 为单一真相源:正文宋体、标题黑体(Heading 2 必须加粗)、大标题方正小标宋简体、拉丁 Times New Roman。样式级必须显式设 `eastAsia`,只设 run 字体不足以通过 validator。73- 通过 `doc.styles` 设置一次,不逐 run 写字体。74- 表格标题(表N xxx):表格**下方**居中、常规字体不加粗、不用黑体,space_before 4 / space_after 6。75- 字号、页边距、表格几何、列宽算法、对齐规则见 `references/docx-style.md`。76- No decorative literal spaces. Strip text segments, no spaces around `=`/`+`/`-`; minus sign U+2212.7778### Validation7980- BEFORE generation: batch-verify all formulas new to this document with `scripts/formula_check.py` (0 failures before writing the generator).81- After generation, run `scripts/validator.py` on the saved `.docx`.82- Level 1: basic open/equation/markdown residue checks(含代码块豁免:Consolas 段落里的 `**`/`$` 不判残留)。83- Level 2: academic font/table checks. Level 3: publication checks plus page size.84- **level 2 的字体断言以 `specs.py` 默认值为准**。若项目以既有 docx 作样式基线且基线另有约定,以基线为准,此时该断言不适用 —— 详见 `references/validator.md`。85- LibreOffice/Poppler unavailable: explicitly state `渲染未验证`。8687### Rendering8889外部工具按「环境变量 → PATH → 常见安装路径」定位(实现在 `specs.find_soffice` / `specs.find_pdftoppm`),不要写死本机路径:9091- LibreOffice:`MATHDOC_SOFFICE` 覆盖,否则查 `soffice` / 常见安装目录。92- poppler:`PDFTOPPM` 覆盖,否则查 PATH。Windows 上**必须指向原生 `pdftoppm.exe`**,不要用 `.cmd` 包装器(本环境会报 `The system cannot find the path specified`)。9394```bash95soffice --headless --convert-to pdf --outdir out input.docx # 渲染 PDF96python scripts/render_check.py out/input.docx 关键词1 关键词2 # 文本冒烟97python scripts/render_diff.py before.pdf after.pdf --out out/ # 像素 diff(--out 落盘差异图)98```99100`render_check` 的 probe 必须与文档措辞**逐字节一致**(含空格);中间 PDF 默认写临时目录,需要保留时加 `--keep-pdf`。101102## Auto-Learning(动态 skill)103104Skill 不是静态的:每次任务自动读经验、自动沉淀新经验,用户无需手动追加。105106- **任务开始前(自动读取)**:读取 `~/.config/math-doc/user-lessons.md`(路径可用 `python scripts/mathdoc_learn.py --path` 确认)。该文件存个人经验库(用户格式要求、踩坑、验证过的新语法),生成时必须遵循其中的条目,优先级:用户明确要求 > user-lessons 条目 > 内置默认规则。107- **任务完成后(自动追加)**:若本次任务产生了新经验——踩坑(含根因/修复)、用户明确给出的格式要求、新验证通过的 LaTeX 语法——用 `mathdoc_learn.py --add` 自动追加,无需用户手动操作:108109 ```bash110 python scripts/mathdoc_learn.py --add "教训一句话" --root-cause "根因" --fix "修复" --verify "验证" --task "项目名"111 ```112113- 追加前用 `--list` 检查去重;经验文件在 skill 目录之外,`sync_install.sh` 不会覆盖;`references/lessons.md` 保持只读(内置通用经验),个人经验一律进 user-lessons.md。114115## Failure Handling116117- `latex2mathml` or `MML2OMML.XSL` unavailable: report environment missing; do not silently switch to Unicode or manual OMML.118- Font missing: detect first, fallback second, note fallback in delivery.119- Template exists: template wins.120- Formula conversion fails: preserve the source formula and report the error instead of dropping it.121- Validator reports `empty m:e`:优先检查含裸 `|...|` 的公式,改为 `\left|...\right|` 后重新生成。122123## References124125Load the relevant reference before generating:126127- `references/docx-style.md`: fonts, headings, spacing, page, tables, baseline extraction.128- `references/omml.md`: latex2mathml -> XSLT -> OMML, lim/underbrace handling, manual OMML edge cases, accessibility.129- `references/markdown-parser.md`: Markdown placeholder parsing, bold across `$...$`, literal `$`, spacing, mixed paragraphs.130- `references/validator.md`: validation levels, table grid checks, PDF rendering.131- `references/performance.md`: formula cache, XSLT single-pass, global styles, deferred attach, batch validation.132- `references/lessons.md`: 批量章节文档生成实战(公式预验证、渲染冒烟 probes、GBK 假警报、模板继承、逐章流水线)。133- `~/.config/math-doc/user-lessons.md`: 个人经验库,任务开始前自动读取、完成后自动追加(见 Auto-Learning)。134- `CHANGELOG.md`: version history and past failures.135136## Scripts137138- `scripts/specs.py`: 共享常量与外部工具定位(`NARY_LIM_LOC`、`FONT_*`、`find_soffice`、`find_pdftoppm`)。零依赖叶子模块,生成侧与校验侧共用。139- `scripts/latex_to_omml.py`: LaTeX -> OMML 管线。入口 `latex_to_omml`,旧名 `latex_to_omml_alt/_fixed/_fixed_alt` 保留为等价别名。140- `scripts/omml_helpers.py`: 手工 OMML builder(`mpara_mix`、`mnary`、`mlim`、`mabs` 等),供 latex2mathml 表达不了的边缘公式。141- `scripts/mathdoc_cli.py`: `--template proof|notes|derivation` skeleton generator.142- `scripts/validator.py`: post-generation .docx validator with `--level 1|2|3`.143- `scripts/render_diff.py`: pixel diff between rendered PDFs(`--out` 写差异图)。144- `scripts/formula_check.py`: batch-verify LaTeX formulas against latex_to_omml before generating.145- `scripts/render_check.py`: lightweight render smoke test (LibreOffice -> PDF -> text probes).146- `scripts/publish_report.py`: delivery report — validate a docx and write `validation-report.md` (equation count, checks, OMML engine), producing the source/result/report triplet.147- `scripts/mathdoc_learn.py`: 动态 skill 经验管理 — `--add` 自动追加教训到个人经验库(自动带日期与问题/根因/修复/验证格式)、`--list` 查看、`--path` 定位。148149```bash150python scripts/mathdoc_cli.py --template proof --title 证明 --output proof.docx151python scripts/validator.py proof.docx --level 2152python scripts/formula_check.py --file new_formulas.txt153python scripts/render_check.py doc.docx 定理 定义 性质154python scripts/mathdoc_learn.py --add "本次教训" --root-cause "根因" --fix "修复" --verify "验证"155python -m pytest tests/ -v156```157158## Equation Numbering159160Default: plain visible text `(1)`, `(2)`, `(3)` with a right tab stop. Use SEQ fields only when the document needs cross-references, and warn that Word requires Ctrl+A F9 to refresh.