latex2mathml/latex2mathml-php
Composer 安装命令:
composer require latex2mathml/latex2mathml-php
包简介
Pure PHP library for LaTeX to MathML and OMML conversion
README 文档
README
Pure PHP 8.4 library for LaTeX to MathML conversion. This is a port of the Python library latex2mathml.
Features
- No external dependencies (only PHP 8.4+ and
ext-dom). - Supports a wide range of LaTeX commands and symbols.
- Outputs clean MathML.
- Supports inline and block display modes.
Installation
composer require latex2mathml/latex2mathml-php
Usage
Simple conversion
By default, the converter produces inline MathML.
use Latex2MathML\Converter; $latex = 'a^2 + b^2 = c^2'; $mathml = Converter::convert($latex); echo $mathml; // <math xmlns="http://www.w3.org/1998/Math/MathML" display="inline">...</math>
Display Mode
You can specify the display mode (inline or block) as the second argument. For clarity and to ensure future compatibility, using named arguments (PHP 8.0+) is recommended.
// Recommended: Named argument (PHP 8.0+) $mathml = Converter::convert($latex, display: 'block'); // Also possible: Positional argument $mathml = Converter::convert($latex, 'block');
Custom XML namespace
The third argument allows you to specify a custom XML namespace. It defaults to the standard MathML namespace (http://www.w3.org/1998/Math/MathML). Pass an empty string if you don't want a namespace attribute. Again, named arguments are recommended.
// Recommended: Named argument $mathml = Converter::convert($latex, xmlns: 'http://custom-namespace.org'); // Also possible: Positional arguments $mathml = Converter::convert($latex, 'inline', 'http://custom-namespace.org');
Integration with DOMDocument
If you are already working with a DOMDocument, you can use convert_to_element to get a DOMElement instead of a string.
$dom = new DOMDocument(); $mathElement = Converter::convert_to_element($latex, $dom, display: 'block'); $dom->appendChild($mathElement); echo $dom->saveXML();
Visualization Script
A helper script display_math.php is included to visualize LaTeX conversion from .tex files.
- Place one or more
.texfiles in thetests/directory. - Run the script and redirect the output to an HTML file:
php display_math.php > output.html - Open
output.htmlin your web browser to see the rendered MathML.
The script automatically:
- Finds the first
.texfile in thetests/directory. - Extracts math expressions delimited by
$,$$, or various LaTeX environments (e.g.,equation,align,split). - Converts them to MathML and displays them in a side-by-side view.
Architecture
The project is structured as follows:
Latex2MathML\Converter: Main entry point for conversion.Latex2MathML\Tokenizer: Breaks LaTeX string into tokens.Latex2MathML\Walker: Parses tokens into a tree ofNodeobjects.Latex2MathML\SymbolsParser: Handles conversion of LaTeX symbols to Unicode MathML entities usingunimathsymbols.txt.Latex2MathML\Commands: Contains definitions and maps for LaTeX commands.
Requirements
- PHP 8.4 or higher.
domextension.
License
MIT
latex2mathml/latex2mathml-php 适用场景与选型建议
latex2mathml/latex2mathml-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 211 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「word」 「converter」 「latex」 「MathML」 「omml」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 latex2mathml/latex2mathml-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 latex2mathml/latex2mathml-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 latex2mathml/latex2mathml-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Tools to convert between .NET and PHP date formats
Encode integer IDs using a preset or customized symbol set
Unofficial port of the Carbone API SDK to Saloon v3
Generate random words and sentences with ease in PHP.
TCMB Currenct Converter
Immutable PHP currency converter that's data-agnostic.
统计信息
- 总下载量: 211
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 28
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-05