Can I convert Latex to MathML in Mathjax?

681 views
Skip to first unread message

jerry...@gmail.com

unread,
Jan 17, 2013, 3:44:00 PM1/17/13
to mathja...@googlegroups.com
Hi guys,
   My project is displaying mathematics formulas using Latex. Lately, we have a request that we need to get the MathML from those Latex formula. I know when I right click latex formulas, it shows 'show mathml' option. I was wondering if I can actually covert the latex to MathML is the run time. Thanks for the help!  

Thomas Leathrum

unread,
Jan 18, 2013, 11:35:31 PM1/18/13
to mathja...@googlegroups.com
Yes, as a matter of fact, you can make the conversion to MathML on the fly suitable for display on the page. The key methods for this are toMathML() and toMathMLquote() -- they are a bit hard to access, and they behave somewhat differently in spite of having similar names, but here is a code snippet to show you how they work:

math=MathJax.Hub.getAllJax("mathid")[0];
str1=math.root.toMathML("");
str2=math.root.toMathMLquote(str1);

Here "mathid" is a CSS ID of an HTML element (like a <span>) that contains the LaTeX, and "[0]" indicates to get the first LaTeX expression inside that element. Then the toMathML("") method (yes, with an empty string for input) returns the MathML equivalent expression, as a string, and the toMathMLquote(str) method takes a MathML string and applies ampersand-escaping, so that "<" becomes "&lt;" and so forth -- for HTML display inside a <pre> environment, this is the version you need. All of this has to happen *after* the math has been processed, so you have to use the MathJax queue, as described in the documentation page "Modifying Math on the Page" (my favorite bit of MathJax documentation), at

http://www.mathjax.org/en/latest/typeset.html

Davide P. Cervone

unread,
Jan 19, 2013, 10:15:59 AM1/19/13
to mathja...@googlegroups.com
It's actually a little more complicated than that, since toMathML is technically an asynchronous routine (it can cause files to load, and that causes a restart, so you need to trap errors and check for that). See my example at

https://groups.google.com/d/msg/mathjax-users/unL8IjcrTto/DjHpH4BbPRcJ

for more information about how to do that.

Davide
Reply all
Reply to author
Forward
0 new messages