latex2mathml/latex2mathml-php
最新稳定版本:v1.2.3
Composer 安装命令:
composer require latex2mathml/latex2mathml-php
包简介
Pure PHP library for LaTeX to MathML 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
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-05