PHP code example of latex2mathml / latex2mathml-php

1. Go to this page and download the library: Download latex2mathml/latex2mathml-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

latex2mathml / latex2mathml-php example snippets


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>

// Recommended: Named argument (PHP 8.0+)
$mathml = Converter::convert($latex, display: 'block');

// Also possible: Positional argument
$mathml = Converter::convert($latex, 'block');

// 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');

$dom = new DOMDocument();
$mathElement = Converter::convert_to_element($latex, $dom, display: 'block');
$dom->appendChild($mathElement);
echo $dom->saveXML();
bash
composer 
bash
   php display_math.php > output.html