Add MathJax and mhchem support to Mahara

Bug #1464858 reported by Geoffrey Rowland
14
This bug affects 3 people
Affects Status Importance Assigned to Milestone
Mahara
Fix Released
Wishlist
Unassigned

Bug Description

MathJax has become the de facto standard for displaying mathematical equations on the Web. The mhchem extension to MathJax provides additional support for chemical equations. The addition of these would allow Mahara to better support STEM subjects. MathJax is already supported by recent versions of Moodle where it provides an alternative to server-side TeX filters.

The following is a fairly minimalist implementation. More sophisticated admin/configuration could be added.

1. Add a checkbox to /htdocs/admin/site/options.php:

Around line 113 add:

                'mathjax' => array(
                    'type' => 'checkbox',
                    'title' => get_string('mathjax', 'admin'),
                    'description' => get_string('mathjaxdescription', 'admin'),
                    'defaultvalue' => get_config('mathjax'),
                    'help' => true,
                    'disabled' => in_array('mathjax', $OVERRIDDEN),
                ),

Around line 763 add mathjax to the $fields array to give:

    $fields = array(
        'sitename','lang','theme', 'dropdownmenu',
        'defaultaccountlifetime', 'defaultregistrationexpirylifetime', 'defaultaccountinactiveexpire', 'defaultaccountinactivewarn',
        'defaultaccountlifetimeupdate', 'allowpublicviews', 'allowpublicprofiles', 'allowanonymouspages', 'generatesitemap',
        'registration_sendweeklyupdates', 'mathjax', 'institutionexpirynotification', 'institutionautosuspend', 'requireregistrationconfirm',
        'showselfsearchsideblock', 'searchusernames', 'searchplugin', 'showtagssideblock',
        'tagssideblockmaxtags', 'country', 'viewmicroheaders', 'userscanchooseviewthemes',
        'remoteavatars', 'userscanhiderealnames', 'antispam', 'spamhaus', 'surbl', 'anonymouscomments',
        'recaptchaonregisterform', 'recaptchapublickey', 'recaptchaprivatekey', 'loggedinprofileviewaccess', 'disableexternalresources',
        'proxyaddress', 'proxyauthmodel', 'proxyauthcredentials', 'smtphosts', 'smtpport', 'smtpuser', 'smtppass', 'smtpsecure',
        'noreplyaddress', 'homepageinfo', 'showprogressbar', 'showonlineuserssideblock', 'onlineuserssideblockmaxusers',
        'registerterms', 'licensemetadata', 'licenseallowcustom', 'allowmobileuploads', 'creategroups', 'createpublicgroups', 'allowgroupcategories', 'wysiwyg',
        'staffreports', 'staffstats', 'userscandisabledevicedetection', 'watchlistnotification_delay',
        'masqueradingreasonrequired', 'masqueradingnotified', 'searchuserspublic',
        'eventloglevel', 'eventlogexpiry', 'sitefilesaccess', 'exporttoqueue',
    );

2 Add $CFG->mathjaxpath and $CFG->mathjaxconfig to the end of /lib/config-default.php:

 /**
 * @global string $cfg->mathjaxpath Determines the path to CDN or server-local installation of MathJax.js.
 * If MathJax is enabled, use to configure path to MathJax.
 */
$cfg->mathjaxpath = '//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&delayStartupUntil=configured';

/**
 * @global string $cfg->mathjaxconfig Determines MathJax configuration options.
 * The default MathJax configuration should be appropriate for most users, but MathJax is highly configurable
 * and any of the standard MathJax configuration options can be added here.
 */
$cfg->mathjaxconfig = '
MathJax.Hub.Config({
TeX:{extensions: ["mhchem.js"]}
});
MathJax.Hub.Configured();
';

NB the protocol-neutral MathJax URL should work with either http: or https: Mahara sites.

3. Add the following to htdocs/lib/web.php immediately before TinyMCE is first included around line 111:

    if (get_config('mathjax')) {
        $headers[] = '<script>'.get_config('mathjaxconfig').'</script>';
    }

and just after $javascript_array[] = $jsroot . 'keyboardNavigation.js'; around line 280 add:

    if (get_config('mathjax')) {
        $javascript_array[] = get_config('mathjaxpath');
    }

4. To /lang/en.utf8/admin.php add:

$string['mathjax'] = 'Enable MathJax';
$string['mathjaxdescription'] = 'MathJax enables mathematical and science equations to be displayed';
$string['mathjaxconfig'] = 'MathJax configuration';

5 Create /lang/en.utf8/help/forms/siteoptions.mathjax.html with the content:

<!-- @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later -->
<!-- @copyright For copyright information on Mahara, please see the README file distributed with this software. -->
<h3>MathJax</h3>
MathJax is a JavaScript display engine for TeX, LaTeX, AsciiMath and MathMl notation which allows mathematics and science equations to be displayed in Mahara pages.
<p>MathJax allows the display of equations throughout your Mahara installation.</p>
<h4>MathJax URL</h4>
<p>The default configuration uses the latest stable version on the MathJax Content Distribution Network (CDN). http and https protocols may be used, as appopriate.</p>
<p>Alternatively, $cfg->mathjaxpath may use a local server installation of MathJax. See <a href="http://docs.mathjax.org/en/latest/installation.html">http://docs.mathjax.org/en/latest/installation.html</a></p>
<p>The default includes a commonly used configuration allowing input of MathML, AsciiMath, TeX or LaTeX and processing to MathML or HTML-CSS</p>
<h4>MathJax additional configuration</h4>
<p>Additional MathJax configuration can be added to $cfg->mathjaxconfig. The default MathJax configuration should be appropriate for most users, but MathJax is highly configurable and any of the standard MathJax configuration options can be added here.</p>
<p>By default, includes the mhchem extension which adds the \ce macro for chemical formulae. See <a href="http://docs.mathjax.org/en/latest/tex.html#tex-and-latex-extensions">http://docs.mathjax.org/en/latest/tex.html#tex-and-latex-extensions</a>.</p>

Aaron Wells (u-aaronw)
Changed in mahara:
status: New → In Progress
importance: Undecided → Wishlist
Aaron Wells (u-aaronw)
tags: added: mathjax mhchem tinymce
Revision history for this message
Geoffrey Rowland (rowland-geoff) wrote :

Duplicates and extends (additional mhchem support) https://bugs.launchpad.net/mahara/+bug/730373 Mathematical notation in Mahara with MathJax

See also this discussion: https://mahara.org/interaction/forum/topic.php?id=3759 Developers - Math on Mahara

Would also work nicely alongside the TinyMCE4 MathSlate plugin https://github.com/dthies/tinymce4-mathslate

Revision history for this message
Geoffrey Rowland (rowland-geoff) wrote :

...and for Mahara 15.04 or later use a switchbox, rather than checkbox, in /htdocs/admin/site/options.php

Revision history for this message
Geoffrey Rowland (rowland-geoff) wrote :

Attached is a patch against Mahara 15.10 dev

See also: https://github.com/geoffrowland/mahara/tree/bug1464858

If it would help, I could also try to submit this via Gerrit (though I'm a Gerrit novice!)

To test

To a text box, using code view, add a simple LaTeX expression such as \( \alpha \) and save
This should be displayed as an alpha symbol, if MathJax display is enabled in the site options.

Similarly, a simple mhchem expression such as \(ce{ H2SO4 }\) should be displayed with appropriate chemistry formatting i.e. with subscripted 2 and 4.

Revision history for this message
Mahara Bot (dev-mahara) wrote : A patch has been submitted for review

Patch for "master" branch: https://reviews.mahara.org/4913

Revision history for this message
Aaron Wells (u-aaronw) wrote :

Hi Geoffrey,

Thanks for contributing your code to the community! Sorry it took so long to get back to you on this. I was in the process of upstreaming your earlier (non-patch-file) code and got sidetracked, then this slipped through the cracks.

It does make things easier for us if you push things directly into Gerrit, but we understand that git in general has a steep learning curve, and Gerrit even moreso. So we're happy to accept code in other means.

I've pushed your patch file up into Gerrit: https://reviews.mahara.org/4913

Cheers,
Aaron

Aaron Wells (u-aaronw)
tags: added: no-behat-needed
Changed in mahara:
milestone: none → 15.10.0
Revision history for this message
Aaron Wells (u-aaronw) wrote :

To test:

1. Go to Administration -> Configure Site

2. Expand "General Options"

3. Enable mathjax switchbox

5. Create a page, put a text box on it, and in the text box's TinyMCE field, enter this text:

\( \alpha \) \(ce{ H2SO4 }\)

Expected result: When you save the text box, the block will display an alpha symbol and a chemical formula with the 2 and 4 in subscript.

Revision history for this message
Mahara Bot (dev-mahara) wrote :

Patch for "master" branch: https://reviews.mahara.org/4926

Revision history for this message
Aaron Wells (u-aaronw) wrote :

Patch 4926 is for adding the MathSlate TinyMCE plugin. I've spun that off into a separate Launchpad issue: Bug 1472446.

Revision history for this message
Mahara Bot (dev-mahara) wrote : A change has been merged

Reviewed: https://reviews.mahara.org/4913
Committed: https://git.nzoss.org.nz/mahara/mahara/commit/5a7b0f81c574e5d02b56015f8453f753eb503dc4
Submitter: Aaron Wells (<email address hidden>)
Branch: master

commit 5a7b0f81c574e5d02b56015f8453f753eb503dc4
Author: Geoff Rowland <email address hidden>
Date: Wed Jun 17 14:27:51 2015 +0100

Add MathJax and mhchem support to Mahara (bug #1464858)

This provides support for rendering LaTeX into math
& chemistry equations.

Change-Id: I34a30296f46ba7eeb635e6924172ddc6e65175c3
Signed-off-by: Aaron Wells <email address hidden>

Revision history for this message
Aaron Wells (u-aaronw) wrote :

Okay, Mahara 15.10 now supports MathJax! Thanks for the patch, Geoffrey.

Changed in mahara:
status: In Progress → Fix Committed
tags: added: nominatedfeature
Changed in mahara:
status: Fix Committed → Fix Released
no longer affects: mahara/15.10
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.