项目中需要在文本输入框中可以输入一些复杂的数学公式,网上调研了一下,发现一个好东西TeX 。
TeX 是Knuth 教授开发的一种优秀的桌面电子排版系统。它提供了一套功能强大并且十分灵活的排版语言,有多达900多条指令,并且具有宏功能,用户可以不断地定义自己适用的新命令来扩展TeX系统的功能。
使用办法很简单,先在网上下载ASCIIMathMLwFallback.js 或 ASCIIMathTeXImg.js 存放到网站根目录,然后在网页中写入
<script type="text/javascript" src="ASCIIMathMLwFallback.js"></script>
或
<script type="text/javascript" src="ASCIIMathTeXImg.js"></script>
我在试验时,ASCIIMathMLwFallback.js不能用,图片显示不出来,不知道什么原因,所以用了ASCIIMathTeXImg.js
然后在想输出公式的地方书写Tex语言就可以显示相关公式,比如想显示根号3,则写`sqrt3`
参考:http://www.pierce.ctc.edu/dlippman/asciimathtex/AMT.html
可以直接将下面的代码保存为html看效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>数学公式</title>
<script type="text/javascript" src="ASCIIMathMLwFallback.js"></script>
<script type="text/javascript" src="ASCIIMathTeXImg.js"></script>
</head>
<body>
<table><tr>
<TD>`x^2+y_1+z_12^34`</TD>
<tr></table>
</body>
</html>
下面还有一个sample:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>数学公式</title>
<script type="text/javascript" src="ASCIIMathMLwFallback.js"></script>
<script type="text/javascript" src="ASCIIMathTeXImg.js"></script>
<SCRIPT>
function showTeX(){
sel=document.activeElement;
t = sel.value;
document.getElementById('showTeXs').innerHTML = t;
ASCIIMathDisplay = document.getElementById('showTeXs');
AMprocessNode(ASCIIMathDisplay);
}
function showTxt(){
sel = document.activeElement;
txt2.value = txt2.value + sel.id;
}
</SCRIPT>
</head>
<body>
<TABLE width="50%" border=0>
<TD id=showTeXs></TD></TR>
<TD><TEXTAREA id=txt2 οnkeyup=showTeX() οnclick=showTeX() name=textarea rows=6 cols=50></TEXTAREA> </TD></TR>
<TR>
<TABLE width="50%" border=0>
<TR>
<TD id=x^2 οnmοuseοver="this.style.backgroundColor='#dddddd'" style="BACKGROUND-COLOR: #ffffff" οnclick=showTxt() οnmοuseοut="this.style.backgroundColor='#ffffff'" align=middle width=35>`x^2`</TD>
<TD id=a_n οnmοuseοver="this.style.backgroundColor='#dddddd'" style="BACKGROUND-COLOR: #ffffff" οnclick=showTxt() οnmοuseοut="this.style.backgroundColor='#ffffff'" align=middle width=35>`a_n`</TD>
<TD id=a/b οnmοuseοver="this.style.backgroundColor='#dddddd'" style="BACKGROUND-COLOR: #ffffff" οnclick=showTxt() οnmοuseοut="this.style.backgroundColor='#ffffff'" align=middle width=35>`a/b`</TD>
<TD id=sqrta οnmοuseοver="this.style.backgroundColor='#dddddd'" style="BACKGROUND-COLOR: #ffffff" οnclick=showTxt() οnmοuseοut="this.style.backgroundColor='#ffffff'" align=middle width=35>`sqrta`</TD>
<TD id="`roota b`" οnmοuseοver="this.style.backgroundColor='#dddddd'" style="BACKGROUND-COLOR: #ffffff" οnclick=showTxt() οnmοuseοut="this.style.backgroundColor='#ffffff'" align=middle width=35>`roota b`</TD>
<TD id="`int_a^b xdx`" οnmοuseοver="this.style.backgroundColor='#dddddd'" style="BACKGROUND-COLOR: #ffffff" οnclick=showTxt() οnmοuseοut="this.style.backgroundColor='#ffffff'" align=middle width=35>`int_a^b xdx`</TD>
<TD id=oo οnmοuseοver="this.style.backgroundColor='#dddddd'" style="BACKGROUND-COLOR: #ffffff" οnclick=showTxt() οnmοuseοut="this.style.backgroundColor='#ffffff'" align=middle width=35>`oo`</TD></TR>
<TR>
<TD id=* οnmοuseοver="this.style.backgroundColor='#dddddd'" style="BACKGROUND-COLOR: #ffffff" οnclick=showTxt() οnmοuseοut="this.style.backgroundColor='#ffffff'" align=middle width=35>`*` </TD>
<TD id=** οnmοuseοver="this.style.backgroundColor='#dddddd'" οnclick=showTxt() οnmοuseοut="this.style.backgroundColor='#ffffff'" align=middle width=35>`**`</TD>
<TD id=xx οnmοuseοver="this.style.backgroundColor='#dddddd'" οnclick=showTxt() οnmοuseοut="this.style.backgroundColor='#ffffff'" align=middle width=35>`xx`</TD>
<TD id=-: οnmοuseοver="this.style.backgroundColor='#dddddd'" οnclick=showTxt() οnmοuseοut="this.style.backgroundColor='#ffffff'" align=middle width=35>`-:`</TD>
<TD id="`<=`" οnmοuseοver="this.style.backgroundColor='#dddddd'" οnclick=showTxt() οnmοuseοut="this.style.backgroundColor='#ffffff'" align=middle width=35>`<=`</TD>
<TD id="`>=`" οnmοuseοver="this.style.backgroundColor='#dddddd'" style="BACKGROUND-COLOR: #ffffff" οnclick=showTxt() οnmοuseοut="this.style.backgroundColor='#ffffff'" align=middle width=35>`>=`</TD>
<TD id=!= οnmοuseοver="this.style.backgroundColor='#dddddd'" style="BACKGROUND-COLOR: #ffffff" οnclick=showTxt() οnmοuseοut="this.style.backgroundColor='#ffffff'" align=middle width=35>`!=`</TD></TR>
<TR>
<TD id=in οnmοuseοver="this.style.backgroundColor='#dddddd'" οnclick=showTxt() οnmοuseοut="this.style.backgroundColor='#ffffff'" align=middle width=35>`in`</TD>
<TD id=!in οnmοuseοver="this.style.backgroundColor='#dddddd'" οnclick=showTxt() οnmοuseοut="this.style.backgroundColor='#ffffff'" align=middle width=35>`!in`</TD>
<TD id=sube οnmοuseοver="this.style.backgroundColor='#dddddd'" οnclick=showTxt() οnmοuseοut="this.style.backgroundColor='#ffffff'" align=middle width=35>`sube`</TD>
<TD id=supe οnmοuseοver="this.style.backgroundColor='#dddddd'" οnclick=showTxt() οnmοuseοut="this.style.backgroundColor='#ffffff'" align=middle width=35>`supe`</TD>
<TD id=nn οnmοuseοver="this.style.backgroundColor='#dddddd'" οnclick=showTxt() οnmοuseοut="this.style.backgroundColor='#ffffff'" align=middle width=35>`nn`</TD>
<TD id=uu οnmοuseοver="this.style.backgroundColor='#dddddd'" style="BACKGROUND-COLOR: #ffffff" οnclick=showTxt() οnmοuseοut="this.style.backgroundColor='#ffffff'" align=middle width=35>`uu`</TD>
<TD id=x^2 οnmοuseοver="this.style.backgroundColor='#dddddd'" οnclick=showTxt() οnmοuseοut="this.style.backgroundColor='#ffffff'" align=middle width=35> </TD></TR></TABLE>
</body>
</html>
另外看到一个博客比较全面的介绍了输入数学公式的几种方法,可以参考: