Tricks of the Trade In and Out Trott's Corner New Products New Publications Calendar News Bulletins New Resources Classifieds Download This Issue Editorial Policy Staff Submissions Subscriptions Advertising Back Issues Contact Information 
XML and Mathematica
Working with MathML Introduction MathML is an XMLbased markup language for representing mathematics. It was developed by the W3C to provide an effective way to display math in web pages and to facilitate the transfer and reuse of mathematical content between applications. MathML can encode information about both the meaning and the appearance of mathematical notation. For example, a MathML equation can be copied out of a web page and directly pasted into an application like Mathematica for evaluation. Wolfram Research was a key participant in the development of MathML and is committed to supporting this important web technology. Mathematica includes full support for MathML 2.0, the latest version of the language. You can import MathML equations into a Mathematica notebook and evaluate them, or export equations from a notebook as MathML and paste them into an HTML document for viewing in a web browser. There are also several kernel commands for converting between MathML and the boxes and expressions used internally by Mathematica to represent mathematics. Syntax of MathML Overview Since it is an XML application, the syntax rules of MathML are defined by the XML specification. Each MathML expression consists of a series of elements, written in the angle bracket syntax similar to HTML. Each element can take several attributes. The allowed elements and attributes are determined by the MathML DTD. All MathML elements fall into one of three categories: interface elements, presentation elements, and content elements. Interface elements, such as the toplevel math element, determine how a MathML expression is embedded in other XML documents. Presentation elements encode information about the appearance of a mathematical expression, that is, its twodimensional structure. For example, the mrow, mfrac, msqrt, and msub elements represent a row, a fraction, a square root, and a subscripted expression, respectively. Content elements encode information about the logical meaning of a mathematical expression. For example, plus and sin represent addition and the trigonometric sine function, and apply represents the operation of applying a function. A given equation can be represented in several different ways in MathML.
Using Mathematica, you can generate presentation markup, content markup, or combined markup for any equation. Presentation MathML Presentation MathML consists of about 30 elements and 50 attributes, which encode the visual twodimensional structure of a mathematical expression. For example, the Mathematica typeset expression would have the following MathML representation.
The entire expression is enclosed in a math element. This must be the root element for every instance of MathML markup. The above example also uses four other presentation elements.
Identifiers, operators, and numbers are each represented by a different type of element because each type of object has slightly different typesetting conventions for fonts, spacing, and so on. For example, variables are typically rendered in an italic font, numbers are displayed in a normal font, and operators are rendered with extra space around them, depending on whether they occur in a prefix, postfix, or infix position. In addition to the mi, mn, and mo elements, there are presentation elements corresponding to common notational structures, such as fractions, square roots, subscripts, superscripts, and matrices. Any given formula can then be represented by decomposing it into its constituent parts and replacing each notational construct by the corresponding presentation elements. For example, the typeset expression would have the following MathML representation.
Here, the mfrac, msqrt, and msup elements represent a fraction, a square root, and a superscripted expression. Each of these elements takes a fixed number of child elements, which have a specific meaning based on their position. These child elements are called arguments. For example, both the mfrac and msup elements take two arguments with the following syntax.
As before, the mrow element is used to enclose other elements that need to be displayed in a horizontal row. For example, the typeset expression would have the following MathML representation.
Here, the limits of the integral are shown using the presentation element msubsup, which takes three arguments, with the following syntax.
Another notable feature is that the symbols representing the integral sign, the exponential, and the differential d are represented using the character entities ∫, &exp;, and ⅆ, respectively. These are among approximately two thousand special symbols defined by the MathML DTD. These can be included in a document using a named entity reference or a character entity reference that uses the Unicode character code for that symbol. The mstyle element is used for applying styles to an equation. Any attributes specified in an mstyle element are inherited by all its child elements. Hence, you can use this element to specify properties like the font size and color for an equation, as in the example below. Note the use of the entity $InvisibleTimes; to denote multiplication.
The above examples are intended only to illustrate how presentation markup works through a sampling of some of its elements. To see a complete listing of all the presentation elements and attributes, see the MathML specification at www.w3.org/TR/MathML2. Content MathML Content MathML consists of about 140 elements and 12 attributes, which encode the logical meaning of a mathematical expression. The content elements ci and cn are used to represent identifiers and numbers, respectively. They are analogous to the mi and mn elements in presentation markup. For example, the typeset expression would have the following content MathML representation.
The apply element is used to apply operators or functions to expressions. The first argument of the apply element is usually an empty element indicating an operator or function. The remaining arguments represent one or more expressions to which the first argument is applied. In the above example, the first argument of the apply function is the empty element plus, which denotes addition. The type attribute of cn describes the type of number encoded. It can take values real, integer, rational, complexpolar, complexcartesian, and constant. The empty element sep is used to separate different parts of a number, such as the numerator and denominator of a fraction or the real and imaginary parts of a complex number. For example:
The majority of content elements are empty elements representing specific operators or functions. The various elements are organized into groups named after the following specific elementary subfields of mathematics:
There are elements corresponding to most operators and functions that are encountered in high school mathematics. For example, basic arithmetic operators are represented by plus, minus, times, divide, and power.
Integrals are specified using the int element. The variable of integration is represented using the element bvar. The upper and lower limits of integration are usually specified using the elements lowlimit and uplimit.
The interval element is used to specify closed and open intervals. It takes the attribute closure, which can take the values closed, open, closedopen, and openclosed corresponding to the four types of intervals possible. The default value for closure is closed.
You can also use the interval element to specify the limits of a definite integral as an alternative to using uplimit and lowlimit.
The matrix and matrixrow elements are used to represent a matrix and a row of a matrix, respectively. The eq element is used to express equality.
The above examples are intended only to illustrate how content markup works through a representative sampling of some of its elements. To see a complete listing of all the content elements and attributes, see the MathML specification at www.w3.org/TR/MathML2. Importing MathML There are two ways to import MathML equations into Mathematica.
By default, MathML markup is imported as a Mathematica box expression. You can convert the boxes into an expression using the ToExpression command.
MathML Conversion Options The standard ConversionOptions feature of the Import function can be used for greater control over the export process. The syntax for specifying a conversion option is as follows.
For more information on the conversion options available for importing MathML, see XML Import Conversion Options. Generating MathML Mathematica includes several functions for generating MathML from the boxes and expressions used internally by Mathematica to represent equations. You can enter an equation in a notebook using palettes, menus, or keyboard shortcuts and then convert it into MathML using one of these conversion functions. All the MathML conversion functions are located in the XML`MathML` context. You can use BoxesToMathML to generate MathML from a box structure. By default, this generates presentation markup only.
Alternatively, you can use ExpressionToMathML to convert a typeset equation in a notebook into MathML. By default, this generates combined markup with both the presentation markup and content markup for the equation enclosed in a semantics element.
The annotationxml element is used to provide additional information of a type specified by its encoding attribute. Here, the encoding attribute has the value "MathMLContent", indicating that the annotationxml element contains content MathML. You can use the option "Formats" to generate either presentation MathML or content MathML only. You can also set the option "Annotations">{} to suppress the header information.
Note that ExpressionToMathML evaluates its first argument before converting it to MathML. Hence, if you supply an expression that can be simplified on evaluation, you may get unexpected results. For example, suppose you want to generate the presentation markup for the following definite integral.
Because the integral evaluates to give the result 1, the following command generates the MathML representation of 1 instead of the integral.
To get the MathML representation of the integral, you must force the integral to remain unevaluated by wrapping the Unevaluated function around it.
Setting Options These are all the functions that generate MathML as output.
These functions all accept the following options.
Using these options, you can control various features of the generated MathML, such as including an XML declaration or DTD declaration, generating presentation markup, content markup, or both, and using an explicit namespace declaration and namespace prefix. You can specify the options explicitly each time you evaluate one of the MathML functions. Alternatively, you can use the SetOptions command to change the default values of the options for a particular function. The option values you set are then used for all subsequent evaluations of that function. For example, evaluating the following command ensures that the output for all subsequent evaluations of ExpressionToMathML will, by default, generate presentation MathML only and omit the header information in the output.
Exporting MathML Introduction You can use Mathematica’s sophisticated typesetting capabilities to create properly formatted equations and then convert them into MathML for display on the web. There are several ways to export mathematical expressions from a Mathematica notebook as MathML.
The resulting output contains both presentation markup and content markup for the expression, enclosed in a semantics element. You can choose to generate either presentation markup or content markup by changing the value of the option "Formats". Note that the xmlns attribute is added to the toplevel math element to provide information about the namespace of the enclosed elements. Symbols for MathML Elements Certain content elements in MathML do not have a direct analog in Mathematica. Therefore, a few symbols are specially defined in the XML`MathML` context to represent these elements. These symbols are listed below.
MathML Conversion Options The standard ConversionOptions feature of the Export function can be used for greater control over the export process. The syntax for specifying a conversion option is as follows.
The following conversion options are specially useful when exporting MathML.
Annotations This option controls which annotations are added to the output MathML. The value of this option is a list whose elements can be "DocumentHeader", "XMLDeclaration", or "DOCTYPEDeclaration". The order of the elements in the list is not relevant. XMLDeclaration When "XMLDeclaration" is one of the annotations, then an XML declaration is included in the header. This means that the statement <?xml version="1.0"?> appears in the header.
DOCTYPEDeclaration When "DOCTYPEDeclaration" is one of the annotations, then an XML document type declaration of the form <!DOCTYPE … > appears in the header. This is a statement that specifies the DTD for the XML application in which the output is written.
DocumentHeader With the setting "Annotations">{"DocumentHeader", "XMLDeclaration", "DOCTYPEDeclaration"}, a header containing an XML declaration and a document type declaration for the MathML DTD is automatically added to the output.
When "Annotations" does not contain "DocumentHeader", then the output MathML has no header. This is true even if the "Annotations" contains other elements, such as "XMLDeclaration" or "DOCTYPEDeclaration". Thus, "DocumentHeader" is an overall switch that controls whether the structure has a header or not.
Formats This option controls which type of MathML markup is generated. The value of the option is a list that can have as its elements "PresentationMathML", or "ContentMathML", or both. The default setting is "Formats">{"PresentationMathML", "ContentMathML"}, which generates both presentation and content MathML in parallel. The following command exports presentation MathML only.
This command exports content MathML only.
NamespacePrefixes This conversion option lets you generate XML markup with a specific namespace declaration and namespace prefixes. The option is specified in the form
where url and prefix are strings specifying the URL of the namespace and the namespace prefix. In the following example, the "NamespacePrefix" option is used to generate presentation markup with each MathML element having a namespace prefix "mml" associated with the MathML namespace.
UseUnicodePlane1Characters This conversion option controls whether characters belonging to plane 1 of Unicode should be replaced with similar characters in plane 0. This option is useful because currently most browsers cannot properly display plane 1 characters.
The option "UseUnicodePlane1Characters" and its possible values. With the default setting, "UseUnicodePlane1Characters">True, special characters that belong to plane 1 of Unicode (such as Gothic, scripted, and doublestruck characters), are written out with their plane 1 numeric character codes. Here is an example involving the Gothic “g” character.
With the setting, "UseUnicodePlane1Characters">False, any special character that belongs to plane 1 of Unicode, is replaced by a corresponding plane 0 character with a suitable value of the mathvariant attribute specified.


About Mathematica Download Mathematica Player Copyright © Wolfram Media, Inc. All rights reserved. 