exposing asciimath's symbol table

63 views
Skip to first unread message

Ben Crowell

unread,
Jun 27, 2015, 3:28:20 PM6/27/15
to mathja...@googlegroups.com
Peter Jipsen's original implementation of asciimath has a rather large table of built-in symbols, but he also provided a couple of API hooks for adding to it, e.g.:
  <script type="text/javascript">newcommand("mysymbol","\u210F")</script>
The more common problem I've encountered is that there are symbols that I don't want and that conflict with what I'm trying to do, e.g., he has a unary function "bar" for putting a bar over a symbol, which causes problems when I want to use the symbol hbar for the reduced Planck constant. Although he doesn't seem to have anticipated this, there are ways of getting around it. What I've been doing so far is to maintain my own private version of ASCIIMathML.js, with some symbols commented out. Now that I've gotten around to reading his code more carefully, it looks like there would also have been a cleaner way, which is that at run-time you simply delete entries from his global hash table AMsymbols.

However, I would like my application (http://www.lightandmatter.com/spotter/spotter.html ) to support other browsers besides firefox, so I'd like to switch to mathjax's implementation of asciimath. This seems to raise some problems, which were discussed here in 2011:
  https://groups.google.com/d/msg/mathjax-users/14yPzgrYjYw/Xncjr4uXeW0J
Based on a casual glance at the code on github, it looks like the situation is still probably the same as it was then. Two methods were discussed and attempted for incorporating the asciimath code into mathjax without polluting the global namespace: putting all the code inside a closure, or making it into an object. Everyone seemed to agree that the object method was preferable, but difficulties were encountered with making it work, so the closure method was used. If I'm understanding correctly, then this makes it impossible to use asciimath's two API hooks for adding symbols, and it also makes it impossible to delete entries from AMsymbols at run-time.

From previous discussion, it looks to me like there are other users who may also see this as a problem, e.g., College of the Redwoods, who are using asciimath in their OptiMath project.

First off, could anyone tell me whether the above analysis seems incorrect or out of date?

Assuming I've got it right, I'm thinking that the way to go would be to augment mathjax's existing configuration system for the asciimath parser, so that one could do something like this:

MathJax.Hub.Config({
  AsciiMath: {
    deleteSymbols: ["bar"],
addSymbols: [{input:"acos", tag:"mo", output:"acos", tex:null, ttype:UNARY, func:true}]
 } });

If this sounds like a good idea, then I'd be interested in coding this up and submitting it as a patch.

Davide P. Cervone

unread,
Jun 27, 2015, 5:07:37 PM6/27/15
to mathja...@googlegroups.com
There is an example of how to add new symbols to AsciiMath at


You can use a similar approach to remove symbols (using the AM.symbols array).  You could either search through the array to find the entry you want, or simply splice the array using the position of the symbol.

Davide


--
You received this message because you are subscribed to the Google Groups "MathJax Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mathjax-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ben Crowell

unread,
Jun 27, 2015, 6:26:23 PM6/27/15
to mathja...@googlegroups.com, dp...@union.edu
Aha! Thanks, Davide, that looks like exactly what I needed!

-Ben

Ben Crowell

unread,
Jun 27, 2015, 8:06:58 PM6/27/15
to mathja...@googlegroups.com, dp...@union.edu
Below is a minimal working example, which might be helpful to others.

=========================================================================

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Test of adding and deleting symbols in mathjax's implementation of asciimath</title>
    <script type="text/x-mathjax-config">
      MathJax.Hub.Register.StartupHook("AsciiMath Jax Config",function () {
        var AM = MathJax.InputJax.AsciiMath.AM;
        var sym = AM.symbols;

        // Treat the following as functions, i.e., don't italicize them.
        var functions_to_add = ["asin","acos","atan","asinh","acosh","atanh"];
        function add_function(name) {
          sym.push(
            {input:name,  tag:"mo", output:name, tex:null, ttype:AM.TOKEN.UNARY, func:true}
          );
        }
        for (var i=0; i < functions_to_add.length; i++) {
          add_function(functions_to_add[i]);
        }

        // Don't treat the following as symbols.
        var functions_to_delete = ["Lim","det","dim","mod","gcd","lcm","lub","glb","min","max"];
        function delete_function(name) {
          for (var i=0; i < sym.length; i++) {
            if (name===sym[i].input) { sym.splice(i,1); break; }
          }
        }
        for (var i=0; i < functions_to_delete.length; i++) {
          delete_function(functions_to_delete[i]);
        }
      });
    </script>
    <script src='https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=AM_HTMLorMML.js'></script>
  </head>

<body>

<p>` cos(acos x) asin x blob x atanh x`</p>

<p>` daisy x Lim x det x max x`</p>


</body>

</html>

Davide P. Cervone

unread,
Jun 28, 2015, 10:21:32 AM6/28/15
to mathja...@googlegroups.com
Thanks for sharing your example.

Davide


Reply all
Reply to author
Forward
0 new messages