March 7, 2008

How to Get Math Support in MediaWiki

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:

287
288
289
290
291
292
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 . '" />';
}

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:

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

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.

Comments

  1. hello - i found this site very useful - i had been trying to do this myself for the past couple of hours and had a little bit of luck - i followed the last part of this and am getting an error in the math.php file on the return part…
    says syntax error, unexpected T_STRING on line 302 not sure if you know anything about this

    thank you very much for posting this - this is the best documentation i’ve found on this

    …said sean.exposure on April 28th, 2008 @ 5:59 am
  2. This has been very useful. Thanks!
    One small improvement: if you’re trying to make this work with FCKeditor extension (see: http://mediawiki.fckeditor.net/index.php/FCKeditor_integration_guide ) you would need to use this line in /includes/Math.php:

    return ”;

    Notice the additional class and alt attributes in img element.

    …said Adison on May 12th, 2008 @ 12:15 am
  3. Let me try again to write the line that needs to be in /includes/Math.php for FCKeditor to work w/ Tex:

    return ‘<img class=”tex” alt=”‘ . $tex . ‘” src=”http://your_host/cgi-bin/mimetex.cgi?’ . $tex . ‘” />’;

    …said Adison on May 12th, 2008 @ 12:17 am
  4. Hi,
    I am new to mediawiki and has just installed it.
    I have read and installed texvc, latex, dvips, imagemagick etc but when I do …., nothing happen. The label displayed just as what I have type, it does not process the math tag. Do you know what I have missed out.

    I have search for many places but none mention this problem.

    …said help on June 3rd, 2008 @ 5:29 am
  5. Just wanted to say thank you for this guide! I have been wanting to get a few wikia set up but felt blocked by not being able to do the math (they’re engineering-related wikia…) without being root. And now in about 3 minutes I was able to both install MW 1.12 *and* get the math parts to work. Thanks much!

    …said Michael Gustafson on June 17th, 2008 @ 5:05 pm
  6. Thanks for the info, this was a huge help!

    I believe that if you go the mimetex route (as opposed to regular LaTeX), then it isn’t necessary to install texvc, convert, gs, or anything other than mimetex. (You still need to make the changes to Math.php and LocalSettings.php, of course). This simplifies the instructions above a little bit.

    …said Jack Boyce on June 27th, 2008 @ 7:42 pm
  7. Glad to hear that people found this useful! ^_^

    @help:
    Sorry, I’m not sure how to fix your problem; I’d need more information.

    @Jack:
    Oh cool, thanks for the tip!

    …said Zarjay on June 27th, 2008 @ 8:10 pm

Leave a Reply