MediaWiki and LaTeX on a host with shell access

There are a number of methods for including mathematical formulae in mediawiki using LaTeX notation. Examples can be seen in LaTeX Rendering Method Examples.

Install mediawiki
Installation of mediawiki proceeds with instructions such as these: How to Install MediaWiki on Hostmonster

The simplest and universal option: mathtex.cgi
Using mathTeX you can test out LaTeX in your web server almost immediately, using the generosity of forkosh.dreamhost.com to render the PNG images for you. See the advice in the section mathTeX web services.

This section will explain, more specifically, how to better integrate mathtex.cgi in your MediaWiki installation. After this step is successful, you can gradually move to using LaTeX on your account, as explained in the following sections.

You will need this file (and embedded instructions):   tags.

$wgExtensionFunctions[] = "wfMtag";

function wfMtag { global $wgParser; $wgParser->setHook( "m", "returnMtagged" ); }

function returnMtagged( $code, $argv) {

$txt='';
 * 1) if you have mathtex.cgi installed:
 * 2) $txt='';
 * 3) OR if you want to temporarily test a public mathtex.cgi:

return $txt; }

?>

mimetex.cgi
As an alternative method that still doesn't require LaTeX to be installed, see "LaTeX on a shared host" (which should probably be merged to this page).

Installing LaTeX
The above setup has the advantage of being simple and quick, but your wiki will depend on another web service being available and responsive. An independent solution will also give you more control.

However, a prerequisite for going solo is to have LaTeX installed. If the shell command which latex does not give you an answer, chances are it is not available at your site, and so you will need to download and install it yourself by following the instructions in this section. On the other hand, if latex is already installed you can safely skip it.

TeX Live distribution
The LaTeX distribution called "TeX Live" is relatively easy to install. Follow the instructions at http://www.tug.org/texlive/acquire.html and http://www.tug.org/texlive/quickinstall.html.

The following sections will try to break it down a little, but if there are any discrepancies follow the above TeX Live installation guides.

Downloading
With shell access from the server, from your command line, in a directory of your choice issue the following command wget http://mirror.ctan.org/systems/texlive/tlnet/2008/install-tl-unx.tar.gz

Unpacking
tar -xvzf install-tl-unx.tar.gz This creates a directory install-tl, with an executable of the same name inside.

Installing
Follow the above-mentioned TeX Live installation guide from here. The installation takes about 1 hour. If you get disconnected or the process gets killed for any reason, it is safe to start again. TeX Live installation is much simpler than teTeX, so this is all that should be needed to install LaTeX. If you want to give teTeX (an older and discontinued distribution) a try, see the next section, otherwise you may skip it.

Downloading
With shell access from the server, do from your command line:

wget ftp://tug.ctan.org/tex-archive/systems/unix/teTeX/current/distrib/tetex-src-3.0.tar.gz

wget ftp://tug.ctan.org/tex-archive/systems/unix/teTeX/current/distrib/tetex-texmf-3.0.tar.gz

Unpacking
In your home directory,

gunzip tetex-src-3.0.tar.gz tar xvf tetex-src-3.0.tar.gz

This produces a directory tetex-src-3.0. There is a file QuickInstall within, which your should probably read. Here is another summary of installation, a little bit less cryptic.

mkdir -p /home/username/local/teTeX/share/texmf-dist Then move the massive tetex-texmf-3.0.tar.gz to that directory, gunzip it and untar it, then either delete it or move it back out. This step has installed a bunch of files that are independent of the compilation step. The compilation step comes next, and that will fill out the teTeX directory that you have created.

Compiling
If uname -a</tt> reveals that you are on a 32-bit server, try within tetex-src-3.0</tt> executing ./configure --prefix=/home/username/local/teTeX -without-texinfo -with-x=no At hostgator, you will find that your jailshell access does not allow access to gcc</tt>, so may need to ship off the directory to your local linux PC and attempt the compilation there. The ./configure</tt> step may reveal you need to install packages for flex, bison, byacc, libncurses5-dev before the next step is successful: make world That above step will take several (many) minutes. If it is successful (and, if need be, you have shipped the directory back to the server) then finish the installation on the server with: make all install

texconfig
The usual advice to finish an installation of LaTeX is to run texconfig</tt>, which brings up an interactive menu. But if you are installing LaTeX on a webserver, for the purpose of making little PNGs, you may not need to change anything with texconfig</tt>.

texhash
If you have your own personal style file, say titled myown.sty</tt>. Then cd /home/username/local/teTeX/share/texmf-dist/tex/latex Make a directory there: mkdir myown</tt> and drop myown.sty</tt> into it. Then run texhash</tt>, so that LaTeX can find the new style file. Even without installing a style file, I believe the usual installation advice is to run texhash</tt>.

VARTEXFONTS
Execution of texhash</tt> may also reveal an attempt to access /var/tmp/texfonts</tt>. At hostgator, that presents a problem, at hostmonster it does not. If a problem arises (or even before arises) you may want to: grep VARTEXFONTS /home/username/local/teTeX/share/texmf/web2c/texmf.cnf You should see a line VARTEXFONTS = /var/tmp/texfonts You can remedy this with mkdir /home/username/texfonts texconfig font vardir /home/username/texfonts grep should now reveal VARTEXFONTS = /home/username/texfonts</tt>.

Or follow | this teTeX advice, and change VARTEXFONTS before compilation.

64-bit servers
For 64-bit servers, the ./configure</tt> step will need to be: CC="gcc ${BUILD64}" CXX="g++ ${BUILD64}" USE_ARCH=64 \ ./configure --prefix=/home/username/local/teTeX \ –enable-shared \ –without-texinfo \ –with-x=no \ –with-system-ncurses \ –with-system-zlib && [ -f texk/libtool ] && sed -i “/sys_lib_search_path_spec=/s:/lib:&64:g” texk/libtool;

That step can be found at Installing LaTeX on hostmonster, with one exception. The exception is to NOT put a trailing slash (as appears on 12 July 2008): ./configure --prefix=/home/username/local/teTeX/ Instead, the correct line should be: ./configure --prefix=/home/username/local/teTeX

I find 127 binaries installed in /home/youraccount/local/teTeX/bin/x86_64-unknown-linux-gnu If I use the trailing /</tt> in the <tt>--prefix</tt>, I get 119. If you read the QuickInstall included with the distro, you will see the advice to ./configure --prefix=/usr/local/teTeX >& configure.log You should probably include the <tt> >& configure.log </tt> in the advice from Burad's blog.

Compiling your own mathtex.cgi
To gain independence from external mathtex web services, or if you want to customise the mathtex setup, you will need to compile it.

Before you do, ensure that mathtex's dependencies (e.g. <tt>latex</tt> and <tt>dvipng</tt> executables) are in the PATH. To check that this is the case, enter e.g. which latex in the command line. If this does not output the directory where such executable is found, you will need to add said directory to the <tt>PATH</tt> environment variable, by adding a line similar to the following to <tt>.bashrc</tt> in your home directory PATH=$PATH:$HOME/texlive/2008/bin/x86_64-linux

You can then follow these instructions to compile, customize and install your own <tt>mathtex.cgi</tt>.

Remember to change the relevant section of the file <tt>Mtag.php</tt> at the top of this article to point to your local <tt>mathtex.cgi</tt>.

Custom styles
You may want to allow for certain LaTeX commands as defined in your personal style file. Suppose you have a style file <tt>mystyle.sty</tt> with the command definitions. Depending on where your LaTeX is installed, you should find many directories containing <tt>.sty</tt> files in: /home/myaccount/local/teTeX/share/texmf-dist/tex/latex

Within that directory, <tt>mkdir mystyle</tt> and then move your <tt>mystyle.sty</tt> into <tt>mystyle</tt>.

In order for LaTeX to find the newly created <tt>mystyle</tt>, you need to simply run <tt>texhash</tt> from the command line.

Here is an example script to compile <tt>mathtex</tt>:

cc mathtex.c -DLATEX=\"$(which latex)\" -DDVIPNG=\"$(which dvipng)\" \ -DPNG -DUSEPACKAGE=\"mylittlefile\" -o mathtex.cgi

where within <tt>mylittlefile</tt>, there could be just one line: "\\usepackage{mystyle}\n"

The traditional
In addition to the requirements you needed for <tt>mathtex</tt>, you will also need to have <tt>latex</tt> and <tt>dvipng</tt> in the PATH of the web server process. You will likely also need to compile <tt>texvc</tt>, for which you will have to install <tt>ocaml</tt> if it's not already available at your site.

Installing OCaml
Objective Caml is obtained from http://caml.inria.fr/. A number of pre-compiled binaries are avaiable, or follow the instructions in the INSTALL file that comes with the tarball. The only thing to be aware of is that, like for the LaTeX install above, in a shared environment where you don't have superuser access you will probably need to change the various directories (during the configuration stage) to subdirectories under your $HOME directory.

After compiling and installing, make sure the binary <tt>ocaml</tt> is in your $PATH; see above for how to change <tt>.bashrc</tt> to this effect.

Making <tt>texvc</tt>
Follow the instructions in the <tt>README</tt> file inside the <tt>math</tt> subdirectory of your mediawiki installation.

After <tt>texvc</tt> is compiled, OCaml will not be used again by the wiki for the purpose of rendering TeX formulae.

wikitex
If you want to extend your LaTeX with AMS-LaTeX or other styles, an alternative to <tt>mathtex</tt> is <tt>wikitex</tt>, available from http://wikisophia.org/wiki/Wikitex. Suppose you want to extend <tt>wikitex</tt> with your own styles, similar to what was described above for <tt>mathtex</tt>. Here is an example of using <tt> <ma>...</ma> </tt> tags for that purpose:

<tt>'ma'  => 'strMa',</tt> function strMa($str) { global $objRend; return $objRend->strRend($str, array('class' => 'ma')); }
 * 1) <tt>cp wikitex.math.inc.tex wikitex.ma.inc.tex </tt>
 * 2) add your command definitions within the preamble of <tt>wikitex.ma.inc.tex</tt>
 * 3) alternatively add your package to the list within <tt>\usepackage{...</tt>
 * 4) Within <tt>wikitex.inc.php</tt>, add this line to the definition of <tt>$arrRend</tt>:
 * 1) Within <tt>wikitex.php</tt>, add this function

It is possible that when the webserver invokes LaTeX, the webserver won't have permissions to make missing fonts, and you will see an error message. In the <tt>tmp</tt> directory within <tt>wikitex</tt> you may find long file names, for example <tt>cd02e2b844e50595b3d569247d533cc9</tt> without the corresponding <tt>cd02e2b844e50595b3d569247d533cc91.png</tt>. Entering the shell command <tt>latex cd02e2b844e50595b3d569247d533cc9</tt> may give some messages about the missing fonts, and then make the missing fonts. The wiki invocation may now be able to render the PNG.

In order to render a page full of PNGs <tt>wikitex</tt> will call <tt>wikitex.sh</tt> for each PNG. A shared host may be very slow in completing the many calls to <tt>wikitex.sh</tt>, perhaps deliberately by its configuration. PHP may then time out. Use of <tt>mathtex</tt> avoids this problem.