mathml to render as image

mathml to render as image

by Jeff Graham -
Number of replies: 21
I just finished writing an importer from blackboard 6.x for question pools and noticed that our professors here have used the java applet to render mathml within blackboard.

What I would like to do is setup something similar for the imported questions in moodle . I have parsed out the mathml, but would like to render this as an image server side to avoid having to require the users to setup their browsers to properly render mathml.

Can anybody inform me on how to go about doing this? I am not opposed to converting to TeX first (or some other format) if necessary.

If it is even more simple than this and I have just missed some moodle setting please educate me smile

regards,
Jeff
Average of ratings: -
In reply to Jeff Graham

Re: mathml to render as image

by Zbigniew Fiedorowicz -
I wrote a Moodle filter to display MathML within Moodle.  The idea was to convert the MathML to TeX, then use mimetex to convert that into a gif. The filter is in
http://cvs.sourceforge.net/viewcvs.py/moodle/contrib/mathml/
It worked quite well, once I got it working - I used it to render Jacques Distler's theoretical physics blog at http://golem.ph.utexas.edu/~distler/blog/, some of whose pages contain heavy-duty MathML.

The only trouble with it is that the MathML->TeX part uses XSL transformations using the Xalan XSLT processor.  Unfortunately Xalan is Linux only and even on Linux quite a challenge to install, since it has some nonstandard dependencies.  In particular it depends on a library which is part of Intel's C-compiler suite.  It took me quite some time to figure out to identify that library and then to find the place I could download it off Intel's site.  [It is available for free download, with some strings attached, but you have to jump through a few hoops to get the download.]

I haven't played around with it for more than a year, so perhaps the situation with respect to Xalan has changed in the meantime.


In reply to Zbigniew Fiedorowicz

Re: mathml to render as image

by Zbigniew Fiedorowicz -
I've had another look at Xalan, and they now have binary distributions for the major platforms, including Windows.  I've now gotten my MathML filter to work in WindowsXP/Easy-PHP.  So you should be able to get MathML conversion to TeX working without too much difficulty.  Let me know if I can be of further assistance.
In reply to Zbigniew Fiedorowicz

Re: mathml to render as image

by Jeff Graham -
Hi Zbigniew,

Thanks for the response. I've been taking a look at the xerces/xalan options. It looks like java versions are available for both xerces and xalan that can be called from CLI, or an applet. Have you had any success or tried to get this working? If we were able to get this working I imagine they could be included along with the Moodle distribution so that the administrator would only need to turn the mathml filter on. That would be pretty cool

Regardless I'm in the painful process of compiling xerces/xalan. I'm assuming this is the method you have used previously?

The documentation is rather poor and it appears you can't do the usual ./configure && make && make install sad

In reply to Jeff Graham

Re: mathml to render as image

by Zbigniew Fiedorowicz -
Hi Jeff,

I would suggest that your quiz conversion script just convert all the mathml markup in the Blackboard quiz into corresponding LaTeX markup in the converted Moodle quiz.  Then the Moodle quiz could just be displayed using the Moodle TeX filter.  Thus you would just be using the mathml->tex conversion code in the mathml filter rather than the mathml filter itself.

I think this is better than requiring that Moodle installations activate yet another filter. For one thing, it would be more efficient.  The mathml->tex conversion (which is kind of slow) would be done just once, during the conversion process rather than repeatedly every time a student took a quiz.

Also running an extra filter, even if it doesn't do anything to most pages, slows down a Moodle server.  Finally having the mathml filter active would create more support issues both locally and here on moodle.org, since people would try to use it for other things besides imported Blackboard quizzes and run into trouble with it.  The most serious issue is that mathml is incompatible with the HTML editor (at least the last time I checked - Janne, correct me if I'm wrong).  Also when I set up a public demo of the mathml filter on my site, I found that a lot of people were pasting in badly malformed mathml (apparently generated by some kind of Windows software) and then complaining that it would not display in the filter.

Zig

In reply to Zbigniew Fiedorowicz

Re: mathml to render as image

by Jeff Graham -

Zig,

Thanks again for the response. I agree with you about filtering and storing as TeX makes much more sense. I've jumped over to a PC so I can try the binary distribution. I have it up and running, however I am getting the following upon trying to filter a string containing matml.

Xalan error: Fatal Error.Occurred at unknown file , line 1 , column 1. Invalid document structure SAXParseException: Invalid document structure (, line 1, column 1)

The mathml that was sent to xalan appears to be formatted correctlly for each opening tag there is a closing one. Attached is one of the offending mathml strings. Any ideas as to what is going on?

In reply to Jeff Graham

Re: mathml to render as image

by Zbigniew Fiedorowicz -
The problem is the MathML entity ⅆ, which you need to add to the MathML filter, thus:
$mathml = str_replace("ⅆ","ⅆ",$mathml);

In reply to Zbigniew Fiedorowicz

Re: mathml to render as image

by Jeff Graham -
Zbigniew,

Thanks! smile

I suspected that may be the culprit and that I would have to add a special handler as you mentioned. Is this just mapping the mathml to the unicode equivalent?

I will try this out tomorrow.

Jeff
In reply to Jeff Graham

Re: mathml to render as image

by Zbigniew Fiedorowicz -
Yes, the handlers in the filter are replacing various mathml entities by their unicode equivalents.  You might need to add more on an ad hoc basis.
In reply to Zbigniew Fiedorowicz

Re: mathml to render as image

by Jeff Graham -

Zbigniew,

Thanks again for all of your help. I updated the filter file as suggested and I am still recieving the same error. If you don't mind could you look at the attached mathml that it choked on? It was all on one line, but I indented it for readability. The &DifferenialD; was translated fine. Do you think the empty tags maybe causing this problem? ie. the mi tag

Any ideas appreciated.

In reply to Jeff Graham

Re: mathml to render as image

by Zbigniew Fiedorowicz -
Oops, I just remembered that you are running this on Windows.  I had trouble using the "echo" command in Windows to provide input for Xalan.  I had to write my own version.  Here is the C source, and I am attaching the executable.
#include <stdio.h>

int main(int argc, char *argv[]) {
   fputs(argv[1],stdout);
}

In reply to Zbigniew Fiedorowicz

Re: mathml to render as image

by Jeff Graham -

Hi Zbigniew,

Thanks for all your time.

I tried compiling the source and also using your executable. Same error.

I tried calling xalan from the command line to see if I could get anymore information. I get the following extra information "The filename or extension is too long." The cmd session is attached.

any ideas?

In reply to Jeff Graham

Re: mathml to render as image

by Zbigniew Fiedorowicz -
One change I would suggest is to install EasyPHP directly in C:\ rather than C:\Program Files (to avoid spaces in the path). I am attaching a bat file, which works fine in my installation. See if you can run it (with appropriate modifications).

In reply to Zbigniew Fiedorowicz

Re: mathml to render as image

by Jeff Graham -
Hi again smile,

You've been a great help. comparing your example and my broken one I found that something is clobbering the escaped quotes in the opening <math> tag they get replaced with what appears to be a space. angry

If I replace those and run it from the command line it works fine, but I can't figure out how to get those to not be clobbered. I commented out the escapeshellargs() call to make sure that it wasn't the culprit. I also looked for any obvious &quot; or similar tags in the replace_str lines. I didn't see anything obivious, and all I tried failed.

Once I get this fixed I think we are there! smile

Your help has been greatly appreciated.
In reply to Jeff Graham

Re: mathml to render as image

by Zbigniew Fiedorowicz -
Here are a couple of possibilities:
  1. Instead of \", pass some other distinctive string to your echo.exe and have echo.exe replace this by ".
  2. Instead of passing the mathml into stdin, write it to a file and pass the file as an argument to xalan.
In reply to Zbigniew Fiedorowicz

Re: mathml to render as image

by Jeff Graham -
Hi Zbigniew,

My mistake it actually was the escapeshellargs() clobbering the quotes. I just commented that line and escaped it manually with a couple replace_str's. It's working now! smile

Thanks for all the help and ideas.
In reply to Zbigniew Fiedorowicz

Re: mathml to render as image

by Zbigniew Fiedorowicz -
Here's a link to a demo of the MathML->TeX convertor via Xalan
https://webwork.math.ohio-state.edu/xalan/index.php
Upload an html file containing MathML.  The output will be an html file with TeX expressions enclosed by double $.  These in turn can be converted to images using the Moodle TeX filter.
In reply to Zbigniew Fiedorowicz

Re: mathml to render as image

by Marc Grober -
I have been trying to use your converter and the results are intriguing. I am producing the MathML via OpenOffice Math. Inserting the results in html and uploading I get two iterations of the file uploaded and no Tex that will work in a moodle wiki. On the other hand, generating some simple MathML from the MathML Central website I do get the Tex... WHat am I missing here?
In reply to Marc Grober

Re: mathml to render as image

by Ramon Eixarch -
Hi Marc,

we have a Formula editor based on MathML that might be usefull for you. It is called WIRIS Editor and you can find a Plugin to integrated into Moodle at http://moodle.org/mod/data/view.php?d=13&rid=927 .

Changes in MathML
For tehcnical reasons the tag symbols '<' and '>' in our mnathml code have been changed for '«' and '»'.

For example, the mathml code for 2+3 that is
<math><mrow><mn>2</mn><mo>+</mo><mn>3</mn></mrow></math>

but be stored as
«math»«mrow»«mn»2«/mn»«mo»+«/mo»«mn»3«/mn»«/mrow»«/math»

Our editor has not been designed to import MathML created from tools, it may not work for all your formulas.