mathjax/MathJax-demos-node

How to use mathjax tex2svg-page without separate components?

brunobmello25 opened this issue · 1 comments

Looking at this repository it mentions how to use mathjax separate components to customize the way I want, but it doesn't mention what package I need to import if I want to use just the basics of tex2svg-page.

Additionally, it doesn't specify how can I setup a custom inline math selector, like the way we do on the client side:

window.MathJax = {
              tex: {
                inlineMath: [
                  [
                    '$', '$'
                  ]
                ],
            };
dpvc commented

The code for all the examples are available in this repository. But since there are several different ways to include MathJax, depending on your needs, the examples are broken down by the mechanism used. You probably want to component-based examples, such as tex2svg-page. You may not need all the command-line processing, so some simplification is possible. See the main README for the high-level description of how all the examples in the components directory work.

The key lines are

//
// Load the MathJax startup module
//
require('mathjax-full/' + (argv.dist ? 'es5' : 'components/src/tex-svg') + '/tex-svg.js');
//
// Wait for MathJax to start up, and then typeset the math
//
MathJax.startup.promise.then(() => {
const adaptor = MathJax.startup.adaptor;
const html = MathJax.startup.document;
if (Array.from(html.math).length === 0) {
adaptor.remove(html.outputJax.svgStyles);
const cache = adaptor.elementById(adaptor.body(html.document), 'MJX-SVG-global-cache');
if (cache) adaptor.remove(cache);
}
console.log(adaptor.doctype(html.document));
console.log(adaptor.outerHTML(adaptor.root(html.document)));
}).catch(err => console.log(err));

Line 95 could be simplified to

require('mathjax-full/es5/tex-svg.js');

and

if (Array.from(html.math).length === 0) {
adaptor.remove(html.outputJax.svgStyles);
const cache = adaptor.elementById(adaptor.body(html.document), 'MJX-SVG-global-cache');
if (cache) adaptor.remove(cache);
}

are just to handle case where the page has no math in it (so that the unneeded CSS styles aren't included), so you might be able to remove those as well. What's left, together with the configuration, is the code you need to have MathJax process a page for you. Be sure to read about the configuration in the README linked above.