Help talk:Running MediaWiki on Windows/math-dvipng.php

From Meta, a Wikimedia project coordination wiki
Jump to navigation Jump to search


This is a hack of Alternative Solution using dvipng instead of dvips + gs + convert. It does not require ghostscript or imagemagick. It doesn't check for correct image dimensions. The dvipng parameters are: -q -Q 8 -T tight -bg transparent -D 120 filename.dvi -o filename.png. It uses 8x8 antialiasing, because dvipng renders equations a little differently (not as sharp), and the 8x8 antialiasing seemed the best (see the file's talk page for details). If you want full 8-bit transparency, use Transparent instead of transparent.

0) Make sure you have latex and dvipng (e.g. MikTeX)

1) Copy the file FROM EDIT/SOURCE as includes/Math.php (without the pre and nowiki lines, of course)

2) Add the following to the end of LocalSettings.php (latex.exe and dvipng.exe without path suffice if you have them in your PATH):

$wgUseTeX	=	true;
$wgLaTexCommand	=	'insert_path_here/latex.exe';
$wgDvipsCommand	=	'insert_path_here/dvipng.exe';

3) Try it. My test is usually <math>E=mc^2</math> :)

Frigo 21:23, 14 November 2006 (UTC)Reply[reply]


I've sent a letter to the author of dvipng, here is it and the answer (I hope he doesn't mind I copied it here ^_^) (BTW, meanwhile I found out that a 8x8 antialiasing gives the best results without FreeType):

> > While playing around with the math rendering of MediaWiki, I
> > noticed that dvipng blurs the equations unlike dvips + gs +
> > convert (especially the upper line of the equal sign). I've
> > tried different settings of --gamma and -Q, but I got even
> > worse quality. How can I achieve similar results as with dvips?
> Try the latest version of dvipng, and make sure it links to (a recent
> version of) FreeType. That will fix the problem. The problem you are
> seeing is that in the (relatively) low resolution common in these kinds
> of images, sometimes a line gets split onto two pixel rows at much lower
> ink thickness. Recent versions of FreeType provide means of correcting
> this, libt1 and PK fonts just aren't good enough in this respect.
> If you cannot link to FreeType, the value '--gamma 1.4' or 1.5 improves
> things a bit. You can also try experimenting with the resolution, using
> -D117 instead of -D120 for example, but the exact results depend on the
> font, YMMV.
> Regards,
> Jan-Åke

Frigo 21:23, 14 November 2006 (UTC)Reply[reply]


Okay, if somebody wants to know, here is the diff file:

--- Math.php	Tue Nov 14 21:28:31 2006
+++ Math-dvipng.php	Tue Nov 14 21:51:13 2006
@@ -59,7 +59,7 @@
     var $_font_size = 10;
     var $_latexclass = "article"; //install extarticle class if you wish to have smaller font sizes
     var $_tmp_filename;
-        var $_image_format = "gif"; //change to png if you prefer
+    var $_image_format = "png"; //change to png if you prefer
     // this most certainly needs to be extended. in the long term it is planned to use
     // a positive list for more security. this is hopefully enough for now. i'd be glad
     // to receive more bad tags !
@@ -265,26 +265,8 @@
         // convert dvi file to postscript using dvips
-        $command = "\"".$this->_dvips_path."\" -q -E ".$this->_tmp_filename.".dvi"." -o ".$this->_tmp_filename.".ps";
+        $command = "\"".$this->_dvips_path."\" -q -Q 8 -T tight -bg transparent -D ".$this->_formula_density." ".$this->_tmp_filename.".dvi"." -o ".$this->_tmp_filename.".".$this->_image_format;
         $status_code = exec($command);
-        // imagemagick convert ps to image and trim picture
-        $command = $this->_convert_path." -density ".$this->_formula_density.
-                    " -trim -transparent \"#FFFFFF\" ".$this->_tmp_filename.".ps ".
-                    $this->_tmp_filename.".".$this->_image_format;
-        $status_code = exec($command);
-        // test picture for correct dimensions
-        $dim = $this->getDimensions($this->_tmp_filename.".".$this->_image_format);
-        if ( ($dim["x"] > $this->_xsize_limit) or ($dim["y"] > $this->_ysize_limit)) {
-            $this->cleanTemporaryDirectory();
-            chdir($current_dir);
-            $this->_errorcode = 5;
-            $this->_errorextra = ": " . $dim["x"] . "x" . number_format($dim["y"],0,"","");
-            return false;
-        }
         // copy temporary formula file to cached formula directory
         $latex_hash = md5($latex_formula);
@@ -316,7 +298,6 @@
-        unlink($this->_tmp_dir."/".$this->_tmp_filename.".ps");

Frigo 21:23, 14 November 2006 (UTC)Reply[reply]