# Creating macros

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

Another important use of XSLT in working with MathML is for creating macros to abbreviate complex notation. For example, suppose you are authoring a document in which a complicated expression is repeated frequently. Instead of writing out the full expression each time, you can use an XSLT stylesheet to define a new element to represent the whole expression. This allows you to write out the entire document in a much more compact form. Once the document is finished, you can replace each occurrence of the macro with the corresponding MathML expression by using the XSLT stylesheet that contains the definition for the macro.

This technique is particularly useful when you are defining new elements to represent concepts not covered in the existing MathML specification. For example, content MathML does not include any element that correspond to the concept of the trace of matrix. Hence, suppose we wanted to encode a statement like the one below:

You could define a new element called x:trace to represent this concept. You can specify the notation and meaning of this element in terms of standard MathML elements by using a semantics element. In other words, you can define x:trace to be equivalent to the following expression:

```
<semantics>
<mi>Tr</mi>
<annotation-xml encoding="OpenMath">
<function name="Trace" cd="linalg3" xmlns="http://www.openmath.org/OpenMath"/>
</annotation-xml>
</semantics>
```

If you then wanted to encode a statement like , you could use the following markup:

```
<apply>
<eq/>
<apply>
<x:trace/>
<apply><plus/><ci>A</ci><ci>B</ci></apply>
</apply>
<apply>
<plus/>
<apply><x:trace/><ci>A</ci></apply>
<apply><x:trace/><ci>B</ci></apply>
</apply>
</apply>
```

This is much more compact than having to use the full semantics element in place of each occurrence of x:trace. Of course, the x:trace element exists only in a private namespace and would not be recognized by other MathML processing applications. Hence, before processing the document further, you can replace all macros with their full form by applying an XSLT stylesheet that contains a template of the form shown below:

```
<xsl:template match="x:trace">
<semantics>
<mi>Tr</mi>
<annotation-xml encoding="OpenMath">
<function name="Trace" cd="linalg3" xmlns="http://www.openmath.org/OpenMath"/>
</annotation-xml>
</semantics>
</xsl:template>
```

Of course, in practice, most MathML markup will not be authored by hand but rather generated by special authoring software. Hence, the strategy described here for defining, inserting, and replacing macros is best implemented by a particular software application. The point here is that XSLT transformations provide a convenient mechanism for carrying out this kind of replacement procedure, even if the actual details are concealed under a more high-level interface.

<< back | next >> |

**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