{"id":9456,"date":"2014-09-19T15:24:10","date_gmt":"2014-09-19T07:24:10","guid":{"rendered":"http:\/\/www.intmath.com\/blog\/?p=9456"},"modified":"2015-05-01T16:17:16","modified_gmt":"2015-05-01T08:17:16","slug":"katex-with-asciimathml-input-and-mathjax-fallback","status":"publish","type":"post","link":"https:\/\/www.intmath.com\/blog\/mathematics\/katex-with-asciimathml-input-and-mathjax-fallback-9456","title":{"rendered":"KaTeX with ASCIIMathML input and MathJax fallback"},"content":{"rendered":"<p><!--NoIntMathMathPlugin--> <\/p>\n<p>I <a href=\"https:\/\/www.intmath.com\/blog\/mathematics\/katex-a-new-way-to-display-math-on-the-web-9445\">recently wrote<\/a> about the new way to deliver math on the Web, called <a href=\"https:\/\/github.com\/Khan\/KaTeX\">KaTeX<\/a> (a direct competitor to <a href=\"http:\/\/www.mathjax.org\/\">MathJax<\/a>, which I've been using on <a href=\"https:\/\/www.intmath.com\/\">IntMath<\/a> for some time now).<\/p>\n<h2>KaTeX only accepts LaTeX input <\/h2>\n<p>While KaTeX is a lot faster than MathJax, for me there are 2 downsides with KaTeX:<\/p>\n<ol>\n<li>KaTeX only accepts LaTeX input. However, I have used <a href=\"http:\/\/www1.chapman.edu\/~jipsen\/mathml\/asciimath.html\">ASCIIMathML<\/a> for all the equations and formulas on IntMath, because it is signiicantly simpler and easier to use than LaTeX. If I wanted to use KaTeX for my site, I'm stuck. <\/li>\n<li>KaTeX doesn't recognize many math expressions yet, for example matrices, determinants, various symbols, &quot;aligned&quot; environments (where you can align everything on the equal sign), various accents, case definitions and so on. <\/li>\n<\/ol>\n<p>So I set about writing a page that handles both of these issues. <\/p>\n<h2>ASCIIMathML input, fallback to MathJax <\/h2>\n<p>I needed a system that recognised ASCIIMathML input, converted it to LaTeX, and then was processed by KaTeX so it looked like &quot;real&quot; math. In the cases where KaTeX cannot handle the math, it needs to fall back to MathJax so all the math is rendered properly. <\/p>\n<p>Here is a demo of my solution: <\/p>\n<blockquote>\n<p><a href=\"https:\/\/www.intmath.com\/cg5\/asciimathml-katex-mathjax.php\">ASCIIMathML, KaTeX and MathJax Demo<\/a><\/p>\n<\/blockquote>\n<p>You'll see some equations appear quickly (those done by KaTeX), then the MathJax-rendered ones will appear. They have a green mark next to them so you can see what MathJax can do that KaTeX cannot. <\/p>\n<p>This is deliberately a \"heavy\" page (with many equations) to push its performance.<\/p>\n<h2>How is it done?<\/h2>\n<p>Fortunately, ASCIIMathML comes with a script, ASCIIMathTeXImg.js, which converts the simple ASCIIMathML input to LaTeX, and then outputs each math expression as an image (using MimeTex or MathTex). <\/p>\n<p>I modified the function AMTparseMath() in ASCIIMathTeXImg.js, so it no longer converts math to images, but outputs LaTeX. <\/p>\n<p>My tweaks were:<\/p>\n<ol>\n<li>Create a span in the place of each piece of ASCIIMathML<\/li>\n<li>Give the span a unique id<\/li>\n<li>Try first to render it with KaTeX<\/li>\n<li>If that fails, put back the original ASCIIMathML and render it with MathJax<\/li>\n<\/ol>\n<p>Here's the relevant parts of the changed script.<\/p>\n<pre>\r\nvar node = document.createElement(\"span\");\r\nthisId = \"mathId\"+counter;\r\nnode.id = thisId;\r\ntry {\r\n\tkatex.render(texstring,node);\r\n} catch(err) { \r\n\tnode.className = \"mj\";\r\n\tnode.innerHTML = \"`\"+str+\"`\";\r\n\tMathJax.Hub.Queue([\"Typeset\",MathJax.Hub,thisId]);\r\n}\r\ncounter++;\r\n<\/pre>\n<h2>Downsides to the approach <\/h2>\n<p>Of course, the main aim is speed, and if the user needs to download both KaTeX and MathJax, as well as ASCIIMathTeXImg.js, there is going to be a significant delay before any rendering occurs, especially on a mobile device. <\/p>\n<p>On my phone, the first KaTeX equations on the <a href=\"https:\/\/www.intmath.com\/cg5\/asciimathml-katex-mathjax.php\">demo page<\/a> appeared in about 6 seconds, and the MathJax matrix came in around the 13 second mark. <\/p>\n<p>But it is a feasible solution for mobile devices, especially if your more complicated equations are low down the page (so they will process in the background and be done before the user gets to them). <\/p>\n<p>Another downside is that the user will need to download 2 sets of fonts - one for MathJax and one for KaTeX. One way out of that would be to specify one or the other.<\/p>\n<h2>Related pages <\/h2>\n<p>You may also be interested in this demo, where you can see how much faster KaTeX does its thing compared to MathJax: <\/p>\n<blockquote>\n<p><a href=\"https:\/\/www.intmath.com\/cg5\/katex-mathjax-comparison.php\">KaTeX and MathJax comparison demo<\/a>  <\/p>\n<\/blockquote>\n<p>This page gives background on KaTeX:<\/p>\n<blockquote>\n<p><a href=\"https:\/\/www.intmath.com\/blog\/mathematics\/katex-a-new-way-to-display-math-on-the-web-9445\">KaTeX &ndash; a new way to display math on the Web<\/a><\/p>\n<\/blockquote>\n<p>This page is a sandbox where you can play with ASCIIMathML input:<\/p>\n<blockquote>\n<p><a href=\"https:\/\/www.intmath.com\/help\/asciimathml-input-mathjax-output.php\">ASCIIMathML input, MathJax output<\/a> <\/p>\n<\/blockquote>\n<p>This page uses the script mentioned above, ASCIIMathTeXImg.js, to send math in emails:<\/p>\n<blockquote>\n<p><a href=\"https:\/\/www.intmath.com\/help\/send-math-email.php\">Send an email containing math<\/a><\/p>\n<\/blockquote>\n<p>And finally, this page gives examples of how to enter math using ASCIIMathML:<\/p>\n<blockquote>\n<p><a href=\"https:\/\/www.intmath.com\/help\/send-math-email-syntax.php\">Syntax for entering math using ASCIIMathML<\/a><\/p>\n<\/blockquote>\n<h2>Credits<\/h2>\n<p>Peter Jipsen and David Lippman for ASCIIMathML.<\/p>\n<p>The MathJax team (especially Davide and Peter) for MathJax.   <\/p>\n<p>The Khan Academy team for KaTeX. <\/p>\n<p class=\"alt\"><a href=\"#respond\" id=\"comms\">Be the first to comment<\/a> below.<\/p>","protected":false},"excerpt":{"rendered":"<p>  <a href=\"https:\/\/www.intmath.com\/blog\/mathematics\/katex-with-asciimathml-input-and-mathjax-fallback-9456\"><img loading=\"lazy\" src=\"https:\/\/www.intmath.com\/blog\/wp-content\/images\/2014\/09\/asciimathml-katex-mathjax.png\" alt=\"KaTEX ASCIIMathML and MathJax\" width=\"128\" height=\"100\" class=\"imgRt\" \/><\/a><br \/>\nKaTeX can handle ASCIIMathML input, and fall back to MathJax when KaTeX gives up with this approach.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mo_disable_npp":""},"categories":[4],"tags":[127],"_links":{"self":[{"href":"https:\/\/www.intmath.com\/blog\/wp-json\/wp\/v2\/posts\/9456"}],"collection":[{"href":"https:\/\/www.intmath.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.intmath.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.intmath.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.intmath.com\/blog\/wp-json\/wp\/v2\/comments?post=9456"}],"version-history":[{"count":0,"href":"https:\/\/www.intmath.com\/blog\/wp-json\/wp\/v2\/posts\/9456\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.intmath.com\/blog\/wp-json\/wp\/v2\/media?parent=9456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.intmath.com\/blog\/wp-json\/wp\/v2\/categories?post=9456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.intmath.com\/blog\/wp-json\/wp\/v2\/tags?post=9456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}