I’ve been playing with MediaWiki a lot lately. (It’s the wiki software that Wikipedia uses, if you didn’t know.) It’s a nice piece of software, but compared to something like WordPress, it’s fairly complex to set up. Getting math support on MediaWiki was especially difficult for me. It took me a while, but I finally figured out how to do it, so I thought I ought to make a tutorial for everyone else. This is how to get math markup working in MediaWiki on a BlueHost site.

First of all, how does math markup in MediaWiki work? It takes LaTeX math expression and converts them into images using texvc. The texvc source comes with MediaWiki, but you’ll have to compile it yourself. Since texvc is partly written in OCaml, you’d need to install OCaml on your server just to compile it. However, there’s an easier way: just go to MediaWiki’s SourceForge site and download texvc binary. If the binary doesn’t work for you, however, you’ll have to download OCaml and compile your own binary. Once you have the texvc binary, put it in the/math/ folder of your MediaWiki installation.

Next, you’ll need to make sure that you have on your server the following: LaTeX, dvips, ImageMagick, and Ghostscript. An easy test to make sure if you have them installed or not is to SSH to your server and try typing in these commands:

latex
dvips
convert
gs

If your server’s like mine, then convert (ImageMagick) and gs (Ghostscript) should work for you. The other two should not. If not, then go get Ghostscript and ImageMagick.

The last thing you’ll have to worry about is LaTeX. If you have complete control over your server and feel like installing LaTeX on your own, then good luck. Otherwise, if you have a shared host like I do, you’ll want to install mimeTeX. However, mimTeX only works if you have CGI support.

Download mimeTeX, unzip it on your server, and then type this in the command line:

cc -DAA mimetex.c gifsave.c -lm -o mimetex.cgi

That should compile mimeTeX and create mimetex.cgi. Move that file to the cgi-bin. Also, make sure the permissions are properly set, or MediaWiki won’t be able to execute the file.

After you have mimeTeX installed, go to /includes/Math.php of your MediaWiki installation and find the following:

function renderMath( $tex ) {
   global $wgUser;
   $math = new MathRenderer( $tex );
   $math->setOutputMode( $wgUser->getOption('math')); //
   return $math->render();
}

Replace it with the following:

function renderMath( $tex ) {
   ###Hacking Math.php
   #global $wgUser;
   #$math = new MathRenderer( $tex );
   #$math->setOutputMode( $wgUser->getOption('math'));
   #return $math->render();
   return '<img src="http://your_host/cgi-bin/mimetex.cgi?' . $tex . '" alt="" />';
}

Don’t forget to replace the image URL with the proper location of the mimeTeX CGI.

Okay, now that you have all that, you should be ready to enable math support on your wiki. Just to recap, make sure you have the following:

  • texvc binary
  • LaTeX and dvips (or a suitable equivalent, such as mimeTeX)
  • ImageMagick
  • Ghostscript

Once that’s all setup, simply make sure the following is in your MediaWiki’s LocalSettings.php:

$wgUseTeX = true;

One last note: Make sure that you MediaWiki installation has /images/math/ and /images/tmp/ and that they’re both set to 755.

There, you should be done! If you want to test it out, on any of your wiki pages, try this:

\sum_{n=0}^{\infty} \frac{f^{(n)}(a)}{n!} (x-a)^{n}

By the way, just in case you were wondering how I figured this all out, here are the resources I used:

If I missed anything, or you’re still struggling to get math markup working in MediaWiki, let me know, and I can do my best to help.