npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

latex-to-omml

v2.1.1

Published

A Node.js library for converting LaTeX mathematical formulas to Microsoft Word OMML format / 将 LaTeX 数学公式转换为 Microsoft Word OMML 格式的 Node.js 库

Downloads

260

Readme

latex-to-omml

一个将 LaTeX 数学公式转换为 Microsoft Word OMML 格式的 Node.js 库

Version License Platform Node Version

English | 中文


📑 目录


✨ 功能特性

  • LaTeX 转 OMML - 将 LaTeX 数学公式转换为 OMML 格式
  • 行内与块级公式 - 支持行内公式和块级公式(display mode)
  • 自动标签处理 - 自动处理并移除 \tag 命令
  • 成熟库支持 - 基于成熟的第三方库(mathjax-nodemathml2omml
  • 简单 API - 易于使用的基于 Promise 的 API
  • 错误处理 - 完善的错误处理和验证

📦 安装

npm install latex-to-omml

🚀 快速开始

const { latexToOMML } = require('latex-to-omml');

// 行内公式
const omml = await latexToOMML('\\frac{a}{b}');
console.log(omml);

// 块级公式
const ommlBlock = await latexToOMML('\\sum_{i=1}^{n} x_i', { displayMode: true });

📖 使用方法

基本用法

const { latexToOMML } = require('latex-to-omml');

// 行内公式
const omml = await latexToOMML('\\frac{a}{b}');

// 块级公式
const ommlBlock = await latexToOMML('\\sum_{i=1}^{n} x_i', { displayMode: true });

选项说明

| 选项 | 类型 | 默认值 | 说明 | |------|------|--------|------| | displayMode | boolean | false | 是否为块级公式 |


📚 API 参考

latexToOMML(latex, options?)

将 LaTeX 代码转换为 OMML 字符串。

参数:

  • latex (string): LaTeX 数学公式代码
  • options (object, 可选): 转换选项
    • displayMode (boolean): 是否为块级公式,默认 false

返回:

  • Promise<string>: OMML XML 字符串

抛出:

  • TypeError: 如果 latex 不是字符串
  • Error: 如果 LaTeX 代码为空或转换失败

💡 示例

基础公式

// 分数
await latexToOMML('\\frac{a}{b}');

// 根号
await latexToOMML('\\sqrt{x}');

// 幂
await latexToOMML('x^{2}');

// 下标
await latexToOMML('x_{i}');

复杂公式

// 矩阵
await latexToOMML('\\begin{pmatrix} a & b \\\\ c & d \\end{pmatrix}');

// 积分
await latexToOMML('\\int_{0}^{\\infty} e^{-x} dx');

// 求和
await latexToOMML('\\sum_{i=1}^{n} x_i', { displayMode: true });

带标签的公式

// \tag 命令会被自动移除
await latexToOMML('E = mc^2 \\tag{1}');

错误处理

try {
  const omml = await latexToOMML('\\frac{a}{b}');
  console.log('成功:', omml);
} catch (error) {
  console.error('错误:', error.message);
}

🔄 转换流程

完整的转换流程如下:

LaTeX 代码
  ↓ [预处理:移除 \tag,处理换行符]
预处理后的 LaTeX
  ↓ [mathjax-node]
MathML XML
  ↓ [清理:移除 MathJax 特定属性]
清理后的 MathML
  ↓ [mathml2omml]
OMML XML

🎯 支持的 LaTeX 特性

  • ✅ 基础数学符号和运算符
  • ✅ 分数、根号、幂、下标、上标
  • ✅ 矩阵和行列式
  • ✅ 积分、求和、乘积
  • ✅ 希腊字母和特殊符号
  • ✅ AMS 数学扩展
  • ✅ 括号和分隔符

⚠️ 限制

  • ❌ 不支持 \tag 命令(会被自动移除)
  • ⚠️ 某些复杂的 LaTeX 命令可能不被支持,取决于 MathJax 的支持情况

📦 依赖项

| 包名 | 版本 | 用途 | |------|------|------| | mathjax-node | ^2.1.1 | 将 LaTeX 转换为 MathML | | mathml2omml | ^0.5.0 | 将 MathML 转换为 OMML |


📝 注意事项

MathJax 初始化

库内部使用单例模式管理 MathJax 初始化,无需手动初始化。

错误处理

转换过程中可能出现以下错误:

  • LaTeX 语法错误:会抛出包含 MathJax 错误信息的异常
  • 转换失败:会抛出包含详细错误信息的异常

性能优化

  • 对于相同 LaTeX 代码,建议在应用层进行缓存
  • 批量转换时建议使用并发控制(建议最大 10 个并发)

行内 vs 块级公式

  • 块级公式:通常居中显示,在 Word 中使用 <m:oMathPara> 包装
  • 行内公式:嵌入在文本中,在 Word 中使用 <m:oMath> 包装

🤝 贡献

欢迎贡献!请随时提交 Pull Request。


📄 许可证

本项目采用 MIT 许可证。


📚 参考资源


为 LaTeX 和 Word 社区而制作