Welcome to the ASCIIMathPHP Project Page! ASCIIMathPHP is a PHP port of ASCIIMath, a really great Javascript application to render ASCII forumlas as MathML. The ASCIIMathPHP Project is supported by Old School. Try out the ASCIMathPHP Sandbox.
ASCIIMathPHP 2.0 (for PHP5 only). With the end of life announcement of PHP4, I have developed a PHP5 only version of ASCIIMathPHP. Same functionality as ASCIIMathPHP 1.12.1.
ASCIIMathPHP 1.12.1 (for PHP4 only). This is a quick release to 1.12 that fixes the setCurrExpr() missing method bug. If you have downloaded 1.12, please use 1.12.1 instead.
| Version | Changes |
|---|---|
| 2.0 |
|
| 1.12.1 |
|
| 1.12 |
|
| 1.11 |
|
| 1.10 |
|
| 1.02 |
|
| 1.01 |
|
There are 2 parts to getting ASCIIMathPHP to work. The first, is to get the actual ASCIIMathPHP class working and generating MathML. The next, is to make sure the brower understands that it is actually seeing MathML so it can render it properly.
include('ASCIIMathPHP.cfg.php');
include('ASCIIMathPHP.class.php');
$ascii_math = new ASCIIMathPHP($symbol_arr); // The $symbol array is defined in ASCIIMathPHP.class.php
$ascii_math->setExpr($expr); // The ASCIIMath expression you want generate MathML for
$ascii_math->genMathML();
print($ascii_math->getMathML());
Use the code above, to generate MathML presentation markup from ASCIIMath syntax. Once the ASCIIMathPHP object has been instantiated, continue using the code below to parse more ASCIIMath expressions.
$ascii_math->setExpr($expr); $ascii_math->genMathML(); $mathml = $ascii_math->getMathML();
An alternative would be to create a callback function and use it to parse an entire page of expressions. Let's say you are using backquotes(`) as delimters.
function ASCIIMathPHPCallback($mtch_arr)
{
$txt = trim($mtch_arr[1]);
include('ASCIIMathPHP.cfg.php');
require_once('ASCIIMathPHP.class.php');
static $asciimath;
if (!isset($asciimath)) $asciimath = new ASCIIMathPHP($symbol_arr);
$math_attr_arr = array('displaystyle' => 'true');
$asciimath->setExpr($txt);
$asciimath->genMathML($math_attr_arr);
return($asciimath->getMathML());
}
Once you have created the callback function, use the following code to parse a string with a lot of embedded ASCIMath expressions.
$regexp = '/`(.*?)`/s'; // Backquotes as delimiters $txt_with_mathml = preg_replace_callback($regexp,'ASCIIMathPHPCallback',$txt_with_asciimath_exprs); echo $txt_with_mathml;
Currently, I only know of 3 browsers which can render MathML well. Internet Explorer 6 or 7 with MathPlayer and Mozilla Firefox. Safari and Opera do not support MathML at the moment. MathML can be embedded in XML or HTML. This site uses the HTML method. Programtically, XML is easier to implement but it puts a lot of restrictions about what else you can put in the webpage. It also requires a separate XSL stylesheet, pmathml.xsl. In the XML document header you must have:
<?xml version="1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <?xml-stylesheet type="text/xsl" href="pmathml.xsl"?> <!-- ************************************************************************ If you see this text while using Internet Explorer, please install the free MathPlayer plugin from http://www.dessci.com/en/products/mathplayer/ This will enable your browser to display properly formatted mathematics. ************************************************************************ --> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:mml="http://www.w3.org/1998/Math/MathML">
Please remember that you have to output the header('Content-type: text/xml') HTTP header as well to get the browser to understand that it is seeing XML. HTML is more complicated, requires Javascript and requires the use of specific XML namespaces but is much more flexible. Your HTML documents should have the following header:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns:m="http://www.w3.org/1998/Math/MathML"> <head> <title>Some Title</title> <object id="mathplayer" classid="clsid:32F66A20-7614-11D4-BD11-00104BD3F987"> </object><?import namespace="m" implementation="#mathplayer"?> <script type="text/javascript" src="htmlMathML.js"></script> </head> <body onload="convert();">
htmlMathML.js is very important and jumpstarts Firefox's Gecko Engine to render MathML embedded in HTML correctly. You also need to surround each <math>…</math> with <span class="asciimath"></span> tags. This is critical for the JS function to work.
That's about it! If you have any questions, just use the Inquiry Form.