# Extension:MathEasy

Other languages:
English • ‎Türkçe • ‎српски / srpski • ‎日本語 • ‎ꯃꯤꯇꯩ ꯂꯣꯟ • ‎한국어
MediaWiki extensions manual
MathEasy
Release status: unmaintained
Implementation Tag
Description Temporary LaTeX math extension
Author(s) (Ejcaputotalk)
Database changes No
Example ${\displaystyle e=mc^{2}}$

This extension was developed after a frustrating experience in trying to get the main Math Extension to work, as it appears to have been drastically changed recently and isn't fully documented. This version is extremely simple, and probably shouldn't be used on large wikis nor on wikis which have untrusted editors. However, it uses the same LaTex transformation code which apparently will be used in the Math Extension, i.e. Mathoid, so it should be compatible in the future.

## Usage

The intent was to implement a [itex] tag as close as possible in operation to the Math Extension's tag. This version has some minor enhancements to help with the formatting, but aren't necessary to use:

• To set the graphics type to SVG: <math type=svg>
• To set the image width in pixels: <math size=500>

Basically what this does is use the MD5 hash of the formula being rendered as a key for naming the files to be used. The files are kept in a directory under your main wiki directory (\$IP/matheasy), and have the following files:

• {md5}.txt - LaTeX formula to be converted
• {md5}.sh - Command used to convert the formula from LaTeX to graphics
• {md5}.png or .svg - Graphics file

Once the file is converted, it is saved until the next time it might be needed. A link to the graphics file is returned in the form of a <img> tag. Due to possible slow processing speeds, it is possible that the first time you try to view the image, it will show as a broken link because the process isn't completed. This is solved by refreshing the page. This issue is possible to fix if necessary, but for this simple extension it didn't seem worth the effort.

## Installation

Insert the code near the end of the LocalSettings.php file, or copy the code to another file and include it, as you wish.

It is necessary to have access to a Mathoid server, which is easy to setup, see:

You can also use someone else's existing Mathoid server, but there is no guarantee that it will be available when you need it. Here are two examples which may be used for limited testing:

It is advisable to read Manual:Mathoid first. To set Mathoid up on Ubuntu 16.04 (and probably many others):

## Prerequisites, as root user:
apt-get install nodejs nodejs-legacy nodejs-dev npm mocha librsvg2-dev pkg-config git

## To install, using a non-privledged user:
cd ~/
git clone https://github.com/wikimedia/mathoid.git
cd mathoid
npm i
npm test
nodejs server.js

## To test:
curl -d 'q=E=mc^2' localhost:10044/mml ## produces an MML result
curl -d 'q=E=mc^2' localhost:10044/png >xx.png ## produces PNG file
curl -d 'q=E=mc^2' localhost:10044/svg >xx.svg ## produces SVG file, openable in inkscape or a browser

## Code

//// extension for MathEasy
if ( !defined( 'MEDIAWIKI' ) ) {
die( 'This file is a MediaWiki extension, it is not a valid entry point' );
}

\$wgExtensionCredits['parserhook'][] = array(
'name' => 'MathEasy',
'version' => '1.0',
'url' => 'https://www.mediawiki.org/wiki/Extension:MathEasy',
'author' => 'Ejcaputo',
'description' => "Temporary replacement for the Math Extension."
);

\$wgHooks[ 'ParserFirstCallInit' ][] = "ExtMathEasy::setup";
class ExtMathEasy {
public static function setup( &\$parser ) {
// register each hook
\$parser->setHook( 'math', 'ExtMathEasy::parserMathEasy' );

return true;
}

public static function parserMathEasy( \$data, \$attribs, \$parser, \$frame ) {
\$formula = \$data; // formula to be rendered
if(!isset(\$formula)) return "<b>NO FORMULA</b>";
if(isset(\$attribs['size'])) \$width = \$attribs['size']; // width of image shown (best with svg)
if(isset(\$attribs['type'])) \$imgext = \$attribs['type']; // mathoid conversion type (png or svg)
if(!isset(\$imgdir)) \$imgdir = 'matheasy/'; // directory for images
if(!isset(\$imgext)) \$imgext = 'png'; // image type default
if(!isset(\$mathoid)) \$mathoid = 'raptor575.startdedicated.com:10044';
//if(!isset(\$mathoid)) \$mathoid = 'http://mathoid.testme.wmflabs.org';
if(!file_exists(\$imgdir)) mkdir(\$imgdir); // create image directory
\$md5 = md5(\$formula); // calculate file name of image
\$fname = \$imgdir.\$md5.'.'.\$imgext;
if(!file_exists(\$fname)) { // create image if doesn't already exist
// put formula in .txt file, then process it
\$ffile = fopen(\$fname.'.txt', 'w');
fwrite(\$ffile, "q=".\$formula."\n");
fclose(\$ffile);
\$cmd =
'curl -d "@-" '.\$mathoid.'/'.\$imgext.' >'.\$fname.
' <'.\$fname.'.txt';
\$ffile = fopen(\$fname.'.sh', 'w');
fwrite(\$ffile, \$cmd."\n");
fclose(\$ffile);
exec(\$cmd,\$cmdout,\$cmdret);
}
global \$wgScriptPath;
\$retval = '<img src="'.\$wgScriptPath.'/'.\$imgdir.\$md5.'.'.\$imgext.'"'.
(isset(\$width) ? ' width="'.\$width.'"' : '').
(isset(\$forumla) ? ' alt="'.\$formula.'"' : '').
'>';
return \$retval;
}
}
//// end extension MathEasy