This is the LatexRender-ng Project Install Instructions




Sample page





Browse code

Rebort a bug

The author

Requirements common to all installations

LatexRender is capable of rendering \LaTeX fragments into png, gif or svg images seamlessly and beautifully embedded in your pages. For that you will also need the following pieces of software:

  • A decent installation of \LaTeX, including the preview, and AMS packages. Note: you can do without the AMS packages, but you will have to remove the corresponding \usepackage commands from class.latexrender.php file. However, you cannot do without the preview package, and the file preview.sty must be present.
  • If you are rendering png or gif images, the dvipng program must have been installed
  • If you are planning to experiment with svg images, then the dvisvgm program must have been installed. Note: in this case, your pages must be coded in xhtml instead of plain html and will not work with Internet Explorer, but should work beautifully with Firefox, Opera and Safari.
  • Your webserver should be able to execute php 5 code.
  • The graphics library gd should be installed and the gd php extension should be loaded. Otherwise, the ImageMagick tools should be present as a fallback. In this case, please adjust the path to the identify program in class.latexrender.php

Installation in Moodle

To install this filter in Moodle, simply unpack the the distribution inside Moodle's filter folder. This will create the latexrender subfolder. The default settings in class.latexrender.php should be go enough to start with, namely the usage of png images stored in a subfolder of the datadir.

Next, log on to Moodle as the administrator and go on to Site Administration/Modules/Filters/Manage filters. You should see the filter Latexrender in the filters list. Click on the eye icon to activate the filter.

Optionally, you can go on to Site Administration/Modules/Filters/Manage filters/Latexrender and change de filter defaults. Hit the Save Changes button when you are done. Please notice that if you change the image format after a while, you will have to clean the image cache manually.

If you are using Moodle 1.9.5+, you might want to apply this security patch on moodle to let LatexRender filter user's input to remove potentially harmful \LaTeX commands. To apply it, go to the root of moodle's installation tree, make a backup of the whole of moodle's code, then issue the following command:

patch -p1 < moodle-waldeck.patch

This assumes that the moodle-waldeck.patch file was placed in that same location. The patch file can be deleted afterwards. If the patch program reports errors or notices that certain hunks could not be applied, it might be the case taht you are trying to patch the wrong version of Moodle. In that case, please contact me for possible solutions, and roll back your Moodle code from the backup.

Installation in a regular website

What I mean by regular websites are those which were coded by you in plain html or php and do not possess a content-management infrastructure. In that case, you can still use LatexRender but you'll have to do some extra coding as explained below.

First unpack the distribution files somewhere in your website tree and locate the subfolders named pictures and tmp in latexrender. You must change the ownership of these folders to the same user your webserver runs on. In some linux distros, this user might be apache or www-data. This is required so that LatexRender can have write access to these folders.

Next, open up the file named latex.php whose contents are reproduced below

// To customize the filter funcionality, please consider setting the
// the parameters located on the top of class.latexrender.php
require_once('[system path to latexrender installation]/class.disk_cache.php');
require_once('[system path to latexrender installation]/class.latexrender.php');

function latex_content ($text)
$latexrender_path = '[system path to latexrender installation]';
$latexrender_path_http = '[http path to latexrender installation]';
$cache = new DiskImageCache($latexrender_path.'/pictures', $latexrender_path_http.'/pictures');

$latex = new LatexRender($cache,$latexrender_path_http, $latexrender_path."/pictures",$latexrender_path_http."/pictures",$latexrender_path."/tmp"); $latex->_image_format = "png";

return $latex->latex_content($text);


In this file, replace the [System path to latexrender installation] and [http path to latexrender installation] with the appropriate values. For instance, suppose that your site is physically located in the folder /var/www/htdocs/mathsite and latexrender was unpacked inside this folder. In this case, the System path to latexrender should be /var/www/htdocs/mathsite/latexrender.

Important: As the superuser you should issue the following commands:

chown www-data:www-data /var/www/htdocs/mathsite/latexrender/pictures
chown www-data:www-data /var/www/htdocs/mathsite/latexrender/tmp

so that LatexRender can write to those folders. Furthermore, the http path to latex render should probably be equal to /mathsite/latexrender

Finally assume that you have a page named fibonacci.html and you want to insert beautiful \LaTeX formulas in it. Then write down the following code:

include_once("[System path to latexrender installation]latex.php");
$text = file_get_contents("fibonacci.html");
echo latex_content($text);

and name it fibonacci.php. Now instead of linking to to fibonacci.html, change your website's links to point to fibonacci.php instead.

Not you can insert \LaTeX fragments like those in the examples page in your page using the [tex]...[/tex] constructs. For instance, if you type [tex]F_{n}=F_{n-1}+F_{n-2},\ n\ge 2[/tex] inside fibonacci.html, you should see F_{n}=F_{n-1}+F_{n-2},\ n\ge 2.

Customizing your installation

The topmost part of class.latexrender.php contains several variable definitions and establishes their default values. Although the default values could be suitable to most installations, you might want to tweak with them. Please notice that these values are overriden by the values which were set in the filter settings dialog.

If your \LaTeX formulas appear too bit or too small, try changing the values of _default_font_size (in pt) and/or _formula_density (in DPI).

The _xsize_limit and _ysize_limit determine the maximum absolute site of the image produced by rendering a single \LaTeX fragment.

// ------------------------------------------------------------------------
// Variable Definitions - you may adjust these values to suit your install
// ------------------------------------------------------------------------
var $_popupwindow = true; // Set this to true if you want popup windows
var $_default_bgcolor="#FFFFFF"; // Default background color (transparent)
var $_image_format = "png"; // default image format: png, gif or svg
var $_html_use_file_ext = true; // htmo uses image format extension?
var $_html_input = true; // Set to true if you use textarea for input
var $_formula_density = 120;
var $_xsize_limit = 1024;
var $_ysize_limit = 500;
var $_string_length_limit = 5000;
var $_default_font_size = 12;
var $_align_to_baseline = true;
var $_latexclass = "article"; //install extarticle class if you wish to have smaller font sizes
var $_font_encoding = "utf8"; // Default character encoding
var $_splashwindow = true; // Disable if you don't want the splash window
// ------------------------------------------------------------------------
// These might also have to be changed depending on you operating system
// and/or distro
// ------------------------------------------------------------------------
var $_latex_path = "/usr/bin/latex"; // path to latex executable
var $_dvipng_path = "/usr/bin/dvipng"; // path to dvipng executable
var $_identify_path="/usr/bin/identify"; // path to identify exec.
var $_dvisvgm_path="/usr/bin/dvisvgm"; // path to dvi->svg converter


Copyright (c) 2009, Waldeck Sch├╝tzer. All rights reserved. Released under the terms of the GPL. Web hosting by SourceForge.