使用mathjax和asciimathml中的单位

时间:2016-07-28 20:14:30

标签: mathjax

我们使用MathJax以度量单位呈现ASCIIMathML方程。我们发现MathJax对自己的好处太聪明了......

3.14 in

呈现为......

3.14∈

其中ε是集合成员符号。

有关改善这种聪明才智的建议吗? doc有一些选项,但似乎没有任何选项可以涵盖这个用例。

这些是学生输入的字符串,因此我们无法让他们了解MathJax的细微差别。

谢谢! 吉姆

1 个答案:

答案 0 :(得分:2)

如果你想在数字和单位之间留一个空格,3.14 text{in}3.14 text{ in}怎么样?

由于您尝试使用AsciiMath进行学生输入,因此一种方法是限制可用命令的数量。例如,您可以修改符号列表以删除不需要的符号列表。一种方法是简单地将列表替换为仅包含您想要的列表(这可能是最简单的方法)。例如,您可以将以下代码放在页面之前加载MathJax.js本身的脚本标记。

<script type="text/x-mathjax-config">
MathJax.Hub.Register.StartupHook("AsciiMath Jax Config",function () {
  var AM = MathJax.InputJax.AsciiMath.AM;
  var CONST = AM.TOKEN.CONST,
      UNARY = AM.TOKEN.UNARY,
      BINARY = AM.TOKEN.BINARY,
      INFIX = AM.TOKEN.INFIX,
      TEXT = AM.TOKEN.TEXT,
      DEFINITION = AM.TOKEN.DEFITION,
      UNDEROVER = AM.TOKEN.UNDEROVER,
      LEFTRIGHT = AM.TOKEN.LEFTRIGHT,
      LEFTBRACKET = AM.TOKEN.LEFTBRACKET,
      RIGHTBRACKET = AM.TOKEN.RIGHTBRACKET;

  AM.symbols.splice(0,AM.symbols.length,
    {input:"*",  tag:"mo", output:"\u22C5", tex:"cdot", ttype:CONST},
    {input:"-:", tag:"mo", output:"\u00F7", tex:"div", ttype:CONST},
    {input:"divide",   tag:"mo", output:"-:", tex:null, ttype:DEFINITION},
    {input:"!=",  tag:"mo", output:"\u2260", tex:"ne", ttype:CONST},
    {input:"lt",  tag:"mo", output:"<",      tex:null, ttype:CONST},
    {input:"<=",  tag:"mo", output:"\u2264", tex:"le", ttype:CONST},
    {input:"lt=", tag:"mo", output:"\u2264", tex:"leq", ttype:CONST},
    {input:"gt",  tag:"mo", output:">",      tex:null, ttype:CONST},
    {input:">=",  tag:"mo", output:"\u2265", tex:"ge", ttype:CONST},
    {input:"gt=", tag:"mo", output:"\u2265", tex:"geq", ttype:CONST},
    //grouping brackets
    {input:"(", tag:"mo", output:"(", tex:null, ttype:LEFTBRACKET},
    {input:")", tag:"mo", output:")", tex:null, ttype:RIGHTBRACKET},
    {input:"[", tag:"mo", output:"[", tex:null, ttype:LEFTBRACKET},
    {input:"]", tag:"mo", output:"]", tex:null, ttype:RIGHTBRACKET},
    {input:"{", tag:"mo", output:"{", tex:null, ttype:LEFTBRACKET},
    {input:"}", tag:"mo", output:"}", tex:null, ttype:RIGHTBRACKET},
    {input:"|", tag:"mo", output:"|", tex:null, ttype:LEFTRIGHT},
    {input:"(:", tag:"mo", output:"\u2329", tex:"langle", ttype:LEFTBRACKET},
    {input:":)", tag:"mo", output:"\u232A", tex:"rangle", ttype:RIGHTBRACKET},
    {input:"<<", tag:"mo", output:"\u2329", tex:null, ttype:LEFTBRACKET},
    {input:">>", tag:"mo", output:"\u232A", tex:null, ttype:RIGHTBRACKET},
    {input:"{:", tag:"mo", output:"{:", tex:null, ttype:LEFTBRACKET, invisible:true},
    {input:":}", tag:"mo", output:":}", tex:null, ttype:RIGHTBRACKET, invisible:true},
    //miscellaneous symbols
    {input:"+-",   tag:"mo", output:"\u00B1", tex:"pm", ttype:CONST},
    {input:"O/",   tag:"mo", output:"\u2205", tex:"emptyset", ttype:CONST},
    {input:"oo",   tag:"mo", output:"\u221E", tex:"infty", ttype:CONST},
    {input:"...",  tag:"mo", output:"...",    tex:"ldots", ttype:CONST},
    {input:"'",   tag:"mo", output:"\u2032",  tex:"prime", ttype:CONST},
    {input:"CC",  tag:"mo", output:"\u2102", tex:null, ttype:CONST},
    {input:"NN",  tag:"mo", output:"\u2115", tex:null, ttype:CONST},
    {input:"QQ",  tag:"mo", output:"\u211A", tex:null, ttype:CONST},
    {input:"RR",  tag:"mo", output:"\u211D", tex:null, ttype:CONST},
    {input:"ZZ",  tag:"mo", output:"\u2124", tex:null, ttype:CONST},
    {input:"f",   tag:"mi", output:"f",      tex:null, ttype:UNARY, func:true},
    {input:"g",   tag:"mi", output:"g",      tex:null, ttype:UNARY, func:true},
    //standard functions
    {input:"lim",  tag:"mo", output:"lim", tex:null, ttype:UNDEROVER},
    {input:"Lim",  tag:"mo", output:"Lim", tex:null, ttype:UNDEROVER},
    {input:"sin",  tag:"mo", output:"sin", tex:null, ttype:UNARY, func:true},
    {input:"cos",  tag:"mo", output:"cos", tex:null, ttype:UNARY, func:true},
    {input:"tan",  tag:"mo", output:"tan", tex:null, ttype:UNARY, func:true},
    {input:"sinh", tag:"mo", output:"sinh", tex:null, ttype:UNARY, func:true},
    {input:"cosh", tag:"mo", output:"cosh", tex:null, ttype:UNARY, func:true},
    {input:"tanh", tag:"mo", output:"tanh", tex:null, ttype:UNARY, func:true},
    {input:"cot",  tag:"mo", output:"cot", tex:null, ttype:UNARY, func:true},
    {input:"sec",  tag:"mo", output:"sec", tex:null, ttype:UNARY, func:true},
    {input:"csc",  tag:"mo", output:"csc", tex:null, ttype:UNARY, func:true},
    {input:"arcsin",  tag:"mo", output:"arcsin", tex:null, ttype:UNARY, func:true},
    {input:"arccos",  tag:"mo", output:"arccos", tex:null, ttype:UNARY, func:true},
    {input:"arctan",  tag:"mo", output:"arctan", tex:null, ttype:UNARY, func:true},
    {input:"coth",  tag:"mo", output:"coth", tex:null, ttype:UNARY, func:true},
    {input:"sech",  tag:"mo", output:"sech", tex:null, ttype:UNARY, func:true},
    {input:"csch",  tag:"mo", output:"csch", tex:null, ttype:UNARY, func:true},
    {input:"exp",  tag:"mo", output:"exp", tex:null, ttype:UNARY, func:true},
    {input:"abs",   tag:"mo", output:"abs",  tex:null, ttype:UNARY, rewriteleftright:["|","|"]},
    {input:"log",  tag:"mo", output:"log", tex:null, ttype:UNARY, func:true},
    {input:"ln",   tag:"mo", output:"ln",  tex:null, ttype:UNARY, func:true},
    {input:"gcd",  tag:"mo", output:"gcd", tex:null, ttype:UNARY, func:true},
    {input:"lcm",  tag:"mo", output:"lcm", tex:null, ttype:UNARY, func:true},
    {input:"min",  tag:"mo", output:"min", tex:null, ttype:UNDEROVER},
    {input:"max",  tag:"mo", output:"max", tex:null, ttype:UNDEROVER},
    //arrows
    {input:"uarr", tag:"mo", output:"\u2191", tex:"uparrow", ttype:CONST},
    {input:"darr", tag:"mo", output:"\u2193", tex:"downarrow", ttype:CONST},
    {input:"rarr", tag:"mo", output:"\u2192", tex:"rightarrow", ttype:CONST},
    {input:"->",   tag:"mo", output:"\u2192", tex:"to", ttype:CONST},
    {input:">->",   tag:"mo", output:"\u21A3", tex:"rightarrowtail", ttype:CONST},
    {input:"->>",   tag:"mo", output:"\u21A0", tex:"twoheadrightarrow", ttype:CONST},
    {input:">->>",   tag:"mo", output:"\u2916", tex:"twoheadrightarrowtail", ttype:CONST},
    {input:"|->",  tag:"mo", output:"\u21A6", tex:"mapsto", ttype:CONST},
    {input:"larr", tag:"mo", output:"\u2190", tex:"leftarrow", ttype:CONST},
    {input:"harr", tag:"mo", output:"\u2194", tex:"leftrightarrow", ttype:CONST},
    {input:"rArr", tag:"mo", output:"\u21D2", tex:"Rightarrow", ttype:CONST},
    {input:"lArr", tag:"mo", output:"\u21D0", tex:"Leftarrow", ttype:CONST},
    {input:"hArr", tag:"mo", output:"\u21D4", tex:"Leftrightarrow", ttype:CONST},
    //commands with argument
    {input:"sqrt", tag:"msqrt", output:"sqrt", tex:null, ttype:UNARY},
    {input:"root", tag:"mroot", output:"root", tex:null, ttype:BINARY},
    {input:"frac", tag:"mfrac", output:"/",    tex:null, ttype:BINARY},
    {input:"/",    tag:"mfrac", output:"/",    tex:null, ttype:INFIX},
    {input:"_",    tag:"msub",  output:"_",    tex:null, ttype:INFIX},
    {input:"^",    tag:"msup",  output:"^",    tex:null, ttype:INFIX},
    {input:"text", tag:"mtext", output:"text", tex:null, ttype:TEXT},
    {input:"color", tag:"mstyle", ttype:BINARY},
    {input:"cancel", tag:"menclose", output:"cancel", tex:null, ttype:UNARY}
  );
});
</script>

也可以将其转换为可以加载的配置文件,而不必直接将其包含在每个页面中。

我不确定所有这些都适合您的情况,但您可以删除那些您认为不合适的情况。请确保您没有在列表中最后一个末尾留下额外的逗号

最后,如果您希望“in”之类的内容能够用于英寸,那么您可以为它们添加命令,例如

{input:"in", tag:"mtext", output:"\u2006in", tex:null, ttype:CONST},

说在列表的顶部。 (此处\u2006是表示少量空间的字符。)您可能还需要包含其他几种形式,例如

{input:"in", tag:"mtext", output:"\u2006in", tex:null, ttype:CONST},
{input:"inch", tag:"mtext", output:"\u2006in", tex:null, ttype:CONST},
{input:"inches", tag:"mtext", output:"\u2006in", tex:null, ttype:CONST},

包括您可能需要的任何单位。