# PHP Markdown Extra Math

December 17, 2009 at 5:29 PM by Dr. Drang

Tuesday’s MathJax post reminded me that I’ve never made public my modifications to PHP Markdown Extra that allow me to easily write equations here on the blog. So I took a virgin copy of Michel Fortin’s latest version of PHP Markdown Extra, applied my modifications, and put the result in a GitHub repository. From the repository’s README:

PHP Markdown Extra Math is an extension of Michel Fortin’s PHP Markdown Extra, a PHP script for converting text written in Markdown to HTML. The extension consist of adding support for mathematical equations written in LaTeX to be processed by Davide Cervone’s jsMath system.

Here’s how it works. The author, writing in Markdown, inserts inline equations like this

```
where \(\alpha = (t_1 - t_0)/L\) is the rate at which the thickness increases
```

enclosing the math in a `\( … \)`

pair, just as if writing in LaTeX. PHP Markdown Extra Math converts that to

```
where <span class="math"> \alpha = (t_1 - t_0)/L </span> is the rate at which the thickness increases
```

which is then converted by jsMath into

where \(\alpha = (t_1 - t_0)/L\) is the rate at which the thickness increases

Similarly, display Math is written like this:

```
Putting this into Castigliano's equation, we get
\[\Delta = \frac{\partial U^*}{\partial F} = \frac{12F}{Eb} \int_0^L \frac{x^2}{(t_0 + \alpha x)^3} dx\]
```

which PHP Markdown Extra Math will turn into this HTML

```
<p>Putting this into the Castigliano equation, we get</p>
<div class="math">\Delta = \frac{\partial U^*}{\partial F} = \frac{12F}{Eb} \int_0^L \frac{x^2}{(t_0 + \alpha x)^3} dx</div>
```

which, in turn, will be rendered by jsMath like this:

Putting this into Castigliano’s equation, we get

\[\Delta = \frac{\partial U^*}{\partial F} = \frac{12F}{Eb} \int_0^L \frac{x^2}{(t_0 + \alpha x)^3} dx\]

The examples were taken directly from my post on Castigliano’s Second Theorem.

For posts that have just one or two equations, there’s not much to be gained by using the `\( … \)`

and `\[ … \]`

notation in place of `<span class="math"> … </span>`

and `<div class="math"> … </div>`

, but it’s a real timesaver when the equations start to pile up.

If you’re a jsMath aficionado, you may be wondering why I don’t take advantage of its tex2math extension, which will interpret the `\( … \)`

and `\[ … \]`

notation directly. It’s because Markdown already has a backslash escape syntax that works with parentheses and brackets, and there’s no way for me to keep the notation straight if Markdown and jsMath fighting with each other. So I have the tex2math extensions turned off.