Skip to content

Commit 0794300

Browse files
authored
Merge pull request #69 from mathjax/v4-update
V4 update
2 parents d9ba8c6 + 0c47b21 commit 0794300

File tree

200 files changed

+12053
-8391
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

200 files changed

+12053
-8391
lines changed

README.md

Lines changed: 113 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,138 @@
11
# [MathJax-demos-node](https://github.com/mathjax/MathJax-demos-node)
2-
<img class="shield" alt="GitHub release version" src="https://img.shields.io/github/v/release/mathjax/MathJax-src.svg?sort=semver">
3-
4-
A repository with examples using [MathJax version 3](https://github.com/mathjax/MathJax-src) in node applications.
5-
6-
See the [MathJax Web Demos](https://github.com/mathjax/MathJax-demos-web) for examples of how to use MathJax in web pages. See the [MathJax documentation](https://docs.mathjax.org/) for complete details of how to use MathJax in web browsers and node.
2+
<img class="shield" alt="GitHub release version" src="https://img.shields.io/github/v/release/mathjax/MathJax-demos-node.svg?sort=semver">
3+
4+
A repository with examples using [MathJax version 4](https://github.com/mathjax/MathJax-src) in node applications.
5+
6+
See the [MathJax Web
7+
Demos](https://github.com/mathjax/MathJax-demos-web) for examples of
8+
how to use MathJax in web pages. See the [MathJax
9+
documentation](https://docs.mathjax.org/) for complete details of how
10+
to use MathJax in web browsers and node. In particular, the [Examples
11+
in Node](https://docs.mathjax.org/en/latest/server/examples.html) page
12+
contains a list of the most important examples for node applications;
13+
the [Examples in a
14+
Browser](https://docs.mathjax.org/en/latest/web/examples.html) page
15+
lists other examples that may be useful; though they target web pages,
16+
the same techniques can be employed in node.
717

818
## The Example files
919

10-
This repository contains examples of how to use MathJax v3 in your node projects. These are divided into three main categories: ones based on the MathJax v3 components and its loading system, ones based on the MathJax v3 components that you preload by hand (for easier synchronous use), and ones that use the base MathJax files directly without using the MathJax components.
11-
12-
All three groups include examples of conversion from the various input formats to the various output formats, both for individual expressions, and for complete pages containing math. Each category includes the following executables:
20+
This repository contains a number of examples of command-line tools
21+
that exhibit the features of MathJax. The tools provide a number of
22+
command-line options, which you can discover by using the `--help`
23+
option when running them. Most of the examples include the ability to
24+
generate speech strings for the output, for example, via the
25+
`--speech` or ``--braille` options, and the language can be specified
26+
using `--sre locale de`, for example to get a German localization.
27+
28+
The tools accept one or more mathematical expressions as their
29+
arguments, or you can use the `--file` option to specify an HTML file
30+
that will be processed in full. If you don't give a file name, then
31+
the file is taken from standard input. Similarly, if you don't give
32+
any expressions, then they are taken from standard in, with a blank
33+
line separating expressions.
34+
35+
The tools come in two forms, one using ESM modules in the [`mjs`](mjs)
36+
directory, and one using CommonJS modules in the [`cjs`](cjs) folder,
37+
so you can see examples of both types. These directories are further
38+
subdivided into groups based on the mechanism by which MathJax is
39+
used: [`simple`](mjs/simple) uses a node-only mechanism that makes it
40+
easy to start experimenting with MathJax, but can't be used in web
41+
applications; [`component`](mjs/component) uses the MathJax Component
42+
framework to access MathJax's components via a configuration object
43+
like the one used in web browsers; [`direct`](mjs/direct) uses direct
44+
calls to the MathJax modules, without using MathJax components (so
45+
dynamic loading of TeX extensions is not supported for example); and
46+
finally, [`mixed`](mjs/mixed) combines direct calls with MathJax
47+
Components for the best of both worlds.
48+
49+
All four groups include examples of conversion from the various input
50+
formats to the various output formats, and include the following
51+
executables:
1352

1453
* `tex2chtml`
1554
* `tex2svg`
1655
* `tex2mml`
1756
* `mml2chtml`
57+
* `mml2mml`
1858
* `mml2svg`
1959
* `am2chtml`
2060
* `am2mml`
21-
* `tex2chtml-page`
22-
* `tex2svg-page`
23-
* `tex2mml-page`
24-
* `mml2chtml-page`
25-
* `mml2svg-page`
26-
* `am2chtml-page`
27-
28-
The first seven convert an expression from either a TeX, MathML, or AsciiMath string (given as its first argument) to a CommonHTML, SVG, or MathML string. The ones ending in `-page` take an HTML file and convert the math in it from the given input format to the specified output format and output the modified page.
29-
30-
Use the `--help` option to get a list of all the possible options for each command.
31-
32-
The three categories of commands are stored in the directories called [`component`](component), [`simple`](simple), [`preload`](preload), and [`direct`](direct). Both the `component` and `simple` directories are examples of the component-based approach, with the `component` directory containing versions that mirror the approach needed when MathJax is used in web pages, and the `simple` directory containing examples that take advantage of some simplifications available in node. Each of those directories contain additional information about the example files they contain.
33-
34-
There is also a directory [`speech`](speech) that give examples of converters that add speech strings to their results, which illustrate more sophisticated operations in MathJax. This is described in more detail in that directory.
35-
36-
The [`custom-tex-extension`](custom-tex-extension) directory contains an example of how to create your own custom TeX extension and load it as a component. Again, see the directory for more details.
37-
38-
The [`puppeteer`](puppeteer) directory contains an example of how to use the [Puppeteer](https://developers.google.com/web/tools/puppeteer) library to use a headless Chrome instance to do server-side conversion of mathematics using MathJax. This is useful in situations where you are using characters that are not included in the main MathJax fonts, as Chrome will be able to measure the widths of these characters so that they can be displayed more reliably.
61+
* `am2svg`
62+
* `typeset`
63+
64+
The first nine convert from the format listed before the `2` to the
65+
format listed after it, and represent all combinations of the three
66+
input formats with three output formats. The `mml2mml` example is
67+
useful for reformatting a MathML string, or for converting it to use
68+
the Mathematical Alphanumerics block of Unicode characters rather than
69+
`mathvariant` attributes with ASCII characters, as required by
70+
MathML-Core, for instance.
71+
72+
The `typeset` tool allows you to convert from any input format to any
73+
output format, so is a "swiss army knife" example that gives you
74+
access to all the options within a common command.
75+
76+
Use the `--help` option to get a list of all the possible options for
77+
each tool.
78+
79+
The [`custom-tex-extension`](custom-tex-extension) directory contains
80+
an example of how to create your own custom TeX extension and load it
81+
as a component. See that directory for more details.
82+
83+
The [`puppeteer`](puppeteer) directory contains an example of how to
84+
call the
85+
[Puppeteer](https://developers.google.com/web/tools/puppeteer) library
86+
to use a headless Chrome instance to do server-side conversion of
87+
mathematics using MathJax. This is useful in situations where you are
88+
using characters that are not included in the main MathJax fonts, or
89+
including HTML within TeX or MathML expressions, as Chrome will be
90+
able to measure the sizes of these so that they can be displayed more
91+
reliably.
92+
93+
Finally, the [`jsdom`](jsdom) and [`linkedom`](linkedom) directories
94+
include examples of how to use those DOM libraries instead of
95+
MathJax's usual `LiteDOM` library as a replacement for the browser
96+
DOM.
97+
98+
## The Utility Libraries
99+
100+
The tools in the `mjs` and `cjs` directories share a lot of common
101+
code, and that shared code has been moved to separate utility files in
102+
the [`util`](mjs/util) sub-directories of those directories. There is
103+
one for each of the input and output formats (the `Mml.js` file
104+
contains the code for both input and output in MathML format), and a
105+
main `Util.js` file that holds code common to all the tools. The
106+
`Direct.js` and `Mixed.js` utilities have code specific to those
107+
mechanisms of accessing MathJax, and the `Typeset.js` file handles the
108+
special needs of the `typeset` tool.
109+
110+
These utilities form the heart of the command-line tools, and it is in
111+
them that you will see the real details of how to work with MathJax;
112+
the individual tool files basically just load the needed libraries or
113+
MathJax modules, set up the configuration needed, and call the utility
114+
libraries to handle the details or getting the command-line arguments,
115+
setting up the needed configuration options, converting the expression
116+
or files, filtering the results according to the options given, and
117+
printing the results.
39118

40119
## Installation
41120

42-
In order to try out these examples, clone this repository, enter the directory, and install the dependencies:
121+
In order to try out these examples, clone this repository, enter the
122+
directory, and install the dependencies:
43123

44124
``` bash
45125
git clone https://github.com/mathjax/MathJax-demos-node.git MathJax-demos-node
46126
cd MathJax-demos-node
47127
npm install
48128
```
49129

50-
The examples should be executable files that you can run. On non-unix systems, you may need to call
130+
To run the tools, cd to the directory containing the tools and use
51131

52-
``` bash
53-
node -r esm <example-name>
132+
```
133+
node <example-name> [options] [expressions...]
54134
```
55135

56-
where `<example-name>` is the name of the example file. Some examples take an argument (like a TeX string) that follows the `<example-name>` above.
136+
where `<example-name>` is the name of the example file, like
137+
`tex2svg`. Use the `--help` options to get more details about the
138+
options and arguments that each tool needs.

cjs/component/am2chtml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#! node
2+
3+
/*************************************************************************
4+
*
5+
* cjs/component/am2chtml
6+
*
7+
* Uses MathJax v4 to convert an AsciiMath string to an HTML string.
8+
*
9+
* ----------------------------------------------------------------------
10+
*
11+
* Copyright (c) 2019-2025 The MathJax Consortium
12+
*
13+
* Licensed under the Apache License, Version 2.0 (the "License");
14+
* you may not use this file except in compliance with the License.
15+
* You may obtain a copy of the License at
16+
*
17+
* http://www.apache.org/licenses/LICENSE-2.0
18+
*
19+
* Unless required by applicable law or agreed to in writing, software
20+
* distributed under the License is distributed on an "AS IS" BASIS,
21+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22+
* See the License for the specific language governing permissions and
23+
* limitations under the License.
24+
*/
25+
26+
//
27+
// Load the needed utilities.
28+
//
29+
const {Util} = require('../util/Util.js');
30+
const {Am} = require('../util/Am.js');
31+
const {Chtml} = require('../util/Chtml.js');
32+
const {Sre} = require('../util/Sre.js');
33+
34+
//
35+
// Get the command-line arguments.
36+
//
37+
const args = Util.args('"math" > file.html', [Am, Chtml, Sre]);
38+
39+
//
40+
// Set up the MathJax configuration.
41+
//
42+
global.MathJax = {
43+
loader: Util.config.loader(['adaptors/liteDOM', 'input/asciimath', 'output/chtml', 'a11y/speech'], args),
44+
options: Util.config.options(args),
45+
startup: Util.config.startup(args),
46+
asciimath: Am.config(args),
47+
chtml: Chtml.config(args),
48+
'adaptors/liteDOM': Util.config.adaptor(args),
49+
};
50+
51+
//
52+
// Load the needed components.
53+
//
54+
Util.load('startup', args);
55+
56+
//
57+
// Typeset the math and print the result.
58+
// Then tell MathJax we are done.
59+
//
60+
Util.typeset(args, Chtml)
61+
.then((output) => console.log(output))
62+
.then(() => MathJax.done());

cjs/component/am2mml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#! node
2+
3+
/*************************************************************************
4+
*
5+
* cjs/component/am2mml
6+
*
7+
* Uses MathJax v4 to convert an AsciiMath string to a MathML string.
8+
*
9+
* ----------------------------------------------------------------------
10+
*
11+
* Copyright (c) 2019-2025 The MathJax Consortium
12+
*
13+
* Licensed under the Apache License, Version 2.0 (the "License");
14+
* you may not use this file except in compliance with the License.
15+
* You may obtain a copy of the License at
16+
*
17+
* http://www.apache.org/licenses/LICENSE-2.0
18+
*
19+
* Unless required by applicable law or agreed to in writing, software
20+
* distributed under the License is distributed on an "AS IS" BASIS,
21+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22+
* See the License for the specific language governing permissions and
23+
* limitations under the License.
24+
*/
25+
26+
//
27+
// Load the needed utilities.
28+
//
29+
const {Util} = require('../util/Util.js');
30+
const {Am} = require('../util/Am.js');
31+
const {Mml} = require('../util/Mml.js');
32+
33+
//
34+
// Get the command-line arguments.
35+
//
36+
const args = Util.args('"math"', [Am, Mml.output], false);
37+
38+
//
39+
// Set up the MathJax configuration.
40+
//
41+
global.MathJax = {
42+
loader: Util.config.loader(['adaptors/liteDOM', 'input/asciimath'], args),
43+
options: Util.config.options(args),
44+
startup: Util.config.startup(args),
45+
asciimath: Am.config(args),
46+
'adaptors/liteDOM': Util.config.adaptor(args),
47+
};
48+
49+
//
50+
// Load the needed components.
51+
//
52+
Util.load('startup', args);
53+
54+
//
55+
// Convert and print the math.
56+
//
57+
Util.typeset(args, Mml)
58+
.then((output) => console.log(output));

cjs/component/am2svg

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#! node
2+
3+
/*************************************************************************
4+
*
5+
* cjs/component/am2svg
6+
*
7+
* Uses MathJax v4 to convert an AsciiMath string to an SVG string.
8+
*
9+
* ----------------------------------------------------------------------
10+
*
11+
* Copyright (c) 2019-2025 The MathJax Consortium
12+
*
13+
* Licensed under the Apache License, Version 2.0 (the "License");
14+
* you may not use this file except in compliance with the License.
15+
* You may obtain a copy of the License at
16+
*
17+
* http://www.apache.org/licenses/LICENSE-2.0
18+
*
19+
* Unless required by applicable law or agreed to in writing, software
20+
* distributed under the License is distributed on an "AS IS" BASIS,
21+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22+
* See the License for the specific language governing permissions and
23+
* limitations under the License.
24+
*/
25+
26+
//
27+
// Load the needed utilities.
28+
//
29+
const {Util} = require('../util/Util.js');
30+
const {Am} = require('../util/Am.js');
31+
const {Svg} = require('../util/Svg.js');
32+
const {Sre} = require('../util/Sre.js');
33+
34+
//
35+
// Get the command-line arguments.
36+
//
37+
const args = Util.args('"math" > file.svg', [Am, Svg, Sre]);
38+
39+
//
40+
// Set up the MathJax configuration.
41+
//
42+
global.MathJax = {
43+
loader: Util.config.loader(['adaptors/liteDOM', 'input/asciimath', 'output/svg', 'a11y/speech'], args),
44+
options: Util.config.options(args),
45+
startup: Util.config.startup(args),
46+
asciimath: Am.config(args),
47+
svg: Svg.config(args),
48+
'adaptors/liteDOM': Util.config.adaptor(args),
49+
};
50+
51+
//
52+
// Load the needed components.
53+
//
54+
Util.load('startup', args);
55+
56+
//
57+
// Typeset the math and print the result.
58+
// Then tell MathJax we are done.
59+
//
60+
Util.typeset(args, Svg)
61+
.then((output) => console.log(output))
62+
.then(() => MathJax.done());

0 commit comments

Comments
 (0)