TeX Filtering in Windows 2003

TeX Filtering in Windows 2003

by Alyson Stibbard -
Number of replies: 3

Like many others, I have just been through considerable pain getting the TeX filter to run on Windows and I'd like to post my findings for the sake of anyone else in this unfortunate position. Most, though not all, of the problems involved user permissions or spaces in file paths.

The applications I've been using are:

Windows Server 2003 (Standard Edition)
IIS 6.0
Moodle 1.9.3+ (Build: 20081126)
PHP 5.1.5
IE 6.0
mimeTeX 1.40 (I think)
MikTeX 2.7
Ghostscript 8.6.3
ImageMagick 6.4.9-3 Q16 (2009-02-01)

Most of my efforts were focussed on getting LaTeX (in the form of MikTek) to run as opposed to mimeTeX (which comes with Moodle). For the record, the last 3 applications on the list above perform the necessary file conversions:

        MikTeX: .tex -> .dvi
        Ghostscript: .dvi -> .ps
        ImageMagick: .ps -> .gif

Ghostscript runs from inside MikTeX through dvips.exe.

The following procedure got both the TeX filter and the TeX filter debugger working for me:

STEP 1: Install MikTeX, Ghostscript and ImageMagick

As you might know, they are all downloadable, free, from the relevant web sites. To avoid some of the steps below you may want to install these into file paths that have no spaces.

STEP 2: Enable the TeX Notation filter

Go to Moodle Administration > Modules > Manager filters and click the closed eye next to TeX Notation.

STEP 3. Input the LaTeX renderer settings

Go to Moodle Administration > Modules > TeX Notation and add the latex, dvips and convert paths. If you used the default installation paths these will be:

        C:\Program Files\MiKTeX 2.7\miktex\bin\latex.exe
        C:\Program Files\MiKTeX 2.7\miktex\bin\dvips.exe
        C:\Program Files\ImageMagick-6.4.9-Q16\convert.exe

Press Save changes. If the paths exist, the borders of the little boxes next to them will turn green. Otherwise, they will turn red. Don't put double quotes around the path names as seemingly suggested by the default paths.

You might also like to add \usepackage{amssymb} to the LaTeX preamble to enlarge your symbol set.

STEP 2: Turn on the Moodle debugger

Go to Administration > Server > Debugging and set 'Debug messages' to DEVELOPER. This allows command execution output to display in the TeX filter debugger.

STEP 4: Open the TeX filter debugger

The URL is http://../filter/tex/texdebug.php. If Test 3, 4 or 5 in the debugger produce 'Unable to fork' errors you'll need STEP 5.

STEP 5: Grant permission for the Internet Guest User to use the command function

On your Windows server, go to C:\WINDOWS\system32 and right-click cmd.exe. Choose Properties > Security. Click Add, then Advanced and Find Now. Select the IUSR name and click OK. Grant it Read & Execute permission and click OK.

If you go back to the TeX filter debugger and do Tests 3, 4 or 5 you should find that the 'Unable to fork' errors have gone away. In fact, mimetex (Test 3) should now be working - or at least showing positive signs of life.

STEP 6: Grant permission for the Internet Guest User to use the WINDOWS Temp folder

Go to C:\WINDOWS and right-click Temp. Choose Properties > Security. Add the IUSR user, as before, and grant it permission to Read and Write. This allows ImageMagick to dump temporary files in there while it's doing its stuff.

STEP 7: Edit the texdebug.php file

Open filter\tex\texdebug.php with your favourite editor, e.g. Dreamweaver or Notepad++, and make the following changes:

(i) Add a -q option to the dvips command to make it run quietly, i.e.,
    change line 239 in the TexOutput function

         from: $cmd = "$CFG->filter_tex_pathdvips -E $dvi -o $ps";
         to:    $cmd = "$CFG->filter_tex_pathdvips -q -E $dvi";

    The output file directive (-o $ps) is redundant so you can remove it if you want.

(ii) Remove the header in the outputTex function, i.e.
     comment out line 105:

          header("Content-type: text/html");

     to stop debugger Test 6 (Check slasharguments setting) producing a warning

If you have no spaces in your latex/dvips/convert file paths all the tests in the TeX filter debugger should now work (though you may find there are certain TeX symbols you can't display, e.g. Bigint). Otherwise:

(iii) Put double quotes around the latex/dvips/convert paths to cope with any spaces, i.e., in the TexOutput function, change line 235

       from:  $cmd = "$CFG->filter_tex_pathlatex --interaction=nonstopmode $tex";
       to:    $cmd = "\"$CFG->filter_tex_pathlatex\" --interaction=nonstopmode $tex";

     change line 239

       from:  $cmd = "$CFG->filter_tex_pathdvips -q -E $dvi";
       to:    $cmd = "\"$CFG->filter_tex_pathdvips\" -q -E $dvi";

     change line 243

       from:  $cmd = "$CFG->filter_tex_pathconvert -density 240 -trim $ps $gif ";
       to:    $cmd = "\"$CFG->filter_tex_pathconvert\" -density 240 -trim $ps $gif ";

Whether or not you have spaces in your file paths, the TeX filter debugger should now work. If the expression you've entered is okay, it should render beautifully in TeX when you run Test 4 (Show graphic image with Tex/Ghostscript). And if you run Test 5 (Show command execution output..) latex, dvips and convert should all show RETURN CODE: 0.    

STEP 8: Edit the latex.php file

This time open filter\tex\latex.php. The changes needed are similar to those in STEP 7.

(i) Add a -q option to the dvips command to make it run quietly, i.e.,
     change line 121 in the render function

         from: $command = "{$CFG->filter_tex_pathdvips} -E $dvi -o $ps";
         to:    $command = "{$CFG->filter_tex_pathdvips} -q -E $dvi";

     The output file directive (-o $ps) is redundant so you can remove it if you want.

(ii) Line 116 states that latex always returns False on Windows. This is not true, so you can uncomment the next 'return false;' line if you like. What is true is that it will sometimes return false if it doesn't totally like the look of the TeX, but still have a go at rendering it anyway (so you might want to allow this and exclude the 'return false' after all).

If you have no spaces in your latex/dvips/convert file paths the TeX filter should now work. Otherwise:
           
      (iii) Replace the curly brackets in the latex/dvips/convert paths with double quotes, i.e., in the render function, change line 114

  from:$command = "{$CFG->filter_tex_pathlatex} --interaction=nonstopmode $tex";
  to:   $command = "\"$CFG->filter_tex_pathlatex\" --interaction=nonstopmode $tex";

     change line 121

       from:  $command = "{$CFG->filter_tex_pathdvips} -q -E $dvi";
       to:    $command = "\"$CFG->filter_tex_pathdvips\" -q -E $dvi";

     change line 132

       from:  $command = "{$CFG->filter_tex_pathconvert} -density $density -trim $bg_opt $ps $gif";
       to:    $command = "\"$CFG->filter_tex_pathconvert\" -density $density -trim $bg_opt $ps $gif";

Whether or not you have spaces in your latex/dvips/convert file paths the TeX filter should now work. Try it out somewhere, e.g. in a Forum.

Further comments:

1. Moodle puts the temporary files associated with latex in the moodledata\temp\latex folder and those associated with mimetex in the moodledata\filter\tex folder, so you can watch them coming and going there if you want.

2. When I enter TeX into a forum reply or post I can't get it to display after posting without refreshing the page. If anyone has solved this problem I'd be happy to hear from them.

3. I'm not sure yet how I feel about loosening the Windows permissions as above. If anyone would like to comment on the risks involved I'd be glad to hear about that too.

I'm hoping most of the TeX filter pain now lies behind me, but having read some of the other posts on this forum I wouldn't bet on it!

Alyson
  

Average of ratings: -
In reply to Alyson Stibbard

Re: TeX Filtering in Windows 2003

by Mauno Korpelainen -

Well it sounds far more difficult to set up MikTeX on Windows than setting up TeXLive on Linux - on my Debian Etch I just had to install the full package with one command and give moodle the correct paths for latex, dvips and convert to get moodle to render TeX with TexLive 2007 wink

But we all know that Windows does things differently...

This was a good and detailed guide, Alyson. If you are not sure about those permissions or possible risks check http://docs.moodle.org/en/Security_overview and http://docs.moodle.org/en/Security (other settings and permissions) and upgrade to 1.9.4+

In reply to Mauno Korpelainen

Re: TeX Filtering in Windows 2003

by Alyson Stibbard -

Thanks Mauno, I'll check out those security documents.

Yes, Windows inhabits its own dimension...

In reply to Alyson Stibbard

Re: TeX Filtering in Windows 2003

by Duke Miller -

These instructions are a great help!

However, I'm still getting an error after step 7 ... I know it may look like a file write/permissions issue but I've given the IUSER account approprate access to moodledata/temp/latex/ folder. Any ideas on how to fix?

Checking executables

latex executable (C:\Program Files\MiKTeX 2.7\miktex\bin\latex.exe) is readable
dvips executable (C:\Program Files\MiKTeX 2.7\miktex\bin\dvips.exe) is readable
convert executable (C:\Program Files\ImageMagick-6.5.4-Q16\convert.exe) is readable

base filename for expression is '02c84304fc915c8941e3a5e070dd6fa1'

Processing TeX expression:

f(x)=\Bigint_{-\infty}^x~e^{-t^2}dt
$ "C:\Program Files\MiKTeX 2.7\miktex\bin\latex.exe" --interaction=nonstopmode C:\Inetpub\wwwroot\moodledata/temp/latex/02c84304fc915c8941e3a5e070dd6fa1.tex OUTPUT: This is pdfTeX, Version 3.1415926-1.40.9 (MiKTeX 2.7) entering extended mode ! I can't write on file `02c84304fc915c8941e3a5e070dd6fa1.log'. Please type another transcript file name ! Emergency stop No pages of output. RETURN CODE: 1 
$ "C:\Program Files\MiKTeX 2.7\miktex\bin\dvips.exe" -q -E C:\Inetpub\wwwroot\moodledata/temp/latex/02c84304fc915c8941e3a5e070dd6fa1.dvi OUTPUT: RETURN CODE: 1 
$ "C:\Program Files\ImageMagick-6.5.4-Q16\convert.exe" -density 240 -trim C:\Inetpub\wwwroot\moodledata/temp/latex/02c84304fc915c8941e3a5e070dd6fa1.ps C:\Inetpub\wwwroot\moodledata/temp/latex/02c84304fc915c8941e3a5e070dd6fa1.gif OUTPUT: RETURN CODE: 1