Summary: Code generation can be speeded uo a bit, using macros and proper coding scheme.
Surprisingly, we are not always aware of things available to us. MS Word, for example, has the capability to reproduce a chunk of text with a stroke of key combination. This appears a great facility, particularly for speeding up writing any mark up codes, including MathML. This facility, though available all the time, is hardly ever used in normal text editing session with MS Word.
Using the facility to create macros in MS Word is simple. Matter of fact, this is one of the reasons for recommending an advanced text editing software like MS Word as against bare editors like Notepad for writing plane text MathML code lines.
We need to create macros for writing a set of code lines for the basic coding forms for most of the frequently used element and assign the same to a keyboard combination like ALT a, ALT b etc. Subsequently, when we need to write the code lines for MathML element in the basic form, we shall require to simply use the key combination and the code lines will be reproduced. It is warned that we must provide sufficient space between existing code or text lines so that the reproduced text by macros do not mix up. This techniques, involving macros, has the advantage that once a correct format is recorded, it is less likely that there would be any syntax error.
In this section, the exact steps to create a macro with few code lines involving “mfrac” element are given. Before we proceed, we must realize that choosing a particular size and font to write the codes in macros is extremely important. The renderer accepts MathML in mono font for the code lines. What it means that we should encode our markups in one font only. A mix of many fonts results from “copy” and “paste” operations from different sources. We must avoid font mixing so that the renderer does not report error, while displaying the content.
Now, follow the steps to create a macro for the basic form of “mfrac” element :
Step 1: Open MS Word. Create a new document. Save the document as “test.doc”, ensuring that “save as type” is a word document. Note that we can not recordand store macros in plane format, which is used for encoding MathML.
Step 2: Select “Courier New” from the font drop down as shown here :
| Font selection |
|---|
Select font size 12 from the adjacent drop down.
Step 3 : Write code lines for the basic form of the “mfrac” element as shown here :
<m:mfrac>
<m:mi> x </m:mi>
<m:mi> y </m:mi>
</m:mfrac>
Step 4 : Select “Tools” from the top tool bar of MS Word and choose “Record New Macro” as shown below :
| Recording macros |
|---|
Step 5 : “Record macro” dialog box appears. In the “Macro name” give the name of macro, say “mfrac”. In the “Store macro in” select the name of document “test document” or the name of the document in which you are recording macro.
| Setting macros |
|---|
Step 6 : Press “keyboard” button. The “Customize Keyboard” dialog box appears. Put the cursor in the “Press new shortcut key” box (if not placed automatically). Press “Alt and f” to record the desired key combination to run the macro for “mfrac” code lines. Note that “Alt + F” text with capital “F” is automatically placed inside the box. In the “unassigned” box, select “test”.
| Assigning key combination |
|---|
Step 7 : Press “Assign” button and then “Close” button. Macro recording has started. A pair of buttons are displayed on the margin of the document as shown in the figure below. One of the button is “Stop” button, while the other is “Pause” button for macro recording.
| Recording code lines |
|---|
Now, looking at the code lines written in step 3, type exactly the same after one or two lines. Do not bother for key and cursor movements. It is advisable that we maintain the required indenting say between “mfrac” tag and “mi” tag. When the code lines have been reproduced, press “Stop” button to stop the recording.
Step 8 : You are now ready to use the macro. Hold “Alt” key and simulataneously press “F” key. The code lines as recorded are reproduced instantly as shown here :
<m:mfrac>
<m:mi> x </m:mi>
<m:mi> y </m:mi>
</m:mfrac>
Macro recording is a powerful tool to reproduce a set of code lines. However, this facility is available in the normal document mode and not in the plane text mode. As such, codes are required to be first written in a document file, say “test.doc”. Once coding is complete in a major way, we need to save the file in plane text with Unicode character format (UTF -8) for renderer to display the content. It is implied here that a repository of macros corresponding to the basic forms of element encoding be prepared in advance in order to use the same, when required.
In the following section, we shall illustrate the use of macros and few improvisations to encode “Cauchy-Schwarz” inequality.
Understanding of the underlying presentation framework of MathML is just one aspect of learning process. Equally important aspect is to develop the ability to convert presentation requirement in terms of codes. There is no universal set of algorithms that would help to successfully translate visual presentation requirement into MathML code lines in a mechanical manner - unless aided by specialized program or software. Unaided, this may be achieved by developing ability to visualize the presentation requirements in some logical modular work pieces like :
“Cauchy-Schwarz” inequality is shown here :
A visual inspection of the formula suggests that this inequality is built of three large blocks of similar composition. The first block is raised to apower of “2”. This can be implemented with “msup” element. Thus, we start with using macro for “msup” element :
<m:msup>
<m:mi> x </m:mi>
<m:mi> y </m:mi>
</m:msup>
The superscript is changed to “2” as required. A code line for element “mo” is written using macro for “mo” element and its content is changed to parenthesis :
<m:math display="block">
<m:mo> ( </m:mo>
<m:msup>
<m:mi> x </m:mi>
<m:mn> 2 </m:mn>
</m:msup>
</m:math>
The base of the superscript is the large block, involving summation sign. The base is built upon summation sign with “over” and “under” limits implemented by “munderover” element. We write “munderover” basic code lines, using macro to replace the “base” of superscript i.e. “x” in the basic format reproduced by macro in the above step.
<m:math display="block">
<m:mo> ( </m:mo>
<m:msup>
<m:munderover>
<m:mo> ∫ </m:mo>
<m:mi>x</m:mi>
<m:mi>y</m:mi>
</m:munderover>
<m:mn> 2 </m:mn>
</m:msup>
</m:math>
We replace integer symbol with summation symbol “∑” and change the limits as required. Note that we need to bunch the underscript with “mrow” tags to work as one of the argument of the “msup” element.
<m:math display="block">
<m:mo> ( </m:mo>
<m:msup>
<m:munderover>
<m:mo>∑</m:mo>
<m:mrow>
<m:mi> k </m:mi>
<m:mo> = </m:mo>
<m:mn> 1</m:mn>
</m:mrow>
<m:mn>n</m:mn>
</m:munderover>
<m:mn> 2 </m:mn>
</m:msup>
</m:math>
Now, we add codes for
<m:math display="block">
<m:mo> ( </m:mo>
<m:msup>
<m:mrow>
<m:munderover>
<m:mo>∑</m:mo>
<m:mrow>
<m:mi> k </m:mi>
<m:mo> = </m:mo>
<m:mn> 1</m:mn>
</m:mrow>
<m:mn>n</m:mn>
</m:munderover>
<m:msub>
<m:mi> a </m:mi>
<m:mi> k </m:mi>
</m:msub>
<m:msub>
<m:mi> b </m:mi>
<m:mi> k </m:mi>
</m:msub>
<m:mo> ) </m:mo>
</m:mrow>
<m:mn> 2 </m:mn>
</m:msup>
</m:math>
This completes the coding for the first left hand side block of the “Cauchy-Schwarz” inequality. At this stage, the display in the browser looks like :
Now coding for two other large block is easy. We need to replicate the above code block twice and do the modification as required. Finally three blocks are joined together with the help of an “inequality” operator "≤". The display, now, looks like :
We observe that the parenthesis of the first term has not grown to the expected height to cover the highest term. Moreover, the parentheses are unequal in size. To achieve the equality in size and proper coverage, we enclose all expressions involving parenthses within a pair of “mrow” tags and a “mstyle” pairs of tages to display “true”. The complete code and display is illustrated in the example below :
<m:math display="block">
<m:mstyle displaystyle='true'>
<m:mrow>
<m:msup>
<m:mrow>
<m:mo> ( </m:mo>
<m:munderover>
<m:mo>∑</m:mo>
<m:mrow>
<m:mi> k </m:mi>
<m:mo> = </m:mo>
<m:mn> 1</m:mn>
</m:mrow>
<m:mn>n</m:mn>
</m:munderover>
<m:msub>
<m:mi> a </m:mi>
<m:mi> k </m:mi>
</m:msub>
<m:msub>
<m:mi> b </m:mi>
<m:mi> k </m:mi>
</m:msub>
<m:mo> ) </m:mo>
</m:mrow>
<m:mn> 2 </m:mn>
</m:msup>
<m:mo> ≤ </m:mo>
<m:mrow>
<m:mo> ( </m:mo>
<m:munderover>
<m:mo>∑</m:mo>
<m:mrow>
<m:mi> k </m:mi>
<m:mo> = </m:mo>
<m:mn> 1</m:mn>
</m:mrow>
<m:mn>n</m:mn>
</m:munderover>
<m:msup>
<m:mrow>
<m:msub>
<m:mi> a </m:mi>
<m:mi> k </m:mi>
</m:msub>
</m:mrow>
<m:mn> 2 </m:mn>
</m:msup>
<m:mo> ) </m:mo>
</m:mrow>
<m:mrow>
<m:mo> ( </m:mo>
<m:munderover>
<m:mo>∑</m:mo>
<m:mrow>
<m:mi> k </m:mi>
<m:mo> = </m:mo>
<m:mn> 1</m:mn>
</m:mrow>
<m:mn>n</m:mn>
</m:munderover>
<m:msup>
<m:mrow>
<m:msub>
<m:mi> b </m:mi>
<m:mi> k </m:mi>
</m:msub>
</m:mrow>
<m:mn> 2 </m:mn>
</m:msup>
<m:mo> ) </m:mo>
</m:mrow>
</m:mrow>
</m:mstyle>
</m:math>
Save the file after editing as “test.xml”. The display looks like :