How to Get Math Support in MediaWiki

March 7th, 2008 · , , , , , ,

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.

View Comments to “How to Get Math Support in MediaWiki”

  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

  2. Adison says:

    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.

  3. Adison says:

    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 . ‘” />’;

  4. help says:

    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.

  5. Michael Gustafson says:

    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!

  6. Jack Boyce says:

    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.

  7. Zarjay says:

    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!

  8. #Remove all white space from the tex expression
    The code works fine for maths without white space, but you need to strip any white space from the maths to pass it to the cgi script like that, e.g. for matrices, or multi-line or any maths with spaces in it.

    So add an extra line:

    # and pass result to mimetex for rendering instead

    # – see http://zarjay.net/2008/03/07/how-to-get-math-support-in-mediawiki/
    $tex = preg_replace(‘/\s\s+/’, ”, $tex);
    return ”;
    }}

    which I found fixed it.

  9. Sorry that post got scrambled somehow. Here it is again, complete function:

    function renderMath( $tex ) {
    ###Hacking Math.php
    #global $wgUser;
    #$math = new MathRenderer( $tex );
    #$math->setOutputMode( $wgUser->getOption(‘math’));
    #return $math->render();
    #Remove all white space from the tex expression
    # and pass result to mimetex for rendering instead
    # – see http://zarjay.net/2008/03/07/how-to-get-math-support-in-mediawiki/
    $tex = preg_replace(‘/\s\s+/’, ”, $tex);
    return ”;
    }}

  10. Still getting scrambled. The last line should read the same as before, I’ll try again:

    $tex = preg_replace(’/\s\s+/’, ”, $tex);

    return ”;

  11. Sorry – can’t post it for some reason – but second last line is fine. Just insert that before the return.

  12. Daniel says:

    Thanks so much for your guide! I probably would have never figured it out without your help. I owe you a beer.

  13. Andrew says:

    Hi, unfortunately my server doesn’t enable ssh.
    therefore I cannot compile necessary dependencies for the texvc.
    is there any way to obtain working binaries of latex, dvips, convert and gs ?
    without these it won’t render the png files…

    I have a ubuntu machine and my server uses linux.

    thanks for your help!

  14. TJ says:

    Does it work the same for installation on a windows server? The binaries you mentioned are only for Linux.

  15. Jaime Melis says:

    Thank you very much for guide! It would have never occurred to me that you can make it work with mimetex! thinking out of the box ;) took me less than half an hour to make it work! brilliant!

  16. Jaime Melis says:

    Thank you very much for guide! It would have never occurred to me that you can make it work with mimetex! thinking out of the box ;) took me less than half an hour to make it work! brilliant!

  17. Zarjay says:

    Dunno. If there are Windows binaries available, I assume it should work the same.

  18. Zarjay says:

    Dunno. If there are Windows binaries available, I assume it should work the same.

  19. Zarjay says:

    No prob! Glad I could help. ^_^

  20. Zarjay says:

    No prob! Glad I could help. ^_^

  21. chariya1 says:

    Thank you for saving me a lot of grieve! I tested your approach and a simple test equation rendered quite nicely. I am using media Wiki 1.15.1 version. The renderMath function for this version has two parameters. So not sure if that would cause any problem. Here is the code

    public static function renderMath( $tex, $params=array() ) {
    #global $wgUser;
    #$math = new MathRenderer( $tex, $params );
    #$math->setOutputMode( $wgUser->getOption('math'));
    #return $math->render();
    return '<img src=”http://bluenanta.com/cgi-bin/mimetex.cgi?' . $tex . '” alt=”" />';

    Thanks again for your service to the community :-)
    pax

  22. chariya1 says:

    Thank you for saving me a lot of grieve! I tested your approach and a simple test equation rendered quite nicely. I am using media Wiki 1.15.1 version. The renderMath function for this version has two parameters. So not sure if that would cause any problem. Here is the code

    public static function renderMath( $tex, $params=array() ) {
    #global $wgUser;
    #$math = new MathRenderer( $tex, $params );
    #$math->setOutputMode( $wgUser->getOption('math'));
    #return $math->render();
    return '<img src=”http://bluenanta.com/cgi-bin/mimetex.cgi?' . $tex . '” alt=”" />';

    Thanks again for your service to the community :-)
    pax

  23. Phil says:

    Thx A LOT!

    Instructions in the readme file were so unclear. I'm glad to have found your blog. It worked in minutes.

  24. Phil says:

    Thx A LOT!

    Instructions in the readme file were so unclear. I'm glad to have found your blog. It worked in minutes.

  25. Zarjay says:

    You're welcome! I'm glad my post was helpful. :D

  26. Zarjay says:

    You're welcome! I'm glad my post was helpful. :D

  27. Zarjay says:

    You're welcome! I'm glad my post was helpful. :D

Leave a Reply

blog comments powered by Disqus