PHPackages                             latex2mathml/latex2mathml-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. latex2mathml/latex2mathml-php

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

latex2mathml/latex2mathml-php
=============================

Pure PHP library for LaTeX to MathML and OMML conversion

v1.5.0(2mo ago)074↓33.3%MITPHPPHP &gt;=8.4

Since Jan 5Pushed 2mo agoCompare

[ Source](https://github.com/snorky22/latex2mathml-php)[ Packagist](https://packagist.org/packages/latex2mathml/latex2mathml-php)[ RSS](/packages/latex2mathml-latex2mathml-php/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (12)Used By (0)

Latex2MathML-PHP
================

[](#latex2mathml-php)

Pure PHP 8.4 library for LaTeX to MathML conversion. This is a port of the Python library [latex2mathml](https://github.com/roniemartinez/latex2mathml).

Features
--------

[](#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
------------

[](#installation)

```
composer require latex2mathml/latex2mathml-php
```

Usage
-----

[](#usage)

### Simple conversion

[](#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;
// ...
```

### Display Mode

[](#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

[](#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

[](#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
--------------------

[](#visualization-script)

A helper script `display_math.php` is included to visualize LaTeX conversion from `.tex` files.

1. Place one or more `.tex` files in the `tests/` directory.
2. Run the script and redirect the output to an HTML file: ```
    php display_math.php > output.html
    ```
3. Open `output.html` in your web browser to see the rendered MathML.

The script automatically:

- Finds the first `.tex` file in the `tests/` 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
------------

[](#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 of `Node` objects.
- `Latex2MathML\SymbolsParser`: Handles conversion of LaTeX symbols to Unicode MathML entities using `unimathsymbols.txt`.
- `Latex2MathML\Commands`: Contains definitions and maps for LaTeX commands.

Requirements
------------

[](#requirements)

- PHP 8.4 or higher.
- `dom` extension.

License
-------

[](#license)

MIT

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance86

Actively maintained with recent releases

Popularity13

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~7 days

Recently: every ~14 days

Total

10

Last Release

70d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4d32dc65259fd93e66c533aaa99c570c9d6508f9dbea16b35dd596c2472aa3f6?d=identicon)[snorky22](/maintainers/snorky22)

---

Top Contributors

[![snorky22](https://avatars.githubusercontent.com/u/3802603?v=4)](https://github.com/snorky22 "snorky22 (35 commits)")

---

Tags

phpMathMLconverterwordlatexomml

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/latex2mathml-latex2mathml-php/health.svg)

```
[![Health](https://phpackages.com/badges/latex2mathml-latex2mathml-php/health.svg)](https://phpackages.com/packages/latex2mathml-latex2mathml-php)
```

###  Alternatives

[goetas-webservices/xsd2php

Convert XSD (XML Schema) definitions into PHP classes and JMS metadata

2411.6M37](/packages/goetas-webservices-xsd2php)[goetas-webservices/xsd2php-runtime

Convert XSD (XML Schema) definitions into PHP classes

4910.9M36](/packages/goetas-webservices-xsd2php-runtime)[nikic/phlexy

Lexing experiments in PHP

162570.9k13](/packages/nikic-phlexy)[corveda/php-sandbox

A PHP library that can be used to run PHP code in a sandboxed environment

23483.5k2](/packages/corveda-php-sandbox)[sbsaga/toon

🧠 TOON for Laravel — a compact, human-readable, and token-efficient data format for AI prompts &amp; LLM contexts. Perfect for ChatGPT, Gemini, Claude, Mistral, and OpenAI integrations (JSON ⇄ TOON).

6115.6k](/packages/sbsaga-toon)[goetas/xsd2php-runtime

Convert XSD (XML Schema) definitions into PHP classes

493.3k](/packages/goetas-xsd2php-runtime)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
