# mathjax/MathJax-demos-node

Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
am2chtml Sep 9, 2019
am2chtml-page Sep 9, 2019
am2mml Sep 9, 2019
mml2chtml Sep 9, 2019
mml2chtml-page Sep 9, 2019
mml2svg Sep 9, 2019
mml2svg-page Sep 9, 2019
tex2chtml Sep 9, 2019
tex2chtml-page Sep 9, 2019
tex2mml Sep 9, 2019
tex2svg
tex2svg-page Sep 9, 2019

The examples in this directory illustrate how to use MathJax components in your node projects by preloading the ones you need, and letting MathJax know which you have loaded. That avoids the asynchronous startup process that MathJax usually uses to load the components (at the cost of your having to know which ones to load yourself, including any dependencies). To operate completely synchronously, you would want to disable the autoload and require extensions by not including them in the packages array for the TeX input jax, or load the all-packages component so that there is no need to dynamically load extensions later. Alternatively, you could use the promise-based typesetting and conversion functions and still be able to handle autoloading even though you have preloaded some components by hand.

All the examples in this directory consist of four main parts:

1. Processing the command-line arguments (not described here),
3. Running the startup module, and
4. Performing the desired conversion.

These parts are described below using the tex2chtml command as an example. The configuration and loading of components is accomplished via

MathJax = {
tex: {packages: argv.packages.replace('\*', PACKAGES).split(/\s*,\s*/)},
chtml: {fontURL: argv.fontURL},
startup: {typeset: false}
};

//
//  Load all the needed components
//
require('mathjax-full/components/src/startup/lib/startup.js');
require('mathjax-full/components/src/core/core.js');
require('mathjax-full/components/src/input/tex-base/tex-base.js');
require('mathjax-full/components/src/input/tex/extensions/all-packages/all-packages.js');
require('mathjax-full/components/src/output/chtml/chtml.js');
require('mathjax-full/components/src/output/chtml/fonts/tex/tex.js');
require('mathjax-full/components/src/startup/startup.js');

//
//  Let MathJax know these are loaded
//
'core',
'input/tex-base',
'[tex]/all-packages',
'output/chtml',
'output/chtml/fonts/tex'
);


The MathJax object simply configures the tex, chtml, and startup modules, and then the various components that we want to use are loaded. The first require() statement is important in that it initializes the startup module so that the other modules can properly hook into it. The core module should be loaded next, as it is needed by all the other modules. The adaptors/liteDOM is needed since we don't have a browser DOM to work with. We load tex-base rather than tex, since all-packages includes the other packages and there is no need to load duplicate code. We load the chtml output component, and the chtml/fonts/tex component so that we have the font data as well. Finally, we load the startup component (the first load was just its library).

Once the files are loaded and MathJax is told about them, we tell the startup module to initialize itself:

MathJax.config.startup.ready();


This creates the needed input and output jax, the DOM adaptor, and the internal MathDocument. These are stored in the MathJax.startup object where we can retrieve them, if needed, as we do with the adaptor, here.

Finally, we do the conversion:

const node = MathJax.tex2chtml(argv._[0] || '', {
display: !argv.inline,
em: argv.em,
ex: argv.ex,
containerWidth: argv.width
});

if (argv.css) {

This uses the MathJax.tex2chtml() function created for us by the startup module, which takes the command-line argument (the TeX math string) and the data about font metrics and returns an HTML node (as represented by the liteDOM). We use the adaptor to extract either the serialized version of the node, or the CSS styles required by the expression and print them to the terminal.