Difference between revisions of "MathML code examples"

From ConTeXt wiki
m
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
< [[Math]] | [[MathML]] | [[XML]] >
 
< [[Math]] | [[MathML]] | [[XML]] >
 +
 +
__TOC__
 +
 +
== MKII ==
 +
 +
Here is a complete and simple example of a ConTeXt file including MathML for MKII:
 +
 +
<xmlcode>
 +
\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
 +
</xmlcode>
 +
 +
That gives:
 +
 +
<context>
 +
\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
 +
</context>
  
 
MathML support in ConTeXt is very extensive, but some features are rather hidden for now.  
 
MathML support in ConTeXt is very extensive, but some features are rather hidden for now.  
Line 5: Line 51:
 
<cmd>setupMMLappearance</cmd> allows you to adjust various things. For example, the layout of presentational markup which uses mtables for alignment can be changed via <cmd>setupMMLappearance[mtable][alternative=a|b|c]</cmd>. Experiment with the different alternatives to see the effects.
 
<cmd>setupMMLappearance</cmd> allows you to adjust various things. For example, the layout of presentational markup which uses mtables for alignment can be changed via <cmd>setupMMLappearance[mtable][alternative=a|b|c]</cmd>. Experiment with the different alternatives to see the effects.
  
When embedding XML inside normal ConTeXt code, remember that <cmd>stopXMLdata</cmd> gobbles up any following white-space. You'll need to explicitly put it back in if you want it (with <cmd>space</cmd>.
+
When embedding XML inside normal ConTeXt code, remember that <cmd>stopXMLdata</cmd> gobbles up any following white-space. You'll need to explicitly put it back in if you want it (with <cmd>space</cmd>).
  
== Current bugs and workarounds ==
+
=== Current bugs and workarounds ===
  
 
To use UTF inside MathML currently requires this workaround:
 
To use UTF inside MathML currently requires this workaround:
Line 92: Line 138:
  
 
The * (comment token) prevents the spurious space.
 
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:
 +
<xmlcode>
 +
\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
 +
</xmlcode>
  
 
[[Category:Math]]
 
[[Category:Math]]

Latest revision as of 17:58, 17 November 2009

< 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