Saturday, September 13, 2008

Enable TeX

Add tex-style math expression support functionality into MediaWiki.
Here is the complete recipe to add TeX support.

The official way to do it based on texvc which is written on Caml, so you need texvc binary (try to find it!) or calm compiler binary to compile texvc, so
  1. compile caml compiler with c++ compiler;
  2. compile texvc with caml compiler;
  3. plus: adjust all paths into makefiles;
  4. install LaTeX and dvips;
  5. install ImageMagic (aka convert utility);
  6. install Ghostscript;
Official manual to enable TeX is here http://www.mediawiki.org/wiki/Manual:Enable_TeX


I prefer more convenient way to add tex math expressions into my wiki:
  1. download mimeTeX from http://www.forkosh.com/mimetex.html (binaries are also available there);
  2. extract mimetex.cgi into cgi-bin directory;
  3. test it: go to http://host/cgi-bin/mimetex.cgi?\sin{x^2}+y^{\cos{x^3}}
On my server result was:
So, good news is that the mimetex is working, but it accessible for everyone over the web! Now I'll do few steps to integrate mimetex into MediaWiki and correct accessibility trouble later (this situation is convinient for testing).

  1. read http://www.mediawiki.org/wiki/Mimetex_alternative
  2. create mimetex.php into wiki_home/extensions/mimetex folder. See code below:

<?php

$wgExtensionFunctions[] = "MimetexExtension";

function MimetexExtension() {
global $wgParser;
# register the extension with the WikiText parser
# the first parameter is the name of the new tag.
# In this case it defines the tag <example> ... </example>
# the second parameter is the callback function for
# processing the text between the tags
$wgParser->setHook( "tex", "render_Mimetex" );
}

/**
* Renders $text in Mimetex
*/

function render_Mimetex($input, $argv, $parser = null) {

if (!$parser) $parser =& $GLOBALS['wgParser'];
// $img_url is the url the mimetex will be sent to.
// IMPORTANT!! The URL below should be the link to YOUR mimetex.cgi if possible
// $img_url = "http://www.forkosh.dreamhost.com/mimetex.cgi?".$input;
$img_url = "/cgi-bin/mimetex.cgi?".$input;

// Sets the output of the tex tag using the url from above, and the input as
// the Alt text. It's important to note that there is no error output added yet.
$output = "<img src=\"$img_url\" alt= \"$input\" />";

return $output;
}
?>

  1. Warning! $img_url in the listing above should point to minitex.cgi $img_url = "/cgi-bin/mimetex.cgi?".$input;

  2. add require("extensions/mimetex/mimetex.php"); at the end of LocalSettings.php
  3. set $wgUseTeX to true in LocalSettings.php
  4. try to add TeX expression somewhere in wiki, for example:
    <tex>\sin{x^2}+y^{\cos{x^2}}</tex>


TeX in MediaWiki

About TeX:



If somebody knows how to prevent direct access to mimetex.cgi from the web, please, notify me via comment to this post! I see the only way - make changes into mimetex.cgi code, but it is not very convenient to reapply my own changes when new version of mimetex.cgi will come. I've tried to solve this with Apache rewrite rules, but...

RewriteCond %{REQUEST_URI} .*mimetex\.cgi.* [NC]
RewriteRule ^(.*)$ index.php [F,L]

this is working, but how to split direct access and access from PHP?

No comments: