MathJax-demos-node icon indicating copy to clipboard operation
MathJax-demos-node copied to clipboard

How can I load my external package by mathjax-full v3.0.5 in Node Env

Open adamma1024 opened this issue 3 years ago • 2 comments

I want to use tex2mml api, and my code as follow:

  const adaptor = liteAdaptor();
  const handler = RegisterHTMLHandler(adaptor);
  AssistiveMmlHandler(handler);

  const packages = ['mhchem', 'extpfeil', 'unicode', 'boldsymbol', 'cancel', 'color', 'enclose'];

  const tex = new TeX({
      packages,
      macros: {
          ...
      },
  });

  const tex2mml = new HTMLDocument('', liteAdaptor(), { InputJax: tex });

  const visitor = new SerializedMmlVisitor();
  // @ts-expect-error
  const toMathML = (node) => visitor.visitTree(node, tex2mml);

But, I want to load external package to parser others LaTeX, like siunitx(for mathjax v3) package. And then , I try to use loader module

import {Loader, CONFIG} from 'mathjax-full/js/components/loader.js';
import {combineConfig} from 'mathjax-full/js/components/global.js';

const init = (config = {}) => {
    combineConfig(global.MathJax.config, config);
    return Loader.load(...CONFIG.load)
      .then(() => CONFIG.ready())
      .then(() => global.MathJax)                    // Pass MathJax global as argument to subsequent .then() calls
      .catch(error => CONFIG.failed(error));
}

const defaultOpt = {
        loader: {
            paths: {
                mathjax: 'mathjax-full/es5',
                custom: '../pkg'
            },
            load: ['input/tex', 'adaptors/liteDOM', '[custom]/siunitx']
        },
        tex: {
            packages: ['mhchem', 'extpfeil', 'unicode', 'boldsymbol', 'cancel', 'color', 'enclose'],
            macros: {
                    ...
            },
        },
    };

// get error: document is not defined
init(defaultOpt);

I trace this error , and find when load packages, it use script tag

document.createElement('script');

How can I fix it? Do u have some good idea? Thank u very much!

adamma1024 avatar Sep 26 '21 11:09 adamma1024

@dpvc anyone help? I need u davide~ 😭

adamma1024 avatar Sep 28 '21 03:09 adamma1024

Your original code uses the direct importing of MathJax modules (as illustrated in the direct examples of the MathJax node demos), and you are now trying to mix that with MathJax components, but these are not compatible approaches (unless you are trying to make a webpacked MathJax component yourself). You have to either use all direct imports or only use MathJax components.

There are two ways to do what you are trying to do. The first is to import the TeX extensions directly via

import 'mathjax-full/js/input/tex/mhchem/MhchemConfiguration.js';
import 'mathjax-full/js/input/tex/extpfeil/ExtpfeilConfiguration.js';
...

Or you could just

import 'mathjax-full/js/input/tex/AllPackages.js';

which will import all the packages (less efficient if you don't need them all, but easier to do).

dpvc avatar Oct 09 '21 20:10 dpvc