Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
adamma1024 opened this issue Sep 26, 2021 · 2 comments
Open
Labels

Comments

@adamma1024
Copy link

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 adamma1024 changed the title How can I load my external package by mathjax-full v3.0.5 How can I load my external package by mathjax-full v3.0.5 in Node Env Sep 26, 2021
@adamma1024
Copy link
Author

@dpvc anyone help? I need u davide~ 😭

@dpvc
Copy link
Member

dpvc commented Oct 9, 2021

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 dpvc added the Question label Oct 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants