Importing MathML into Mathematica

(Excerpt from "The MathML Handbook" by Pavi Sandhu)

There are two ways to import MathML equations into Mathematica. The first way is to copy and paste MathML equations from another application directly into a notebook. When you paste a valid MathML expression into a notebook, Mathematica brings up a dialog that asks if you want to paste the literal markup or interpret it (see the following figure). If you choose to interpret the markup, it is automatically converted into Mathematica syntax.

Dialog when pasting MathML into Mathematica notebook

Figure: This dialog appears when you paste MathML markup into a Mathematica notebook.

Another way to import MathML expressions into a notebook is by evaluating the built-in Mathematica function, Import. This function has the following syntax:

Import[file]

Import[file, format]

The first argument specifies the name of the file from which the data should be imported. Mathematica can use the file extension to identify the type of data in the file and to treat it accordingly. For example, any file with a .mml extension is automatically recognized as a MathML file and is converted into a Mathematica box expression. You can use the optional second argument to specify the export format explicitly. For importing MathML, the relevant import format is "MathML".

The following command creates a file called test.mml that contains the MathML markup corresponding to the expression, x^2:

In[1]:= Export["test.mml", x^2, "MathML"]
Out[1]= test.mml

The file test.mml contains the following MathML expression:

<math xmlns='http://www.w3.org/1998/Math/MathML'>
  <semantics>
    <msup>
      <mi>x</mi>
      <mn>2</mn>
    </msup> 
    <annotation-xml encoding='MathML-Content'>
      <apply>
        <power/>
        <ci>x</ci>
        <cn type='integer'>2</cn>
      </apply>
    </annotation-xml>
  </semantics>
</math>

However, when the file is imported back into Mathematica, the MathML is automatically converted into a symbolic expression consisting of boxes, as shown here:

In[2]:= Import["test.mml"]
Out[2]= FormBox[TagBox[TagBox[SuperscriptBox["x", "2"], "MathMLPresentationTag", AutoDelete -> True], "AnnotationsTagWrapper"[TagBox[SuperscriptBox["x", "2"], "MathMLContentTag", AutoDelete -> True]], AutoDelete -> True], TraditionalForm]

The output of the last example is the Mathematica equivalent of the MathML expression. It contains information about both the appearance and meaning of the expression that was imported. You can convert this box expression into a mathematical expression using the function ToExpression, as shown here:

In[3]:= ToExpression[%]
Out[3]= x2

   

<< back next >>

 

 

 

Tipp der data2type-Redaktion:
Zum Thema MathML bieten wir auch folgende Schulungen zur Vertiefung und professionellen Fortbildung an:

 

Copyright © CHARLES RIVER MEDIA, INC., Massachusetts (USA) 2003
Printing of the online version is permitted exclusively for private use. Otherwise this chapter from the book "The MathML Handbook" is subject to the same provisions as those applicable for the hardcover edition: The work including all its components is protected by copyright. All rights reserved, including reproduction, translation, microfilming as well as storage and processing in electronic systems.


CHARLES RIVER MEDIA, INC., 20 Downer Avenue, Suite 3, Hingham, Massachusetts 02043, United States of America