<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The paperCrane's view from the reverseFold &#187; ASCIIMathML</title>
	<atom:link href="http://www.reversefold.com/blog/tag/asciimathml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.reversefold.com/blog</link>
	<description>Flex, PHP, and anything else I care to talk about</description>
	<lastBuildDate>Tue, 30 Aug 2011 08:51:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Pretty equations in HTML</title>
		<link>http://www.reversefold.com/blog/2008/11/27/pretty-equations-in-html/</link>
		<comments>http://www.reversefold.com/blog/2008/11/27/pretty-equations-in-html/#comments</comments>
		<pubDate>Fri, 28 Nov 2008 02:25:53 +0000</pubDate>
		<dc:creator>papercrane</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[ASCIIMathML]]></category>
		<category><![CDATA[DHTML]]></category>
		<category><![CDATA[MathML]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.reversefold.com/blog/?p=79</guid>
		<description><![CDATA[You may have seen the nice looking equations in my last post. As part of the process of writing that post I researched MathML as a way of embedding the equations in the post. it turned out to be a complex markup language which renders very nicely but has a few big drawbacks. The first [...]]]></description>
			<content:encoded><![CDATA[<p>You may have seen the nice looking equations in <a href="/blog/2008/11/24/fitting-circles-within-a-larger-circle/">my last post</a>. As part of the process of writing that post I researched <a href="http://www.w3.org/Math/">MathML</a> as a way of embedding the equations in the post. it turned out to be a complex markup language which renders very nicely but has a few big drawbacks. The first is that only Firefox supports MathML in its core so far. There is a plugin for Internet Explorer which supports it but I doubt it&#8217;s widely installed. The second problem is that you can&#8217;t just put MathML in your web page and have Firefox interpret it. It turns out you need <a href="http://www1.chapman.edu/~jipsen/mathml/mathhtml/">a bit of Javascript</a> to make it pick up and render the MathML. This is great and makes equations render very well in Firefox but writing the MathML is a pain.</p>
<p>Enter <a href="http://www1.chapman.edu/~jipsen/mathml/asciimathsyntax.html">ASCIIMathML</a>, a JavaScript library which converts a simple ASCII format to MathML for rendering in MathML compliant browsers. The syntax is pretty much what you would expect with a few tricks up its sleeve. A simple equation, such as `y=x^2` is written as <tt>y=x^2</tt>. A more complicated example from my last post such as:<br />
`arc=2*cos^-1(1-r_2^2/(2*r_1^2))*r_1`<br />
is written as:</p>
<pre>arc=2*cos^-1(1-r_2^2/(2*r_1^2))*r_1</pre>
<p>Another example with a square root:<br />
`y=(+-r_2*sqrt(4*r_1^2-r_2^2))/(2*r_1)`</p>
<pre>y=(+-r_2*sqrt(4*r_1^2-r_2^2))/(2*r_1)</pre>
<p>Now, those if you viewing these posts in a non-Firefox browser are probably wondering why you are also seeing pretty equations here. The short answer is that you&#8217;re seeing images imstead of MathML rendered by your browser. The long answer involves a four part toolchain. The first piece is ASCIIMathML, slightly altered to insert an image tag instead of MathML. The images are generated by a PHP script which first runs the ASCII through <a href="http://www.oldschool.com.sg/index.php/module/Shared/action/Static/tmpl/ASCIIMathPHP">ASCHIIMathPHP</a>, a version of ASCIIMathML.js to PHP. The resulting MathML has the times symbol replace by one which the later tools understand (replace &amp;#8901; with &amp;#183;). The resulting MathML is then passed to <a href="http://www.grigoriev.ru/svgmath/">svgmath</a>, a python script which converts MathML to SVG. SVG would be great but Internet Explorer still doesn&#8217;t support it without a plugin. Because of this the SVG is then passed through inkscape via command-line to render out a png which is what you see in your browser.<br />
Here&#8217;s some sample PHP to run these commands:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$ascii</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'y=(+-r_2*sqrt(4*r_1^2-r_2^2))/(2*r_1)'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$pngFile</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ascii</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.png'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pngFile</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'ASCIIMathPHP-2.0.cfg.php'</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">require</span> <span style="color: #0000ff;">'ASCIIMathPHP-2.0.class.php'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$ascii_math</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ASCIIMathPHP<span style="color: #009900;">&#40;</span><span style="color: #000088;">$symbol_arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$ascii_math</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>setExpr<span style="color: #009900;">&#40;</span><span style="color: #000088;">$ascii</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$ascii_math</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>genMathML<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$mathml</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$ascii_math</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>getMathML<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$mathml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'⋅'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'·'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mathml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$mathmlFile</span> <span style="color: #339933;">=</span> <span style="color: #990000;">tempnam</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">sys_get_temp_dir</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'mathml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$td</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mathmlFile</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$td</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mathml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$svgFile</span> <span style="color: #339933;">=</span> <span style="color: #990000;">tempnam</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">sys_get_temp_dir</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'svg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">system</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/usr/bin/env python math2svg.py -s <span style="color: #006699; font-weight: bold;">$mathmlFile</span> &amp;gt; <span style="color: #006699; font-weight: bold;">$svgFile</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #990000;">system</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;inkscape -f <span style="color: #006699; font-weight: bold;">$svgFile</span> -e <span style="color: #006699; font-weight: bold;">$pngFile</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: image/png'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pngFile</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>All of this is also wrapped up in a WordPress plugin which includes the JS file in the header. Unfortunately, I don&#8217;t have an easy way to package this up as it requires python and inkscape but if you would like to get more info just let me know and I&#8217;ll send you more information.</p>
<p><strong>UPDATE</strong>: I finally did update the plugin to be useful for others and <a href="http://www.reversefold.com/blog/2009/02/06/asciimathml-wordpress-plugin/">posted it</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.reversefold.com/blog/2008/11/27/pretty-equations-in-html/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

