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 webMathematica 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 webMathematica site, and they can use a MathML entry mechanism to enter mathematical notation into their web browser and send this to webMathematica 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.
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). webMathematica 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 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:
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)
Internet Explorer 5.0 with the techexplorer plug-in
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:
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, ⁡.
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.
Certain webMathematica applications generate results that contain mathematical expressions suitable for formatting with MathML. This section shows how to generate MathML with webMathematica and take advantage of the rendering techniques described in the previous section.
The main webMathematica documentation describes how MathML can be generated with MSPFormat using a format style of MathMLForm. The following will format the expression expr into MathML.
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.
Tools for working with MathML typically support both content and presentation.
MathML Integrate Example
We first see the source for the input page, IntegrateForm.jsp.
<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 uses the MathML stylesheet, which here is assumed to be installed in the webMathematica 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 webMathematica 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 webMathematica
Many of the webMathematica 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 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.