tags:

views:

451

answers:

6

The project: I want to build a LaTeX-to-MathML translator in PHP. Why? Because I'm a mathematician, and I want to publish math on my Drupal site. It doesn't have to translate all of LaTeX, since the basic document-level stuff is ably handled by the CMS and wouldn't be written in LaTeX to begin with; it just has to translate math written in LaTeX into math written in MathML. Although I feel as though I've done my due diligence, this doesn't seem to exist already. Maybe I'm wrong---if you know of something that would serve this purpose, by all means let me know, and thank you in advance. But assuming it doesn't exist, I guess I have to go write it myself.

Here's the thing, though: I've never done anything this ambitious. I don't really know where to begin. I've used PHP for years, but just to do the standard "build a CMS with PHP and MySQL"-type of stuff. I've never attempted anything as seemingly sophisticated as translation from one language to another.

I'm just dumb enough to consider doing it with regex---after all, LaTeX is a much more formal language, and it doesn't allow for nearly the kinds of pathological edge-cases, as say, HTML. But on the other hand, I'm just smart enough to realize this is probably a terrible idea: now I have two problems, and I sure don't want to end up like this guy.

So if that's not the way to go (right?), what is? How should I start thinking about this problem? Am I essentially writing a LaTeX compiler in PHP, and if so, what do I need to know to do that (like, should I just go read the Purple Dragon book first?)?

I'm both really excited and pretty intimidated by the prospect of this project, but hey, this is how we all learn to be programmers, right? If something we need doesn't exist, we go and build it, necessity is the mother of... you get the point. Tremendous thanks to everyone in advance for any and all guidance you can offer.

A: 

Alright this answer was a mess.

Here's a cleaned up version:

Since regex's clearly won't cut it for a translator for this type of thing, you have two options, based on your goals:

  1. You just want to be able to display LATEX on your site one way or another.
    • If this is what you want, there is a simple solution somewhere out there for you that is easier than picking up an advanced book on compiler theory. Either some way to include latex on your site, an existing translator, or something.

  2. You are a keener, and want to learn about compiler theory.
    • If this is the case, I cannot recommend the PDB highly enough. It's a fascinating book, and you'll learn a lot from it; After the first two chapters, you will have learned enough about lexical analysis to complete this project. Best money I've spent on an educational resource to date!
Cam
+1  A: 

Don't write the parser yourself unless you want to do that as a learning experience. Just call existing LaTeX toolchains from PHP.

LaTeX2HTML is about as good as you're going to get, and here's an (old) description of a LaTeX to MathML convertor from the maintainer of LaTeX2HTML.

p00ya
A: 

If you are okay with converting formulas to pictures, there are tons of solutions. If you want MathML specifically, there are several of those as well. However, you might consider jsMath which uses javascript to render (a subset of) LaTeX in the browser. It's used by Sage and works well there.

Ivan Andrus
I'd prefer to avoid the formulas-as-images route if possible. But jsMath does look promising, at least as a short-term solution, especially as Javascript engines have gotten faster. Thanks!
Alex Basson
A: 

uh:

http://drupal.org/project/drutex

http://drupal.org/project/jsmath

http://www.forkosh.com/mimetex.html

http://www.codecogs.com/

what is wrong with any of these?

Mica
After having looked into these, my issue with most of them (and thanks, btw, for mentioning them, as I was unaware of all but DruTeX), is that they end up converting math formulas into gifs. I find this unappealing, as it means the math isn't semantically marked up, isn't searchable, isn't stylable, and generally looks "shoved in", rather than as part of the natural flow of the text. I can understand the appeal of these solutions, but ultimately I think they fall short of the ideal.
Alex Basson
Actually, the more I look at it, the more jsMath looks promising. It doesn't convert LaTeX into MathML specifically, so long-term it might not be what I'm looking for, but it looks like it might serve my immediate needs. Thanks again!
Alex Basson
Ideally i think you'd want MathML-- however support for it across all browsers is certainly lacking. I can see why you wouldn't want it as a gif either, however, you could try using a caption or section header to make it more "search-able"
Mica
A: 

Wikipedia is using a LaTeX to HTML(or image) translator written in OCaml. You can borrow some code there, or just use it as is.

SK-logic
A: 

I actually had a go at this last year. I got something working, though I wouldn't claim it had any elegance or charm to it, nor was it fully functional.

If you want to convert equations to MathML, rather than full LaTeX conversion, then you could use itex2MML. If you can load extensions into your PHP, it's possible to compile itex2MML with PHP-bindings and use it natively in scripts. The Makefile might need a bit of hacking to get all the configurations right.

Links:

Andrew Stacey