网页上输入数学公式TeX

来源:互联网 发布:淘宝官方旗舰店可信吗 编辑:程序博客网 时间:2024/05/16 18:37

 项目中需要在文本输入框中可以输入一些复杂的数学公式,网上调研了一下,发现一个好东西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 onkeyup=showTeX() onclick=showTeX() name=textarea rows=6 cols=50></TEXTAREA> </TD></TR>
<TR>
   <TABLE width="50%" border=0>
<TR>
<TD id=x^2 onmouseover="this.style.backgroundColor='#dddddd'" style="BACKGROUND-COLOR: #ffffff" onclick=showTxt() onmouseout="this.style.backgroundColor='#ffffff'" align=middle width=35>`x^2`</TD>
<TD id=a_n onmouseover="this.style.backgroundColor='#dddddd'" style="BACKGROUND-COLOR: #ffffff" onclick=showTxt() onmouseout="this.style.backgroundColor='#ffffff'" align=middle width=35>`a_n`</TD>
<TD id=a/b onmouseover="this.style.backgroundColor='#dddddd'" style="BACKGROUND-COLOR: #ffffff" onclick=showTxt() onmouseout="this.style.backgroundColor='#ffffff'" align=middle width=35>`a/b`</TD>
<TD id=sqrta onmouseover="this.style.backgroundColor='#dddddd'" style="BACKGROUND-COLOR: #ffffff" onclick=showTxt() onmouseout="this.style.backgroundColor='#ffffff'" align=middle width=35>`sqrta`</TD>
<TD id="`roota b`" onmouseover="this.style.backgroundColor='#dddddd'" style="BACKGROUND-COLOR: #ffffff" onclick=showTxt() onmouseout="this.style.backgroundColor='#ffffff'" align=middle width=35>`roota b`</TD>
<TD id="`int_a^b xdx`" onmouseover="this.style.backgroundColor='#dddddd'" style="BACKGROUND-COLOR: #ffffff" onclick=showTxt() onmouseout="this.style.backgroundColor='#ffffff'" align=middle width=35>`int_a^b xdx`</TD>
<TD id=oo onmouseover="this.style.backgroundColor='#dddddd'" style="BACKGROUND-COLOR: #ffffff" onclick=showTxt() onmouseout="this.style.backgroundColor='#ffffff'" align=middle width=35>`oo`</TD></TR>
<TR>
<TD id=* onmouseover="this.style.backgroundColor='#dddddd'" style="BACKGROUND-COLOR: #ffffff" onclick=showTxt() onmouseout="this.style.backgroundColor='#ffffff'" align=middle width=35>`*` </TD>
<TD id=** onmouseover="this.style.backgroundColor='#dddddd'" onclick=showTxt() onmouseout="this.style.backgroundColor='#ffffff'" align=middle width=35>`**`</TD>
<TD id=xx onmouseover="this.style.backgroundColor='#dddddd'" onclick=showTxt() onmouseout="this.style.backgroundColor='#ffffff'" align=middle width=35>`xx`</TD>
<TD id=-: onmouseover="this.style.backgroundColor='#dddddd'" onclick=showTxt() onmouseout="this.style.backgroundColor='#ffffff'" align=middle width=35>`-:`</TD>
<TD id="`<=`" onmouseover="this.style.backgroundColor='#dddddd'" onclick=showTxt() onmouseout="this.style.backgroundColor='#ffffff'" align=middle width=35>`&lt;=`</TD>
<TD id="`>=`" onmouseover="this.style.backgroundColor='#dddddd'" style="BACKGROUND-COLOR: #ffffff" onclick=showTxt() onmouseout="this.style.backgroundColor='#ffffff'" align=middle width=35>`&gt;=`</TD>
<TD id=!= onmouseover="this.style.backgroundColor='#dddddd'" style="BACKGROUND-COLOR: #ffffff" onclick=showTxt() onmouseout="this.style.backgroundColor='#ffffff'" align=middle width=35>`!=`</TD></TR>
<TR>
<TD id=in onmouseover="this.style.backgroundColor='#dddddd'" onclick=showTxt() onmouseout="this.style.backgroundColor='#ffffff'" align=middle width=35>`in`</TD>
<TD id=!in onmouseover="this.style.backgroundColor='#dddddd'" onclick=showTxt() onmouseout="this.style.backgroundColor='#ffffff'" align=middle width=35>`!in`</TD>
<TD id=sube onmouseover="this.style.backgroundColor='#dddddd'" onclick=showTxt() onmouseout="this.style.backgroundColor='#ffffff'" align=middle width=35>`sube`</TD>
<TD id=supe onmouseover="this.style.backgroundColor='#dddddd'" onclick=showTxt() onmouseout="this.style.backgroundColor='#ffffff'" align=middle width=35>`supe`</TD>
<TD id=nn onmouseover="this.style.backgroundColor='#dddddd'" onclick=showTxt() onmouseout="this.style.backgroundColor='#ffffff'" align=middle width=35>`nn`</TD>
<TD id=uu onmouseover="this.style.backgroundColor='#dddddd'" style="BACKGROUND-COLOR: #ffffff" onclick=showTxt() onmouseout="this.style.backgroundColor='#ffffff'" align=middle width=35>`uu`</TD>
<TD id=x^2 onmouseover="this.style.backgroundColor='#dddddd'" onclick=showTxt() onmouseout="this.style.backgroundColor='#ffffff'" align=middle width=35>&nbsp;</TD></TR></TABLE>
</body>
</html>

 

 

 

另外看到一个博客比较全面的介绍了输入数学公式的几种方法,可以参考:

http://www.lyyz.net/blog/user1/zyair/archives/2007/264.html