MathML code examples

From ConTeXt wiki

< Math | MathML | XML >

MKII

Here is a complete and simple example of a ConTeXt file including MathML for MKII:

\usemodule[mathml]
\starttext
\startXMLdata
<math>
 <mrow>
  <msup> <mi>x</mi><mn>2</mn> </msup>
  <mo>+</mo>
  <mrow>
    <mn>4</mn><mo>InvisibleTimes;</mo><mi>x</mi>
  </mrow>
  <mo>+</mo>
  <mn>4</mn>
 </mrow>
</math>
\stopXMLdata
\stoptext

That gives:

MathML support in ConTeXt is very extensive, but some features are rather hidden for now.

\setupMMLappearance allows you to adjust various things. For example, the layout of presentational markup which uses mtables for alignment can be changed via \setupMMLappearance[mtable][alternative=a|b|c]. Experiment with the different alternatives to see the effects.

When embedding XML inside normal ConTeXt code, remember that \stopXMLdata gobbles up any following white-space. You'll need to explicitly put it back in if you want it (with \space).

Current bugs and workarounds

To use UTF inside MathML currently requires this workaround:

\unprotect

\long\def\doXMLremapdata[#1]#2#3#4%
  {\bgroup
   \startXMLmapping[#1]%
   % enable unknown elements (should be macro)
   \doifsomething{#1}
     {\doifdefinedelse{\@@XML#1:\s!unknown:M}
        {\remapXMLunknowntrue}{\remapXMLunknownfalse}}%
   %
   \pushmacro\doXMLentity % needed ?
   % this will change, proper split in element itself
   \ifx\currentXMLnamespace\empty
     \let\parseXMLelement\remapXMLelement
   \else
     % here we need to get rid of the namespace; we also
     % have to preserve the leaqding / if present
     \@EA\long\@EA\def\@EA\parseXMLelement\@EA
       ##\@EA1\currentXMLnamespace:{\remapXMLelement##1}%
     % ##2 removes leading spaces
   \fi
   %
   \let\parseXMLescape \remapXMLescape
   \let\parseXMLprocess\remapXMLprocess
   %
   \let\doXMLentity    \remapXMLentity
   %
   \enableXML % sets entities
   \enableXMLexpansion
   \let\par\XMLremappedpar
   \the\everyXMLremapping
   %\ignorelines
   \catcode`\^^I=\@@space
   \catcode`\^^M=\@@space
   \catcode`\^^L=\@@space
   \catcode`\^^Z=\@@space
\pushmacro\unicodechar
\let\unicodechar\relax
   \xdef\remappedXMLdata{#4\empty}%
\popmacro\unicodechar
   \let\par\endgraf
   \popmacro\doXMLentity % needed ?
   \disableXMLexpansion
   \catcode`\{=\@@begingroup
   \catcode`\}=\@@endgroup
   \catcode`\\=\@@escape
   \iftraceXMLremapping
     \ifmmode\vbox\fi\bgroup
     \convertcommand\remappedXMLdata\to\ascii
     \tttf\veryraggedright\ascii\par
     \writestatus{xml-remap}{\ascii}%
     \egroup
   \fi
   #2\scantokens\@EA{\remappedXMLdata\empty\empty}#3%
   \stopXMLmapping
   \egroup}

\protect

\def\MMLpTEXT#1#2%
  {\hbox
     {\tf
      \getXMLarguments{mstyle}{#1}%
      \doMMPpbackground{mstyle}
        {\doMMPpcolor{mstyle}
           {\setMMLptextstyle{mstyle}%
            \ignorespaces#2\unskip\unskip}}}}


Additional space appears in front of an inline equation (tagged as an imath element). The fix (from Hans) is made in xtag-ini.tex: locate the line

 .unexpanded.def<B.doXMLelementE

and add a * immediately following it, to produce

.unexpanded.def<B.doXMLelementE*

The * (comment token) prevents the spurious space.

MKIV

Support with MKIV is not as complete as MKII and under construction. The example above for MKIV:

\usemodule[mathml]
\starttext
test
\xmlprocessdata{}{
<math xmlns='http://www.w3.org/1998/Math/MathML'>
 <mrow>
  <msup> <mi>x</mi><mn>2</mn> </msup>
  <mo>+</mo>
  <mrow>
    <mn>4</mn><mi>x</mi>
  </mrow>
  <mo>+</mo>
  <mn>4</mn>
 </mrow>
</math>
}{}
\stoptext