Using Mathjax

Plugin Name:mathJax
Version:
Description:Using Mathjax as a plugin
Last Update:2014/07/07
Author:jacmgr
Comments: See Comment Section Tags:

MathJax is a javascript system for rendereing math equations in Tex, Lex, MML, and AsciiMath. All it takes is inclusion of the MathJax javascript in your displayed page. A plugin is not needed, but it does make it easier for non programmers. See this post on using mathjax withouty a plugin

Why a plugin

Sometimes you don't want to use HTML block tags in your markdown document. You may have a reason to want the section of code parsed by markdown. The default mathjax script will conflict with markdown under these circumstances. Primarily the conflict is with the ASCIIMATH and its backquote delimiter ` which markdown uses as code block. This plugin is for ASCIIMATH in MathJax. It has no effect on TEX/LEX or MathML.

This plugin solves that problem. The plugin also lets you separate the script from your index file so you don't have to hard code it in the template. So instead of

{% if meta.mathjax %}
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_HTMLorMML.js">
</script>
{% endif %}

You can just do this

{% if meta.mathjax %}
    {{ mathjax.script }}
{% endif %}

That's useful since if you are power user of mathjax, the are many options and additional scripts to add in this section.

The plugin config file is where you define your script.

<?php
// MathJax
// ---------------------------------------
/*
    GOOD FOR TEX: http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML-full
    GOOD ASIMATH: src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=AM_HTMLorMML-full
    Good for all markups: http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_HTMLorMML.js
*/
$script_mathjax = <<< ENDSCRIPT
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_HTMLorMML.js"></script>
ENDSCRIPT;

$config = array(
    'plugin_name' => 'mathjax',
    'script_mathjax' => $script_mathjax,
);
return $config;

When the plugin runs it will create the template variable {{ mathjax.script }}. If the plugin detects the double backquote the plugin will also automatically set the page meta mathjax to true. So if you are using the plugin, you don't have to manually set the meta on the page.

What's it do

Basically it strips out the mathjax tags before markdown parses and then it puts the proper tags back in after markdown parses.

In order to work you must USE TWO BACKQUOTES as the ASCIIMATH DELIMITER instead of one backqoute. Also we are using this because we don't want to use a HTML block tags. Compare these examples without HTML block tags:

ONE BLACKSLASH

> The two solutions are `x_(1,2)=(-b+-sqrt(b^2 - 4a c))/(2a)`

The two solutions are x_(1,2)=(-b+-sqrt(b^2 - 4a c))/(2a)

TWO BACKSLASHES

> The two solutions are ```x_(1,2)=(-b+-sqrt(b^2 - 4a c))/(2a)```

The two solutions are x_(1,2)=(-b+-sqrt(b^2 - 4a c))/(2a)

The two backquote method is working because of the plugin.

And a longer example also without HTML block tags.

Suppose \``ax^2+b x+c=0\``  and \``a!=0\``. We first divide by \``a\`` to get \``x^2+b/a x+c/a=0\``. 

Then we complete the square and obtain \``x^2+b/a x+(b/(2a))^2-(b/(2a))^2+c/a=0\``. 
The first three terms factor to give \``(x+b/(2a))^2=(b^2)/(4a^2)-c/a\``.
Now we take square roots on both sides and get \``x+b/(2a)=+-sqrt((b^2)/(4a^2)-c/a)\``.

Finally we subtract \``b/(2a)\`` from both sides and simplify to get the two solutions: 
\``x_(1,2)=(-b+-sqrt(b^2 - 4a c))/(2a)\``

Produces

Suppose ax^2+b x+c=0 and a!=0\. We first divide by a to get x^2+b/a x+c/a=0.

  • Then we complete the square and obtain x^2+b/a x+(b/(2a))^2-(b/(2a))^2+c/a=0.
  • The first three terms factor to give (x+b/(2a))^2=(b^2)/(4a^2)-c/a.
  • Now we take square roots on both sides and get x+b/(2a)=+-sqrt((b^2)/(4a^2)-c/a).

  • Finally we subtract b/(2a) from both sides and simplify to get the two solutions:

    x_(1,2)=(-b+-sqrt(b^2 - 4a c))/(2a)\

CONCLUSION

If you use the plugin, your ASCIIMATH should be in TWO backquotes