{"id":17,"date":"2006-08-20T09:51:00","date_gmt":"2006-08-20T14:51:00","guid":{"rendered":"http:\/\/2d823b65bb.nxcli.io\/2006\/08\/a-demo-mathematica-mathml-and-odf.html"},"modified":"2011-03-14T13:54:50","modified_gmt":"2011-03-14T17:54:50","slug":"demo-mathematica-mathml-and-odf","status":"publish","type":"post","link":"https:\/\/www.robweir.com\/blog\/2006\/08\/demo-mathematica-mathml-and-odf.html","title":{"rendered":"A Demo: Mathematica, MathML and ODF"},"content":{"rendered":"<p>Here&#8217;s a short tutorial on exchanging MathML between Mathematica and OpenOffice, showing what is possible today, and offering some suggestions for closer integration.<\/p>\n<p>First, start with a new ODF document in OpenOffice. It is often easier to modify an existing document, inheriting its structure and default styles, than to create a new document from scratch. So I believe that a lot of interesting projects with ODF will start with an existing document as a template, and then add or replace content in it.<\/p>\n<p>So, here&#8217;s what I made, a simple file with a formula describing the Euclidean metric, our old friend the Pythagorean Theorom. Click the image to load the ODF file.<\/p>\n<p><a href=\"https:\/\/2d823b65bb.nxcli.io\/blog\/attachments\/MathML\/euclidean.odt\"><img decoding=\"async\" src=\"https:\/\/2d823b65bb.nxcli.io\/blog\/attachments\/MathML\/form1.jpg\" border=\"0\" alt=\"\" \/><\/a><\/p>\n<p>If you rename the ODF file to a .zip extension, and unzip it, you can see the XML files it contains. Always start with the manifest.xml , for your convenience <a href=\"https:\/\/2d823b65bb.nxcli.io\/blog\/attachments\/MathML\/manifest.xml\">here<\/a>, to which I draw your attention to the entry with the type &#8220;application\/vnd.oasis.opendocument.formula&#8221;. This, according to Appendix C of the ODF 1.0 specification, is the registered MIME type of an ODF formula document. So that sounds like what we want. Let&#8217;s replace that equation with something else.<\/p>\n<p>So into Mathematica we go. Suppose I want to calculate the indefinite double integral of the Euclidean metric. Why not? This is something I&#8217;d rather not do by hand, but I know Mathematica can quickly give me the answer:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/2d823b65bb.nxcli.io\/blog\/attachments\/MathML\/math1.jpg\" border=\"0\" alt=\"\" \/><\/p>\n<p>Now I really don&#8217;t want to retype that result into OpenOffice. So, what can I do? I can use Mathematica&#8217;s ExpressionToMathML function to turn the above into MathML. When I do that I get MathML like <a href=\"https:\/\/2d823b65bb.nxcli.io\/blog\/attachments\/MathML\/ExpressionToMathML.xml\">this<\/a>.<\/p>\n<p>Let&#8217;s see now what happens if I simply drop that content in as a replacement for the original content.xml in the ODF file. Here&#8217;s what I get (click the image to open the ODF file):<\/p>\n<p><a href=\"https:\/\/2d823b65bb.nxcli.io\/blog\/attachments\/MathML\/euclidean2.odt\"><img decoding=\"async\" src=\"https:\/\/2d823b65bb.nxcli.io\/blog\/attachments\/MathML\/form2.jpg\" border=\"0\" alt=\"\" \/><\/a><\/p>\n<p>So we got something, but it is not quite right. I&#8217;m seeing some little hollow boxes, usually an indication of an unprintable character. What&#8217;s up with this?<\/p>\n<p>A closer look at the XML generated from Mathematica shows that these boxes are being displayed whenever the MathML uses the XML character entities corresponding to <a href=\"http:\/\/www.w3.org\/TR\/MathML2\/chapter6.html#chars.nonmark\">section 6.2.4<\/a> &#8220;Non-Marking Characters&#8221; of the MathML specification. This includes things like &#8220;InvisibleTimes&#8221; which handles cases where adjacency represents multiplication (xy == x*y). Using these characters provides hints to the application that can help it optimize its rendering and editing, but they should not be displayed.<\/p>\n<p>In any case there appears to be a bug in OpenOffice 2.0.3 where it tries to display these characters and finds they don&#8217;t map to any printable Unicode character. No big deal, I will enter a bug report on that later. But for now I can easily clean this up by defining a new function in Mathematica, ExpressionToOO, defined as follows:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/2d823b65bb.nxcli.io\/blog\/attachments\/MathML\/math2.jpg\" border=\"0\" alt=\"\" \/><\/p>\n<p>(Note I didn&#8217;t name this &#8220;ExpressionToODF&#8221;, since strictly speaking the ODF specification allows MathML 2.0, including the non-marking characters. This function is specifically to work around an OpenOffice bug. It outputs valid MathML, simply removing the non-marking characters which OO doesn&#8217;t understand.)<\/p>\n<p>So, back to Mathematica, I run ExpressionToOO, grab that XML and inject that XML into the ODF document, and we get the following (click to open the ODF file):<\/p>\n<p><a href=\"https:\/\/2d823b65bb.nxcli.io\/blog\/attachments\/MathML\/euclidean3.odt\"><img decoding=\"async\" src=\"https:\/\/2d823b65bb.nxcli.io\/blog\/attachments\/MathML\/form3.jpg\" border=\"0\" alt=\"\" \/><\/a><\/p>\n<p>That&#8217;s what we want! For those who are interested, the complete Mathematica notebook is here: <a href=\"https:\/\/2d823b65bb.nxcli.io\/blog\/attachments\/MathML\/Session.nb\">Session.nb<\/a>.<\/p>\n<p>As you can see, this isn&#8217;t rocket science, though no doubt it may be useful to rocket scientists. Consider this a little &#8220;proof of concept&#8221;. Real end users will not be going around unzipping ODF documents and copying XML around. There needs to be some additional integration work to make this process simple and joyful. For example:<\/p>\n<ol>\n<li>A Mathematica function that automatically inserts a formula into an ODF document<\/li>\n<li>A OpenOffice add-in that lets the user automatically browser formulas from Mathematica and insert them into the current working document.<\/li>\n<li>Clipboard level exchange of MathML between OpenOffice and Mathematica<\/li>\n<li>An export filter from OpenOffice to export to the XHTML+MathML+SVG <a href=\"http:\/\/www.w3.org\/TR\/XHTMLplusMathMLplusSVG\/\">profile<\/a> defined to the W3C. This, combined with Firefox, would provide kickass scientific publishing using open standards and tools.<\/li>\n<\/ol>\n<p>Note that I&#8217;m using here Mathematica just as an example. There are <a href=\"http:\/\/www.w3.org\/Math\/Software\/mathml_software_by_name.html\">over 100 MathML<\/a> supporting applications out there, both commercial and open source.   I&#8217;d be interested in hearing what other ideas people have for workflows involving ODF editors and other tools that work with the standards ODF includes, not just MathML, but SVG, XForms, etc.  Let&#8217;s demonstrate the value of open standards working together.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a short tutorial on exchanging MathML between Mathematica and OpenOffice, showing what is possible today, and offering some suggestions for closer integration. First, start with a new ODF document in OpenOffice. It is often easier to modify an existing document, inheriting its structure and default styles, than to create a new document from scratch. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[9],"tags":[],"class_list":{"0":"post-17","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-odf","7":"entry"},"_links":{"self":[{"href":"https:\/\/www.robweir.com\/blog\/wp-json\/wp\/v2\/posts\/17","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robweir.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robweir.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robweir.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robweir.com\/blog\/wp-json\/wp\/v2\/comments?post=17"}],"version-history":[{"count":5,"href":"https:\/\/www.robweir.com\/blog\/wp-json\/wp\/v2\/posts\/17\/revisions"}],"predecessor-version":[{"id":1643,"href":"https:\/\/www.robweir.com\/blog\/wp-json\/wp\/v2\/posts\/17\/revisions\/1643"}],"wp:attachment":[{"href":"https:\/\/www.robweir.com\/blog\/wp-json\/wp\/v2\/media?parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robweir.com\/blog\/wp-json\/wp\/v2\/categories?post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robweir.com\/blog\/wp-json\/wp\/v2\/tags?post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}