5.2 MathML

MathML is designed to allow mathematical, scientific, and other technical information to be served, received, and processed on the World Wide Web. It is an official recommendation of the World Wide Web Consortium (W3C) working group on mathematics. Users of web*Mathematica* can benefit from MathML in a number of ways. They can use MathML for documents that contain a mixture of mathematics and text, they can generate MathML dynamically on their web*Mathematica* site, and they can use a MathML entry mechanism to enter mathematical notation into their web browser and send this to web*Mathematica* for computation.

Wolfram Research has long been involved in the development of MathML, both as a founding member of the mathematics working group of the W3C and as the host of the first two official MathML conferences in 2000 (http://www.mathmlconference.org/2000) and 2002 (http://www.mathmlconference.org/2002). *Mathematica* contains many features for working with MathML and there is a strong relationship between the *Mathematica* typesetting system and MathML.

One resource for learning more about MathML is the Wolfram Research sponsored web site, http://www.mathmlcentral.com. A section describing the evolution of MathML and some of the issues involved in developing a mathematical language suitable for a computation system such as *Mathematica *is found at http://www.mathmlcentral.com/history.html.

If you are not interested in the specific details of how MathML works and just want to use MathML in your output, then you should go to the sections Generating MathML and Sending MathML.

5.2.1 Embedding MathML in Web Documents

This section discusses how documents can be written that mix both mathematics and text. These documents are written in XML format and use both MathML and XHTML (the XML compliant form of HTML). web*Mathematica* contains functions that do all of this automatically, so you do not need to read this unless you wish to learn more about the details of how browsers support MathML.

XHTML

XHTML is an XML compliant form of HTML, available as an official W3C recommendation, http://www.w3.org/MarkUp. It is very similar to HTML, except that for a document to be valid it must follow the rules of XML. (Some of these were described in the previous section.) To use documents that mix mathematics and text, XHTML is required. Use of XHTML is needed anyway, since the W3C intends that HTML will not be developed further.

The sample XHTML document illustrated below is very similar to HTML, except for the initial XML declaration and the DTD reference. The latter can be used by an XML parser to validate that the input document is indeed valid XHTML. This demonstrates one of the benefits of XML technology. That is, a parser can validate a document, checking details such as the different tags being in the correct places and holding the correct number of arguments, without specializing in the particular flavor of XML. The reference to the DTD is not required; however, it is necessary if the document is to be validated.

<?xml version="1.0"?>

<!DOCTYPE html PUBLIC

"-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Basic XHTML Document</title>

</head>

<body>

<h1>XHTML</h1>

<p>This is a basic XHTML document.</p>

</body>

</html>

This document could be read by modern web browsers and would display in the expected fashion.

XHTML and MathML

To add mathematics and other technical notation to a text document, it is possible to write one document that contains both XHTML and MathML. A sample document follows.

<?xml version="1.0"?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN"

"http://www.w3.org/TR/MathML2/dtd/xhtml-math11-f.dtd" [

<!ENTITY mathml "http://www.w3.org/1998/Math/MathML">

]>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Basic XHTML+MathML Document</title>

</head>

<body>

<h1>XHTML+MathML</h1>

<p>Here is a math expression.</p>

<math xmlns='http://www.w3.org/1998/Math/MathML'>

<msup>

<mi>x</mi>

<mn>2</mn>

</msup></math>

</body>

</html>

This could be read into a browser that provides native support for MathML and would be read as expected. Note the reference to a DTD that allows the embedding of MathML into XHTML to form an XHTML+MathML document.

Unfortunately, not all browsers support MathML natively. While Mozilla, Amaya, and the most recent versions of Netscape do give native support for MathML, Internet Explorer does not.

For MathML to work with Internet Explorer or older versions of Netscape, a plug-in mechanism must be used. The way to do this is explored in the next section.

Rendering XHTML and MathML Documents

The previous section showed how to embed MathML into XHTML, creating documents that mix text and mathematics. It also explained that this does not work with browsers that rely on a plug-in mechanism. This section shows how to write documents that will work in a wide range of browsers.

To support MathML in browsers using a plug-in mechanism, the document must use special tags that are relevant to the particular plug-in used. If the browser supports MathML natively, then no special tags are needed. Of course, an author does not want to produce different versions of each document specific to each rendering technology. The solution is to make use of XSLT stylesheet technology to convert the document in the browser before it is viewed. This automatically inserts any special tags that are needed for plug-ins. An XSLT stylesheet that implements this solution is available from the W3C Math site, http://www.w3.org/Math/XSL.

Here is a document that uses the MathML stylesheet.

<?xml version="1.0"?>

<?xml-stylesheet type="text/xsl" href="http://www.w3.org/Math/XSL/mathml.xsl"?>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Basic XHTML+MathML Document</title>

</head>

<body>

<h1>XHTML+MathML</h1>

<p>Here is a math expression.</p>

<math xmlns='http://www.w3.org/1998/Math/MathML'>

<msup>

<mi>x</mi>

<mn>2</mn>

</msup>

</math>

</body>

</html>

This document can be rendered by any browser that is supported by the stylesheet. At the current time this includes the following:

Windows:

Internet Explorer 6.0 with either the MathPlayer or techexplorer plug-in

Internet Explorer 5.5 with either the MathPlayer or techexplorer plug-in

Internet Explorer 5.0 with the techexplorer plug-in

Netscape 7.0 PR1

Netscape 6.1 with the techexplorer plug-in

Amaya (Presentation MathML only)

Mozilla 1.0 (and later)

Macintosh:

Internet Explorer 5.0 with the techexplorer plug-in

Mozilla 1.1 (and later)

Linux/Unix:

Netscape 7.0 PR1

Netscape 6.1 with the techexplorer plug-in

Mozilla 1.0 (and later)

Amaya (Presentation MathML only)

See http://www.w3.org/Math/XSL for updates. Information on specific rendering technologies is also given in a later section.

By using an absolute reference to the stylesheet, documents that use the stylesheet found on the W3C site can be moved from one server to another or saved locally and continue to work. One issue with an absolute stylesheet reference is that Internet Explorer may, according to its configuration, give a warning or even reject the stylesheet altogether (leading to a failure to render the MathML). This can be solved with a relative reference to the stylesheet and by placing a copy of the stylesheet on the same server as the document. For example, the document can start as follows:

<?xml version="1.0"?>

<?xml-stylesheet type="text/xsl" href="/webMathematica/Resources/XSL/mathml.xsl"?>

This means that the stylesheet will be found at the URL /webMathematica/Resources/XSL/mathml.xsl relative to the root of the server from which the document is being retrieved. If a server chooses to do this, it will work well with Internet Explorer, but it will be necessary to ensure that the server has an up-to-date version of the stylesheet. It will also mean that documents will not be quite so portable when moved from one server to another.

Note that the XHTML+MathML document shown above that uses the MathML stylesheet does not contain a DOCTYPE declaration. This is, of course, a limitation because the document cannot now be validated. Another consequence is the XML system that renders it will not be aware of any special entity names. The DTD is missing because Internet Explorer does not accept all the entities in the MathML DTD. The solution is to use MathML which refers to numerical rather than named entities.

Here is an example that uses a named entity reference, ⁡.

<math xmlns='http://www.w3.org/1998/Math/MathML'>

<mrow>

<mi>sin</mi>

<mo>⁡</mo>

<mrow>

<mo>(</mo>

<mi>x</mi>

<mo>)</mo>

</mrow>

</mrow>

</math>

This example uses the numerical value ⁡. It is the preferred form.

<math xmlns='http://www.w3.org/1998/Math/MathML'>

<mrow>

<mi>sin</mi>

<mo>⁡</mo>

<mrow>

<mo>(</mo>

<mi>x</mi>

<mo>)</mo>

</mrow>

</mrow>

</math>

If you want to find the numerical value for any character you can use the *Mathematica* function ToCharacterCode to generate the numerical value, and BaseForm to generate the hexadecimal form. For example, the unicode value of a capital Alpha can be found as follows.

BaseForm[ToCharacterCode["Α"], 16]

{"391""16"}

Further information on the appropriate numerical values can be found at the MathML site, http://www.w3.org/TR/MathML2/chapter6.html. Further information can also be found at the unicode site, http://www.unicode.org.

5.2.2 Generating MathML from web*Mathematica*

Certain web*Mathematica* applications generate results that contain mathematical expressions suitable for formatting with MathML. This section shows how to generate MathML with web*Mathematica* and take advantage of the rendering techniques described in the previous section.

The main web*Mathematica* documentation describes how MathML can be generated with MSPFormat using a format style of MathMLForm. The following will format the expression expr into MathML.

<msp:evaluate>

MSPFormat[ expr, MathMLForm]

</msp:evaluate>

MathML comes in two different varieties: presentation MathML specifies the appearance of the MathML whereas content MathML attempts to specify what the MathML means. Since MathML contains no general extension mechanism, the amount of information that can be encoded with content MathML is limited. However, if presentation MathML is generated from *Mathematica*, it will always work when sent back to *Mathematica*.

It is also possible to use other formatting styles such as StandardForm or TraditionalForm, in which case the format type RawMathML should be selected, as shown here.

The following shows how to generate presentation MathML.

<msp:evaluate>

MSPFormat[ expr, TraditionalForm, PresentationMathML]

</msp:evaluate>

The following generates content MathML.

<msp:evaluate>

MSPFormat[ expr, TraditionalForm, ContentMathML]

</msp:evaluate>

Tools for working with MathML typically support both content and presentation.

MathML Integrate Example

This example JSP uses the MathML stylesheet. The page is actually a combination of two JSPs, IntegrateForm.jsp and IntegrateXSLT.jsp, that use JavaScript. They are closely modeled on the standard web*Mathematica* examples PlotScript.jsp and PlotScript1.jsp. The source for these MathML examples is available in webMathematica/Examples/MathML. If you installed web*Mathematica* as described above, you should be able to connect to this JSP via http://localhost:8080/webMathematica/Examples/MathML/IntegrateForm.jsp. (You may have some other URL for accessing your server.)

We first see the source for the input page, IntegrateForm.jsp.

<html>

<head>

<script>

function integrate(f)

{

win = window.open( "IntegrateXSLT?fun=" + f.fun.value, "integrate",

"toolbar=none, resizeable=yes, width=450, height=350");

}

</script>

<title>Integrate Tool</title>

</head>

<body bgcolor="#ffffff" >

<h1>Integrate Tool</h1>

<form action="IntegrateXSLT" method="post">

<p>

Enter a function:

<input type="text" name="fun" size="24" value =

"<msp:evaluate> MSPValue[ $$fun, "Sin[x]^2"] </msp:evaluate>"

>

</p>

<br/>

<input type="button" value="Integrate" onClick="integrate(this.form)" />

</form>

</body>

</html>

This is standard HTML. When the input button is clicked, a JavaScript function is called that extracts the input from the input field and calls the JSP IntegrateXSLT.jsp, which then opens in a new window. The contents of IntegrateXSLT.jsp are shown below.

<?xml version="1.0"?>

<?xml-stylesheet type="text/xsl" href="/webMathematica/Resources/XSL/mathml.xsl"?>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>Integrate Result</title>

</head>

<body>

<msp:evaluateKernel>

<msp:evaluate>

MSPPageOptions[ "ContentType" -> "text/xml"];

res = Null;

If[ MSPValueQ[ $$fun],

fun = MSPToExpression[ $$fun];

int = Integrate[ fun, x];

If[ Head[int ] === Integrate, int = "Unknown"]] ;

</msp:evaluate>

<p>

Integration of a function, formatting into MathML.

</p>

<table border="2" rules="all">

<thead>

<tr>

<th>Function</th><th>Integral</th>

</tr>

</thead>

<tr>

<td align="center"><msp:evaluate> MSPFormat[ fun, MathMLForm]<msp:evaluate></td>

<td align="center"><msp:evaluate> MSPFormat[ int, MathMLForm]<msp:evaluate></td>

</tr>

</table>

</msp:evaluateKernel>

</body>

</html>

This uses the MathML stylesheet, which here is assumed to be installed in the web*Mathematica* web application in the directory XSL. The output content type is set to text/xml, and the necessary computation in *Mathematica* is carried out.

When this example works, it might be interesting to use the View Source menu of your browser. It should be noted how the MathML flows naturally with the XHTML. Also note how the document does not state the physical size of each mathematical expression. This is very useful because the size will only be known accurately when the document is rendered in the browser.

The actual example code delivered with web*Mathematica* is a little more complicated since it contains alternatives for rendering directly with MathPlayer and for generating XHTML+MathML. However, the one shown above that uses the MathML stylesheet is the most general solution. The others are included in the example for demonstration purposes.

5.2.3 Sending MathML to web*Mathematica*

Many of the web*Mathematica* examples provide XHTML forms for users to enter input. This imposes certain limitations; for example, the input must use a one-dimensional syntax and cannot really use special characters. Further, it is often desirable to use special palettes to enter templates to be filled in. Although these are features provided by the *Mathematica* front end, they are not available in a web browser.

Although it is not possible to provide an alternative input mechanism in a pure browser such as Internet Explorer, more powerful input features are available with a plug-in. One suitable plug-in is the Input Control, included in the WebEQ suite of tools. WebEQ is a suite of tools for building web pages that involve dynamic math; it is described further in the Appendix: MathML Technology.

MathML Input Example

This shows an example JSP that uses WebEQ for input. It is actually a combination of two JSPs, IntegrateWebEQ.jsp and IntegrateXSLT.jsp, that use JavaScript. The second JSP was also used in the previous example and renders MathML output. The two JSPs are closely modelled on the standard web*Mathematica* examples PlotScript.jsp and PlotScript1.jsp. If you installed web*Mathematica* as described above, you should be able to connect to this JSP via http://localhost:8080/webMathematica/Examples/MathML/IntegrateWebEQ.jsp. (You may have some other URL for accessing your server.) Note that this assumes your browser has an installation of WebEQ. If you do not have WebEQ it will not work.

The text of IntegrateWebEQ.jsp is shown below.

<html>

<head>

<script>

function integrate(f)

{

win = window.open( "IntegrateOutput?fun=" + f.fun.value, "integrate",

"toolbar=none, resizeable=yes, width=450, height=350");

}

</script>

<title>Integrate Tool</title>

</head>

<body bgcolor="#ffffff" >

<h1>Integrate Tool</h1>

<p>

Enter a function:

<msp:allocateKernel>

<applet code="webeq3.editor.InputControl" height=150 width=400

name="editor">

<param name="toolbar" value=", <tb><btn>SUP</btn><btn>FRAC</btn><btn>SQRT</btn>

<btn>SIN</btn><btn>COS</btn><btn>TAN</btn></tb>">

<param name=eq value=

"<msp:evaluate> If[MSPValueQ[$$mml], $$mml, "<math><mrow></mrow></math>"] </msp:evaluate>">

</applet>

</msp:allocateKernel>

</p>

<br/>

<form action="IntegrateXSLT" method="post">

<input type="button" value="Integrate" onClick="integrate()" />

</form>

</body>

</html>

This is similar to IntegrateForm.jsp, except that WebEQ is used for entering input instead of an XHTML input element.

One of the key issues is that IntegrateXSLT.jsp works both with MathML input from WebEQ and text input from a form. This works because the input processing function MSPToExpression accepts both MathML and text input. MSPToExpression also provides a security check on its argument, so it prevents dangerous unfiltered commands from being processed by *Mathematica.*

Any client machine that uses this example will need an installation of WebEQ. Information on how to obtain and install WebEQ is found at http://www.dessci.com/webmath/webeq/features.stm. In addition, the example could easily be modified so that a WebEQ archive was delivered from the server.