mathjax / MathJax-node

MathJax for Node
Apache License 2.0
615 stars 97 forks source link

Access MathJax.Hub in node? #318

Closed beeplin closed 7 years ago

beeplin commented 7 years ago

mathjax-node only exposes methods such as typeset, start and config, but how to access MathJax.Hub.xxx in NodeJS?

beeplin commented 7 years ago

Currently I found a workaround -- or, a hacking, by adding a new method getMathJax to exports:

exports.getMathJax = function () {
  return MathJax;
}

And then in my node source code, call mjAPI.getMathJax() in the callback of the first calling of mjAPI.typeset():

mjAPI.typeset({}, () => { // empty typeset, only for initializing MathJax
  MathJax = mjAPI.getMathJax();
  MathJax.Hub.Register.StartupHook('xxx', () => {
    ...
  }
})

For Now it works fine, but I think it's better to design an better official API for doing this.

pkra commented 7 years ago

how to access MathJax.Hub.xxx in NodeJS?

I would avoid it if possible (since there might be negative side effects) but you can do this via the MathJax configuration, cf. http://docs.mathjax.org/en/latest/configuration.html#using-plain-javascript

   mjAPI.config = {
      MathJax: {
        AuthorInit: function () {
        MathJax.Hub.Register.StartupHook("xxx",function () {
          ...
        });
      }
      }
    }
beeplin commented 7 years ago

that is just what I want~ thanks!

dpvc commented 7 years ago

I don't think you can use AuthorInit in mathjax-node since mathjax-node sets AuthorInit itself in order to install the changes that it needs. So setting this would wipe out mathjax-node's changes.

Can you be more specific about what your hook is going to do? Perhaps there is another way to handle it.

beeplin commented 7 years ago

Yeah I just figured out the AuthorInit isn't working. I am using asciimath and I need to define some new symbols in it, like this:

    MathJax.Hub.Register.StartupHook('AsciiMath Jax Ready', function () {
      AM = MathJax.InputJax.AsciiMath.AM
      AM.newsymbol({ input: '>=', tag: 'mo', output: '\u2A7E', tex: 'geqslant', ttype: AM.TOKEN.CONST })
      AM.newsymbol({ input: '<=', tag: 'mo', output: '\u2A7D', tex: 'leqslant', ttype: AM.TOKEN.CONST })
      AM.newsymbol({ input: 'comp', tag: 'mo', output: '\u2201', tex: 'complement', ttype: AM.TOKEN.CONST })
      AM.newsymbol({ input: 'sim', tag: 'mo', output: '\u223C', tex: null, ttype: AM.TOKEN.CONST })
      AM.newsymbol({ input: 'sub', tag: 'mo', output: '\u2ACB', tex: 'subsetneqq', ttype: AM.TOKEN.CONST })
      AM.newsymbol({ input: 'sup', tag: 'mo', output: '\u2ACC', tex: 'supsetneqq', ttype: AM.TOKEN.CONST })
      AM.newsymbol({ input: "//", tag: "mo", output: "\u2225", tex: null, ttype: AM.TOKEN.CONST })
      AM.newsymbol({ input: "||", tag: "mo", output: "\u2225", tex: null, ttype: AM.TOKEN.CONST })
      AM.newsymbol({ input: "!//", tag: "mo", output: "\u2226", tex: null, ttype: AM.TOKEN.CONST })
      AM.newsymbol({ input: "!||", tag: "mo", output: "\u2226", tex: null, ttype: AM.TOKEN.CONST })
    })
pkra commented 7 years ago

One way to solve this is to implement #134 and then turn the modification into a proper extension.

dpvc commented 7 years ago

You don't need #134 for this. Just put

MathJax.Hub.Register.StartupHook('AsciiMath Jax Ready', function () {
  var AM = MathJax.InputJax.AsciiMath.AM;
  AM.newsymbol({ input: '>=', tag: 'mo', output: '\u2A7E', tex: 'geqslant', ttype: AM.TOKEN.CONST })
  AM.newsymbol({ input: '<=', tag: 'mo', output: '\u2A7D', tex: 'leqslant', ttype: AM.TOKEN.CONST })
  AM.newsymbol({ input: 'comp', tag: 'mo', output: '\u2201', tex: 'complement', ttype: AM.TOKEN.CONST })
  AM.newsymbol({ input: 'sim', tag: 'mo', output: '\u223C', tex: null, ttype: AM.TOKEN.CONST })
  AM.newsymbol({ input: 'sub', tag: 'mo', output: '\u2ACB', tex: 'subsetneqq', ttype: AM.TOKEN.CONST })
  AM.newsymbol({ input: 'sup', tag: 'mo', output: '\u2ACC', tex: 'supsetneqq', ttype: AM.TOKEN.CONST })
  AM.newsymbol({ input: "//", tag: "mo", output: "\u2225", tex: null, ttype: AM.TOKEN.CONST })
  AM.newsymbol({ input: "||", tag: "mo", output: "\u2225", tex: null, ttype: AM.TOKEN.CONST })
  AM.newsymbol({ input: "!//", tag: "mo", output: "\u2226", tex: null, ttype: AM.TOKEN.CONST })
  AM.newsymbol({ input: "!||", tag: "mo", output: "\u2226", tex: null, ttype: AM.TOKEN.CONST })
});

MathJax.Ajax.loadComplete("file://path-to-file");

into a file, replacing path-to-file by the full unix path reference to the file (e.g., /users/dpvc/mathjax/asciimath-extension.js), and then use

mjAPI.config({
  extensions: "file:///users/dpvc/mathjax/asciimath-extension.js"
});

before calling mjAPI.typeset() for the first time. That should do it.

beeplin commented 7 years ago

@dpvc thanks for this extension guide. It does work. :)