That is also possible. You haven't given your current configuration, so I can't tell.
___________________________________
I finally broke down and installed browserify to test out your code, and it worked fine for me. So there is something different in your installation that you aren't telling me about, and so I can't point it out to you. Here is what I did:
mkdir project
cd project
pnpm install browserify
pnpm install @mathjax/src
pnpm install @mathjax/mathjax-newcm-font
ln -s node_modules/@mathjax/src/bundle mathjax
ln -s node_modules/@mathjax/mathjax-newcm-font mathjax-newcm
The last two lines link top-level directories for the Mathjax bundle and newcm font directories. If you can't do symbolic links, just copy those directories to the top-level ones.
Then I created the transform.js file that I listed above. Then I made a file main.js containing
////BEGIN code
global.MathJax = {
loader: {
paths: {
mathjax: './mathjax',
'mathjax-newcm': './mathjax-newcm',
}
},
options: {},
tex: {
// packages: {
// '[+]': ['xypic', 'img']
// }
// ,
processEnvironments: true,
maxBuffer: 200 * 1024,
maxMacros: 40000,
noundefined: {
color: 'red',
background: '',
size: ''
}, //END , noundefined:
formatError(_, error) {
throw error;
},
macros: {
//
RR: ['{\\bf{R}}'],
bold: ['\\boldsymbol{#1}', 1],
ddx: ['\\frac{d#2}{d#1}', 2, 'x'],
abc: ['(#1)', 1, [null, '\\cba']],
ud: ['{#1^ \\dagger}', 1],
Tr: ['{\\mathrm{Tr}}'],
bold2: ['{\\bf #1}', 1],
c1y: ['{\\color{yellow}{#1}}'],
cy: ['{\\color{yellow}#1}'],
ktc: ['{\\color{yellow}{\\boldsymbol{\\mathsf{#1}}}}', 1],
b: ['{\\boldsymbol{\\mathsf{#1}}}', 1]
} //END , macros:
}, //END , tex:
svg: {
fontCache: 'local'
}
}; //END MathJax =
var init_js_1 = require("@mathjax/src/components/cjs/startup/init.js");
var loader_js_1 = require("@mathjax/src/cjs/components/loader.js");
require("@mathjax/src/components/cjs/core/core.js");
require("@mathjax/src/components/cjs/input/tex/tex.js");
var svg_js_1 = require("@mathjax/src/components/cjs/output/svg/svg.js");
require('@mathjax/src/cjs/adaptors/browserAdaptor.js');
require('@mathjax/src/cjs/ui/menu/Menu.js');
//require('@mathjax/src4/build/xypic.js');
//require('@mathjax/src4/mathjax-img-main/browser/img.js');
loader_js_1.Loader.preLoaded('init', 'loader', 'startup', 'core', 'input/tex', 'output/svg' /*, 'xypic' 'img' */);
loader_js_1.Loader.saveVersion('tex-svg');
(0, svg_js_1.loadFont)(init_js_1.startup, true);
//this creates the stg
function _tex2svg(tex, callback, _errCallback) {
MathJax.tex2svgPromise(tex, {
display: false,
em: 16, // em-size in pixels
ex: 8, // ex-size in pixels
containerWidth: 80 * 16
}).then(function (node) {
callback(node);
}).catch(function (err) {
_errCallback(err.message);
});
}
////END code
global.tex2svg = _tex2svg;
which was your code from one of you earlier messages, but with the correct loader.js file being used, and the added global definition at the bottom. I commented out the xypic and img packages, since I didn't take time to set them up. I assume you can handle that yourself. I also added the loader paths for mathjax and mathjax-newcm.
Then I did
pnpm browserify -g ./transform.js main.js -o tex2svg.js
to bundle the code.
Next I created a test HTML file called main.html:
<!DOCTYPE html>
<html>
<head>
<title>Test of browserify</title>
<script src="tex2svg.js"></script>
<script>
function typeset() {
const tex = document.querySelector("#tex").value;
const output = document.querySelector("#output");
tex2svg(tex, (html) => {
output.innerHTML = "";
output.appendChild(html);
});
}
</script>
</head>
<body>
<textarea id="tex" rows="10" cols="60"></textarea><br>
<input type="button" value="Typeset" onclick="typeset()">
<br><br>
<div id="output"></div>
</body>
</html>
Now you should be able to load this file in a browser, type some math in the text area and press the "Typeset" button to get the output. You should be able to use \boldsymbol, \mathsf, etc, with no problem.
To run this on a server, move tex2svg.js and main.html to an appropriate location on the web server, and copy node_modules/@mathjax/src/bundle to a folder named mathjax in the same directory as main.html, and node_modules/@mathjax/mathjax-newcm-font to mathjax-newcm in the same directory as main.html. Then open main.html (from the server) in your browser.
First,
pnpm install @mathjax/mathjax-tex-font
and then edit the transform.js file to change the line for #default-font to be
.replace(/require\("#default-font/g, 'require("@mathjax/mathjax-tex-font/cjs');
You are correct that this will not need dynamic loading, but the original setup that I had does the dynamic loading properly, so perhaps you can use that after all. Note that mathjax-newcm font has much greater character coverage than mathjax-tex, so if you switch, you may have to be more careful about what symbols you use.
_________________________
At this point, I have done as much free support as I can offer you. I have provided a working example based on your code that you should be able to extend to your situation. If you need additional support, we can discuss privately the option of contracting for that, but I have spent more time on this than I should have, and will not be able to provide additional time for free.
Davide