Integrating LatexRender in Moodle

Moodle (a Free, Open Source Course Management System for Online Learning) provides a Latex filter based on mimeTeX for displaying latex formulas in web pages. However, it has two drawbacks. Firstly, mimeTeX only supports a subset of latex, i.e. mathematical formulas. Also, the quality of generated images is not optimal.

A better solution is possible with LatexRender. This document describes how to integrate LatexRender in Moodle.

Requirements

  • This solution was implemented with Moodle 1.4.4.
  • The solution presented here works on Linux (debian). For a Windows solution, some changes should be made, e.g. to paths (see the ReadMe file included in LatexRender).
  • LatexRender needs a working installation of latex, ImageMagick and Ghostscript. If you are on a Debian box, $apt-get install etex-base imagemagick gs-common should be sufficient to install the required packages.
  • In the setup used here, the Moodle data directory cannot be accessed directly from the web (for security reasons). To access pictures generated in this directory, the script pix.php is called with the image location as slash argument (as is done by the standard tex filter). As a consequence, the variable $latexrender_path_http has to point to the directory where the latexrender php files are stored, and not to the directory where images are stored (see latex.php under « Modify some files » below). Also, slash arguments must be enabled.
    If your data directory is accessible from the web, the use of pix.php is not necessary (but it will still work). If you don’t want to use pix.php, modify the paths accordingly in latex.php. In this case, you won’t need to have slash arguments enabled.

Download, unpack and install LatexRender

  1. Create a directory named latexrender in /moodle/filter/, where moodle is the Moodle installation directory.
  2. Go to the LatexRender Homepage and download latexrender.zip.
  3. Unzip the file to a temporary directory.
  4. After unpacking the archive, there’s a directory named demo, go there.
  5. Copy latex.php, class.latexrender.php and latexcode.php to the directory /moodle/filter/latexrender you created in step 1 above.
  6. Create a directory named latexrender in /moodledata/filter, where moodledata is the Moodle data directory. In this directory, create two directories tmp and pictures, and make them writable by the web server (i.e. chmod 777).

Download additional files for Moodle

  1. Download the file latexrender_moodle.zip
  2. Extract latexrender.php to moodle/lang/en (if you want, you can also translate the content of this file and copy it to the appropriate language directory).
  3. Extract filter.php and pix.php to moodle/filter/latexrender

Modify some files

latex.php

  • Adjust the paths according to your setup, e.g. :
    $latexrender_path = "/home/www/moodledata/filter/latexrender";    $latexrender_path_http = "/moodle/filter/latexrender";
  • Adjust the path in the include_once instruction :
    include_once("class.latexrender.php");
  • By default, latex expressions delimited by [tex]...[/tex] will be interpreted by latexrender. If you want to use other delimiters, find the line with the preg_match_all instruction and change it to whatever you want to use.
  • Adjust the paths in the statement where the LatexRender-object gets instantiated :
    $latex = new LatexRender($latexrender_path."/pictures",$latexrender_path_http."/pix.php",$latexrender_path."/tmp");

class.latexrender.php

  • Depending on where latex, ImageMagick and Ghostscript are installed on your system, you may have to adjust the paths to the required binaries. You’ll find them using whereis latex, etc. :
   var $_latex_path = "/usr/bin/latex";
   var $_dvips_path = "/usr/bin/dvips";
   var $_convert_path = "/usr/bin/convert";
   var $_identify_path= "/usr/bin/identify";

filter.php

  • You can change the desired resolution, the maximum size, the font size, image format and so on. These settings are described further in the README file provided with LatexRender.
  • The setting $cachefiles = 1 enables caching of generated images (recommended for performance reasons). If you don’t want to cache images, comment out this line (don’t write $cachefiles = 0, as this will still be interpreted as true in latex.php !).

Enabling the filter and testing it

Before using the filter, you have to enable it in Moodle:

  • Log in as administrator into your Moodle server.
  • Go to « Configuration/Filters »
  • Activate the filter « LatexRender Filter ».

That’s it ! To test if this works, create a web ressource in a Moodle course, and try inserting the following into it :

[tex]
    frac{3}{4 pi} + sqrt{4 cdot x^2 + 12}
    lim_{n to infty}
    sum_{k=1}^n frac{1}{k^2} = frac{pi^2}{6}[/tex]

It should give you the following result :

Other examples can be found here.

Final words

  • This setup was not thoroughly tested. So unexpected problems may appear ;-). Please report any bugs/issues !
  • If you receive an error message or can’t get the test formula to display, please verify that you followed the instructions carefully (especially the paths). Also permissions of the tmp and pictures directories have to be set correctly !

Changes

  • 2004/04/19. Two small changes in filter.php :
    1. Added a check for the latex delimiters to avoid unnecessary work
    2. Before the Latexrender filter is invoked, Moodle replaces all newlines with <br />, which may have strange effects on the results (especially when using copy/paste to enter formulas). These <br /> are replaced by newlines.

Edit (2007/1/8) : As of Moodle 1.6, LatexRender has been integrated in the standard distribution of Moodle, and the above solution is no longer necessary.

Vous aimerez aussi...

5 réponses

  1. Juan Alfredo dit :

    I use Windows XP, with latest Moddle and I have a problem with pix.php: it does not show the pictures, it only shows a square with a red cross when it is supposed to be the picture.

    But Tex filter pix.php works fine. I have compared your pix.php with the Tex filter pix.php and it is all ok. I have even change your pix.php with the Tex filter pix.php but then it does not work.

    Any idea about what is happening?

    Thank you.

  2. Jacques Monnard dit :

    As mentioned above, the solution described was only tested on linux, so I can’t really help you on Windows issues. However, it seems that a new latex filter (similar to the above) will be part of Moodle 1.6, so you might just wait until this new version is out.

  3. Felice dit :

    Hello,
    I tried to install LatexRender on Mandrake 10.2 and Moodle 1.5.

    Images are correctly generated but in Moodle site I see only the text of the formula: why?

    Thanks in advance
    Felice

  4. Jacques Monnard dit :

    Hi Felice,

    You should check the paths used to display your images. You will probably have to change something depending on your setup.

    Jacques

  5. Wen dit :

    Does anyone install this in Moodle 1.5.4?? Any success? Thanks!

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *