Belleve:
While Peter is working on a node.js version of the script you want, I
decided to try a phantom.js version, and was able to get it to work
out pretty easily. I'm not sure if that fits your bill or not, but
I've attached it in case it helps. The syntax is
phantom.js jax.js [--display] 'TeX code' > file.svg
where 'TeX code' is the math to be processed, and file.svg is a stand-
alone SVG file that will contain the typeset math.
There are a couple of issues that you need to be aware of if you try
to do something similar in node.js. These are taken care of in the
onAlert function in this script, so look there for details. The first
issue is that MathJax uses a hidden svg element at the top of the page
to store the character paths, and then refers to these within the
individual equations (in order to not have to duplicate paths). That
means you have to combine the two pieces in order to form a single svg
file. You also need to add the xmlns attribute for the svg element
since MathJax uses createElementNS to associate the namespace and
apparently WebKit doesn't attach the namespace in its innerHTML
output. Finally, the references to the character paths use xlink, but
in HTML5, namespaces are removed, so you have to put back the "xlink:"
in front of all the hrefs in the <use> elements in order to get the
final xml to work properly as a stand-alone svg file.
This works pretty well, but there is not much error checking, so if
something goes wrong with the MathJax processing, I'm not sure what
the results will be. The one thing I don't like about it is that
phantom.js has a security restriction that won't allow a local file to
load content from a remote site, so if I open a blank page, I can't
add MathJax to it from the CDN. So I end up loading a page from the
cdn initially, and replace its contents with the math that I want to
convert. A bit of a hack, but it works, and since we already need
access to the CDN anyway, I don't feel too bad about that. I did try
setting the localToRemoteUrlAccessEnabled property, but it didn't seem
to help; perhaps I didn't do it properly, but I didn't spend a lot of
time trying to fix it once I worked out the details.
Of course, you could arrange to use a local copy of MathJax rather
than the CDN copy, and that would probably speed up your processing,
as this would require that you load MathJax for each equation (no
caching is performed). It would probably be better to make a version
that takes a file containing the equations that you want processed,
and produces a series of svg files, one for each equation, so that
MathJax needs to be loaded only once. But this is at least a starting
point.
Hope that helps.
Davide
On May 9, 2012, at 9:58 AM, Belleve Invis wrote: