软件编程版 #51371 层

基于Web的数学显示语言MathML

楼主 · 1 楼
Shea 著

  MathML是Mathematical Markup Language的缩写,意为数学标示语言。从MathML的拼
写上,我们也许就能想到它与HTML的联系。事实上,MathML就是为了在基于Web方式浏览网
页时更方便的交流数学信息而开发出来的。它的开发商是赫赫有名的加拿大滑铁卢Maple公
司,著名的符号计算软件Maple就是这一公司的产品。

  目前,MathML的最高版本是MathML 2.0,而且已经集成到了Maple 7和Maple 8中。它
的最大特点是,在网页上可以不通过图片方式来显示数学公式,用起来十分的方便。当然
,这也是有代价的。由于使用的特殊的标示,一般的网页浏览器无法显示MathML的内容,
必须在现有浏览器的基础上加装一个插件WebEQ,下载、安装可以通过这个网页进行http:
//www.maplesoft.com/standards/MathML/Viewer/index.html。速度还是非常快的,对于
拨号用户大约只需两分钟。

  例如,我们要在网页上显示sin(x)的n次幂对x的积分。事实上,这个积分的结果有一
定的复杂度,一般情况下,除了用图片没有什么更好的解决办法。

Pi
----
2
/ 1/2
| n Pi GAMMA(1/2 + n/2)
| sin(x) dx = 1/2 ----------------------
| GAMMA(1 + n/2)
/
0

这是Maple 8的输出结果,还是比较复杂的。其中牵涉到了圆周率π和Γ函数的显示。

  但是使用MathML,我们就可以先对轻松的解决这个问题。
1.加载MathML工具包。

>with(MathML);
[Export, ExportContent, ExportPresentation, Import, ImportContent]

2.使用Export命令。
>Export(Int(sin(x)^n,x=0..Pi/2)=int(sin(x)^n,x=0..Pi/2));

这样就会输出MathML的可用于网页的源代码,但是不是我们所习惯的源代码的缩进格式,
我们可以在改进一下。

3.改成缩进格式。
> XMLTools[Print](%);
<math xmlns='http://www.w3.org/1998/Math/MathML >
<semantics>
<mrow xref='id32'>
<mrow xref='id11'>
<munderover>
<mo>&Integral;</mo>
<mn xref='id1'>0</mn>
<mrow xref='id4'>
<mfrac xref='id2'>
<mn>1</mn>
<mn>2</mn>
</mfrac>
<mo>&InvisibleTimes;</mo>
<mn xref='id3'>&pi;</mn>
</mrow>
</munderover>
<mrow xref='id10'>
<msup>
<mi>sin</mi>
<mi xref='id9'>n</mi>
</msup>
<mo>&ApplyFunction;</mo>
<mi xref='id7'>x</mi>
</mrow>
<mo>&InvisibleTimes;</mo>
<mrow>
<mo>&DifferentialD;</mo>
<mi xref='id5'>x</mi>
</mrow>
</mrow>
<mo>=</mo>
<mrow xref='id31'>
<mfrac xref='id12'>
<mn>1</mn>
<mn>2</mn>
</mfrac>
<mo>&InvisibleTimes;</mo>
<mrow xref='id30'>
<mfrac>
<mrow xref='id22'>
<mrow xref='id14'>
<msqrt>
<mn xref='id13'>&pi;</mn>
</msqrt>
</mrow>
<mo>&InvisibleTimes;</mo>
<mrow xref='id21'>
<mi>&Gamma;</mi>
<mo>&ApplyFunction;</mo>
<mfenced>
<mrow xref='id20'>
<mfrac xref='id16'>
<mn>1</mn>
<mn>2</mn>
</mfrac>
<mo>+</mo>
<mrow xref='id19'>
<mfrac xref='id17'>
<mn>1</mn>
<mn>2</mn>
</mfrac>
<mo>&InvisibleTimes;</mo>
<mi xref='id18'>n</mi>
</mrow>
</mrow>
</mfenced>
</mrow>
</mrow>
<mrow xref='id29'>
<mi>&Gamma;</mi>
<mo>&ApplyFunction;</mo>
<mfenced>
<mrow xref='id28'>
<mn xref='id24'>1</mn>
<mo>+</mo>
<mrow xref='id27'>
<mfrac xref='id25'>
<mn>1</mn>
<mn>2</mn>
</mfrac>
<mo>&InvisibleTimes;</mo>
<mi xref='id26'>n</mi>
</mrow>
</mrow>
</mfenced>
</mrow>
</mfrac>
</mrow>
</mrow>
</mrow>
<annotation-xml encoding='MathML-Content'>
<apply id='id32'>
<eq/>
<apply id='id11'>
<int/>
<bvar>
<ci id='id5'>x</ci>
</bvar>
<lowlimit>
<cn id='id1' type='integer'>0</cn>
</lowlimit>
<uplimit>
<apply id='id4'>
<times/>
<cn id='id2' type='rational'>
1
<sep/>
2
</cn>
<pi id='id3'/>
</apply>
</uplimit>
<apply id='id10'>
<power/>
<apply id='id8'>
<sin id='id6'/>
<ci id='id7'>x</ci>
</apply>
<ci id='id9'>n</ci>
</apply>
</apply>
<apply id='id31'>
<times/>
<cn id='id12' type='rational'>
1
<sep/>
2
</cn>
<apply id='id30'>
<divide/>
<apply id='id22'>
<times/>
<apply id='id14'>
<root/>
<pi id='id13'/>
</apply>
<apply id='id21'>
<csymbol id='id15' definitionURL='http://www.maplesoft.com/Mat
hML/GAMMA'>GAMMA</csymbol>
<apply id='id20'>
<plus/>
<cn id='id16' type='rational'>
1
<sep/>
2
</cn>
<apply id='id19'>
<times/>
<cn id='id17' type='rational'>
1
<sep/>
2
</cn>
<ci id='id18'>n</ci>
</apply>
</apply>
</apply>
</apply>
<apply id='id29'>
<csymbol id='id23' definitionURL='http://www.maplesoft.com/MathM
L/GAMMA'>GAMMA</csymbol>
<apply id='id28'>
<plus/>
<cn id='id24' type='integer'>1</cn>
<apply id='id27'>
<times/>
<cn id='id25' type='rational'>
1
<sep/>
2
</cn>
<ci id='id26'>n</ci>
</apply>
</apply>
</apply>
</apply>
</apply>
</apply>
</annotation-xml>
<annotation encoding='Maple'>Int(sin(x)^n,x = 0 .. 1/2*Pi) = 1/2*Pi^(1/2)*
GAMMA(1/2+1/2*n)/GAMMA(1+1/2*n)</annotation>
</semantics>
</math>

4.在网页中调用。
如果你把这些源代码直接贴到网页中,你会发现还是无法正确显示公式。因为我们必须加
一些必要的标示,以调用Applet。

<applet code=com.maplesoft.mathmlviewer width=234 height=84 align=middle>
<param name="background" value="#FFFFFF">
<param name="parser" value="mathml">
<param name="controls" value="false">
<param name="selection" value="true">
<param name="align" value="left">
<param name="padding" value="0">
<param name="size" value="12">
<param name="foreground" value="#0000FF">
<param name="linebreak" value="true">
<param name="valign" value="top">
<param name="eq" value="
此处贴入MathML源代码。
">
</applet>

其中<param...>标示对公式中的一些字体、颜色、大小进行了设置。

  另外,还有一个讨巧的办法,可以省去许多的时间。那就是直接使用“File”菜单中
的“Export As”选项,再在其中选“HTML with MathML”,然后选中“MathML 2.0 with
WebEQ”,就行了。你会发现Maple会在默认情况下生成一个images目录。在一般情况下这
个目录是空的,但是如果你的式子很长、很复杂时(超过2-3行时),Maple还是会选择图
片来将其导出,而不是使用MathML。