I show in this tutorial how to make Latex under PUNBB forums using mimetex with a cache system. By reading many forums, a recurring question is asked: how to allow the users to make Latex? That proves quite useful in many cases, in particular when mathematical or physical problems are posed by the users.


Initially, it is necessary to download the mimetex utility. I suppose that you installed a recent version of Punbb in the repository {forum} of your site, in other words: https://yoursite/forum leads to your Punbb forum, if it is not the case you must change the ways of the repositories consequently in the following parts.

Mimetex is a cgi script. If you can’t use cgi scripts on your site(Ask your provider), you will not be able to use mimetex. Important points: in the root of your site

  • create a repository cgi-bin
  • create a repository latex
  • create a repository latex/pictures
  • check the rights of latex/pictures: this repository must be in mode writable, we will store in latex/pictures the images generated by mimetex: that’s the cache system. I saw lot of contributions which proposed to the installation of mimetex under PUNBB forums nevertheless in all these contributions anybody does not propose a cache system, the consequences are that the site is then overloaded since with each time a forum is published or read, all the latex formulas are again compiled by mimetex…

Compilation of mimetex

I suppose your domain is: www.mydomain.com ou www.mydomain.net … We will compile mimetex to obtain the cgi script which we will place in repository cgi-bin in the root of your site. You must compile your file {mimetex.c}:

gcc -static -DAA -DREFERER=\"mydomain\" -DCACHEPATH=\"../latex/pictures/\" mimetex.c gifsave.c -lm -o latex.cgi

Some explanations:

  • the option -DREFERERmakes it possible to specify your domain name that proves to be important insofar as you do not want that one uses your script apart from your site
  • the option -DCACHEPATH specifies the repository where the images generated by mimetex will be stored
  • latex.cgi is the file generated by compilation, you must place this file in repository cgi-bin.

Do not forget to place latex.cgi in repository cgi-bin.

Modification of PUNBB’s parser

We now will modify the code of Punbb so that it takes into account latex: all that will be put between [tex] and [/tex] will be latex. In the repository {include} of your Punbb forum, you must find the file parser.php.

  • Open up the file parser.php
  • Locate the function function do_bbcode($text)
  • This function end by return $text;
  • Before return $text; insert the following code, without forgetting to adjust the Configuration section:
//----PunBB 1.2.12 Module latex NadirSOUALEM -----
// Adjust your cgi-bin 
//----------------------FIN Configuration-------------------
for ($i=0; $i < count($tex_matches[0]); $i++)
	$pos = strpos($text, $tex_matches[0][$i]);
	$formula = html_entity_decode($tex_matches[1][$i]);			
	$formula = str_replace("\r","",$formula);
	$formula = str_replace("\n","",$formula);
	$formula = preg_replace("/ {2,}/", " ", $formula);
	$img_url = $latex.$formula;
	$text=substr_replace($text,"<img src=\"$img_url\" title=\"$formula\" alt=\"$formula\" align=\"absmiddle\" \>",$pos,strlen($tex_matches[0][$i]));