Hanging MathJax.init

7 views
Skip to first unread message

Brian Richwine

unread,
Oct 10, 2025, 9:50:32 AM (5 days ago) Oct 10
to MathJax Development
I'm trying to initialize mathjax@4 ("mathjax": "^4.0.0") in a node express app to make an upgrade to the existing Pressbooks mathjax microservice.  I'm trying to make an equivalent to MathJax@3's AllPackages with mhchem and physics. But the MathJax.init() promise never resolves when I add certain packages and I'm not sure how to proceed.

I'm trying to mimic the existing packages in pb-mathjax. They use "mathjax-full": "^3.2.2", load AllPackages, mhchem, and physics:
const { mathjax } = require('mathjax-full/js/mathjax.js');
const { TeX } = require('mathjax-full/js/input/tex.js');
const { SVG } = require('mathjax-full/js/output/svg.js');
const { MathML } = require('mathjax-full/js/input/mathml.js');
const { AsciiMath } = require('mathjax-full/js/input/asciimath.js');
const { liteAdaptor } = require('mathjax-full/js/adaptors/liteAdaptor.js');
const { RegisterHTMLHandler } = require('mathjax-full/js/handlers/html.js');
const { AllPackages } = require('mathjax-full/js/input/tex/AllPackages.js');
...
// Configure TeX input
let tex = new TeX({
packages: configs.typeset.math.includes('\\require{physics}') ? AllPackages.concat(['physics']) : AllPackages,
inlineMath: [['$', '$'], ['\\(', '\\)']],
displayMath: [['$$', '$$'], ['\\[', '\\]']]
});


I'm using mathjax@4 ("mathjax": "^4.0.0") and
The following (first example) works (https://github.com/brichwin/pb-math-service/blob/main/services/mathJaxConverters.js), but if I add '[tex]/mhchem' (second example) it hangs:
const MathJax = require('mathjax');
const { toBool, toNum } = require('../utils');

// Store the initialization promise
const mathJaxReady = MathJax.init({
loader: {
load: [
'input/tex',
'input/mml',
'input/asciimath',
'output/svg',
'adaptors/liteDOM',
// TeX extensions (rough equivalent of MathJax3's AllPackages + mhchem, physics)
'[tex]/physics'
]
},
startup: {
typeset: false
},
tex: {
// Add all loaded TeX packages to the processing list
packages: {
'[+]': [
'physics'
]
}
},
svg: {
fontCache: 'none'
}
}).then(() => {
console.log('✓ MathJax initialized successfully');
}).catch((err) => {
console.error('✗ MathJax initialization error:', err.message);
throw err;
});
The following (with mhchem) never resolves:
const MathJax = require('mathjax');
const { toBool, toNum } = require('../utils');

// Store the initialization promise
const mathJaxReady = MathJax.init({
loader: {
load: [
'input/tex',
'input/mml',
'input/asciimath',
'output/svg',
'adaptors/liteDOM',
'[tex]/mhchem',
'[tex]/physics'
]
},
startup: {
typeset: false
},
tex: {
// Add all loaded TeX packages to the processing list
packages: {
'[+]': [
'mhchem',
'physics'
]
}
},
svg: {
fontCache: 'none'
}
}).then(() => {
console.log('✓ MathJax initialized successfully');
}).catch((err) => {
console.error('✗ MathJax initialization error:', err.message);
throw err;
});


Thanks for any help/suggestions!
  Brian

Brian Richwine

unread,
Oct 10, 2025, 12:59:23 PM (5 days ago) Oct 10
to MathJax Development
Ah.... got it I think. Does this look close to MathJax3's AllPackages?

I manually installed the mhchem package bits and switched to mathjax/src:

"@mathjax/mathjax-mhchem-font-extension": "^4.0.0",
"@mathjax/mathjax-newcm-font": "^4.0.0",
"@mathjax/src": "^4.0.0",
"mhchemparser": "^4.2.1",
then switched mathJaxConverters.js to use:
const CoreV3ish = [
// mirrors MathJax v3's AllPackages (practical subset)
'ams', 'boldsymbol', 'bbox', 'braket', 'cancel',
'color', 'colorv2', 'enclose', 'html', 'mathtools', 'newcommand',
'noundefined', 'noerrors', 'setoptions', 'tagformat',
'textmacros', 'unicode', 'upgreek'
];

// Add the extras you want:
const WantedPackages = [...new Set([...CoreV3ish, 'mhchem', 'physics'])];

// Init: you can either load them all, or just load what you need.
// Loading all is fine in a server where you want feature parity.
global.MathJax ={
loader: {
paths: { mathjax: '@mathjax/src/bundle' },
require: require,
load: [
'input/tex', 'input/mml', 'input/asciimath',
'output/svg', 'adaptors/liteDOM',
// Load the TeX extensions as components (prefixed form):
...WantedPackages.map(p => `[tex]/${p}`)
]
},
startup: { typeset: false },
tex: {
// enable the packages in the TeX input jax
packages: { '[+]': WantedPackages },
inlineMath: [['$', '$'], ['\\(', '\\)']],
displayMath: [['$$', '$$'], ['\\[', '\\]']]
},
svg: {
fontCache: 'none',
font: 'mathjax-newcm'
}
};

// Load the startup component and wait for it
require('@mathjax/src/bundle/startup.js');

const mathJaxReady = MathJax.startup.promise.then(() => {
console.log('✓ MathJax initialized successfully');
}).catch((err) => {
console.error('✗ MathJax initialization error:', err.message);
throw err;
});

Davide Cervone

unread,
Oct 11, 2025, 2:48:19 PM (4 days ago) Oct 11
to mathj...@googlegroups.com
Ah.... got it I think. Does this look close to MathJax3's AllPackages?

Well, the original package list is

  'base',
  'action',
  'ams',
  'amscd',
  'bbox',
  'boldsymbol',
  'braket',
  'bussproofs',
  'cancel',
  'cases',
  'centernot',
  'color',
  'colortbl',
  'empheq',
  'enclose',
  'extpfeil',
  'gensymb',
  'html',
  'mathtools',
  'mhchem',
  'newcommand',
  'noerrors',
  'noundefined',
  'upgreek',
  'unicode',
  'verb',
  'configmacros',
  'tagformat',
  'textcomp',
  'textmacros'

so your list isn't quite the same.  You probably don't want to include both color and colorv2, since they both define the \color macro, but not in a compatible way.

Also, you should not need to add @mathjax/mathjax-newcm-font as that should be included by @mathjax/src already.  Similarly, you don't need to load mhchemparser.  So you should only need to install @mathjax/src and @mathjax-mhchem-font-extension by hand.

Your line

const WantedPackages = [...new Set([...CoreV3ish, 'mhchem', 'physics'])];

could just be

const WantedPackages = [...CoreV3ish, 'mhchem', 'physics'];

(there is no need to go through the Set() object).

Also, you may want to load input/tex-base rather than input/tex to avoid messages about missing version numbers for the packages that are already loaded by input/tex but that you are loading again in your list.

Your original code from your first message SHOULD have worked, but there turns out to be a problem with the dependency for the mathjax-mhchem-font-extension package (which depends on output/svg in your case, but is dealing loaded by output/svg, and so there is a lock-out because the two are waiting for each other to complete).  I will fix that in the next release, but your new code should do the trick for now.  Alternatively, your original code words if you use @mathjax/src and

const MathJax = require('@mathjax/src/source');

(note the "/source" addition there).

Davide


-- 
You received this message because you are subscribed to the Google Groups "MathJax Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mathjax-dev...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/mathjax-dev/c02e6bef-8a78-430a-860d-faabbb1428f0n%40googlegroups.com.

Davide Cervone

unread,
Oct 11, 2025, 2:53:07 PM (4 days ago) Oct 11
to mathj...@googlegroups.com
PS, there is code in the documentation for how to get all the package names in node or a web page.  I will need to add a filter() call in order to remove colorv2, and perhaps some of the other non-essentials, but the main idea is there.

Davide


Reply all
Reply to author
Forward
0 new messages