├── .gitignore ├── LICENSE ├── README.md ├── component ├── README.md ├── am2chtml ├── am2chtml-page ├── am2mml ├── mml2chtml ├── mml2chtml-page ├── mml2svg ├── mml2svg-page ├── tex2chtml ├── tex2chtml-page ├── tex2mml ├── tex2mml-page ├── tex2svg └── tex2svg-page ├── custom-tex-extension ├── README.md ├── mml.js └── tex2mml ├── direct ├── README.md ├── am2chtml ├── am2chtml-page ├── am2mml ├── mml2chtml ├── mml2chtml-page ├── mml2svg ├── mml2svg-page ├── tex2chtml ├── tex2chtml-page ├── tex2mml ├── tex2mml-page ├── tex2svg └── tex2svg-page ├── jsdom ├── README.md ├── adaptor │ ├── adaptor.js │ ├── adaptor.min.js │ └── webpack.config.js ├── package.json ├── tex2chtml ├── tex2chtml-page ├── tex2svg └── tex2svg-page ├── package.json ├── preload ├── README.md ├── am2chtml ├── am2chtml-page ├── am2mml ├── mml2chtml ├── mml2chtml-page ├── mml2svg ├── mml2svg-page ├── tex2chtml ├── tex2chtml-page ├── tex2mml ├── tex2mml-page ├── tex2svg └── tex2svg-page ├── puppeteer ├── README.md ├── package.json ├── puppeteer.html └── tex2svg ├── simple ├── README.md ├── am2chtml ├── am2chtml-page ├── am2mml ├── mml2chtml ├── mml2chtml-page ├── mml2svg ├── mml2svg-page ├── tex2chtml ├── tex2chtml-page ├── tex2mml ├── tex2mml-page ├── tex2svg └── tex2svg-page └── speech ├── README.md ├── action.js ├── mml2mml ├── mml2svg ├── mml2svg-page ├── tex2chtml ├── tex2chtml-speech ├── tex2chtml-speech-page ├── tex2mml ├── tex2speech ├── tex2svg └── tex2svg-page /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | node_modules 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | GitHub release version 2 | 3 | A repository with examples using [MathJax version 3](https://github.com/mathjax/MathJax-src) in node applications. 4 | 5 | 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. 6 | 7 | ## The Example files 8 | 9 | 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. 10 | 11 | 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: 12 | 13 | * `tex2chtml` 14 | * `tex2svg` 15 | * `tex2mml` 16 | * `mml2chtml` 17 | * `mml2svg` 18 | * `am2chtml` 19 | * `am2mml` 20 | * `tex2chtml-page` 21 | * `tex2svg-page` 22 | * `tex2mml-page` 23 | * `mml2chtml-page` 24 | * `mml2svg-page` 25 | * `am2chtml-page` 26 | 27 | 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. 28 | 29 | Use the `--help` option to get a list of all the possible options for each command. 30 | 31 | 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. 32 | 33 | 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. 34 | 35 | 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. 36 | 37 | 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. 38 | 39 | ## Installation 40 | 41 | In order to try out these examples, clone this repository, enter the directory, and install the dependencies: 42 | 43 | The examples should be executable files that you can run. On non-unix systems, you may need to call 44 | 45 | ``` bash 46 | node -r esm 47 | ``` 48 | 49 | where `` is the name of the example file. Some examples take an argument (like a TeX string) that follows the `` above. 50 | -------------------------------------------------------------------------------- /component/am2chtml: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * component/am2chtml 6 | * 7 | * Uses MathJax v3 to convert an AsciiMath string to an HTML string. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // 28 | // Get the command-line arguments 29 | // 30 | var argv = require('yargs') 31 | .demand(0).strict() 32 | .usage('$0 [options] "math" > file.html') 33 | .options({ 34 | em: { 35 | default: 16, 36 | describe: 'em-size in pixels' 37 | }, 38 | ex: { 39 | default: 8, 40 | describe: 'ex-size in pixels' 41 | }, 42 | width: { 43 | default: 80 * 16, 44 | describe: 'width of container in pixels' 45 | }, 46 | css: { 47 | boolean: true, 48 | describe: 'output the required CSS rather than the HTML itself' 49 | }, 50 | fontURL: { 51 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 52 | describe: 'the URL to use for web fonts' 53 | }, 54 | assistiveMml: { 55 | boolean: true, 56 | default: false, 57 | describe: 'whether to include assistive MathML output' 58 | }, 59 | dist: { 60 | boolean: true, 61 | default: false, 62 | describe: 'true to use webpacked version, false to use MathJax source files' 63 | } 64 | }) 65 | .argv; 66 | 67 | // 68 | // Configure MathJax 69 | // 70 | MathJax = { 71 | options: { 72 | enableAssistiveMml: argv.assistiveMml 73 | }, 74 | loader: { 75 | paths: {mathjax: 'mathjax-full/es5'}, 76 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 77 | require: require, 78 | load: ['input/asciimath', 'output/chtml', 'adaptors/liteDOM', 'a11y/assistive-mml'] 79 | }, 80 | chtml: { 81 | fontURL: argv.fontURL 82 | }, 83 | startup: { 84 | typeset: false 85 | } 86 | } 87 | 88 | // 89 | // Load the MathJax startup module 90 | // 91 | require('mathjax-full/' + (argv.dist ? 'es5' : 'components/src/startup') + '/startup.js'); 92 | 93 | // 94 | // Wait for MathJax to start up, and then typeset the math 95 | // 96 | MathJax.startup.promise.then(() => { 97 | MathJax.asciimath2chtmlPromise(argv._[0] || '', { 98 | display: !argv.inline, 99 | em: argv.em, 100 | ex: argv.ex, 101 | containerWidth: argv.width 102 | }).then((node) => { 103 | const adaptor = MathJax.startup.adaptor; 104 | // 105 | // If the --css option was specified, output the CSS, 106 | // Otherwise, output the typeset math as HTML 107 | // 108 | if (argv.css) { 109 | console.log(adaptor.textContent(MathJax.chtmlStylesheet())); 110 | } else { 111 | console.log(adaptor.outerHTML(node)); 112 | }; 113 | }); 114 | }).catch(err => console.log(err)); 115 | -------------------------------------------------------------------------------- /component/am2chtml-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * component/am2chtml-page 6 | * 7 | * Uses MathJax v3 to convert all AsciiMath in an HTML document. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // Get the command-line arguments 28 | // 29 | var argv = require('yargs') 30 | .demand(0).strict() 31 | .usage('$0 [options] file.html > converted.html') 32 | .options({ 33 | em: { 34 | default: 16, 35 | describe: 'em-size in pixels' 36 | }, 37 | ex: { 38 | default: 8, 39 | describe: 'ex-size in pixels' 40 | }, 41 | fontURL: { 42 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 43 | describe: 'the URL to use for web fonts' 44 | }, 45 | dist: { 46 | boolean: true, 47 | default: false, 48 | describe: 'true to use webpacked version, false to use MathJax source files' 49 | } 50 | }) 51 | .argv; 52 | 53 | // 54 | // Read the HTML file 55 | // 56 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 57 | 58 | // 59 | // Configure MathJax 60 | // 61 | MathJax = { 62 | loader: { 63 | paths: {mathjax: 'mathjax-full/es5'}, 64 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 65 | require: require, 66 | load: ['input/asciimath', 'output/chtml', 'adaptors/liteDOM', 'a11y/assistive-mml'] 67 | }, 68 | chtml: { 69 | fontURL: argv.fontURL, 70 | exFactor: argv.ex / argv.em 71 | }, 72 | 'adaptors/liteDOM': { 73 | fontSize: argv.em 74 | }, 75 | startup: { 76 | document: htmlfile 77 | } 78 | } 79 | 80 | // 81 | // Load the MathJax startup module 82 | // 83 | require('mathjax-full/' + (argv.dist ? 'es5' : 'components/src/startup') + '/startup.js'); 84 | 85 | // 86 | // Wait for MathJax to start up, and then typeset the math 87 | // 88 | MathJax.startup.promise.then(() => { 89 | const adaptor = MathJax.startup.adaptor; 90 | const html = MathJax.startup.document; 91 | if (Array.from(html.math).length === 0) adaptor.remove(html.outputJax.chtmlStyles); 92 | console.log(adaptor.doctype(html.document)); 93 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 94 | }).catch(err => console.log(err)); 95 | -------------------------------------------------------------------------------- /component/am2mml: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * component/am2mml 6 | * 7 | * Uses MathJax v3 to convert an AsciiMath string to a MathML string. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // 28 | // Get the command-line arguments 29 | // 30 | var argv = require('yargs') 31 | .demand(0).strict() 32 | .usage('$0 [options] "math"') 33 | .options({ 34 | em: { 35 | default: 16, 36 | describe: 'em-size in pixels' 37 | }, 38 | ex: { 39 | default: 8, 40 | describe: 'ex-size in pixels' 41 | }, 42 | width: { 43 | default: 80 * 16, 44 | describe: 'width of container in pixels' 45 | }, 46 | dist: { 47 | boolean: true, 48 | default: false, 49 | describe: 'true to use webpacked version, false to use MathJax source files' 50 | } 51 | }) 52 | .argv; 53 | 54 | // 55 | // Configure MathJax 56 | // 57 | MathJax = { 58 | loader: { 59 | paths: {mathjax: 'mathjax-full/es5'}, 60 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 61 | require: require, 62 | load: ['input/asciimath', 'adaptors/liteDOM'] 63 | } 64 | } 65 | 66 | // 67 | // Load the MathJax startup module 68 | // 69 | require('mathjax-full/' + (argv.dist ? 'es5' : 'components/src/startup') + '/startup.js'); 70 | 71 | // 72 | // Wait for MathJax to start up, and then typeset the math 73 | // 74 | MathJax.startup.promise.then(() => { 75 | MathJax.asciimath2mmlPromise(argv._[0] || '', { 76 | display: !argv.inline, 77 | em: argv.em, 78 | ex: argv.ex, 79 | containerWidth: argv.width 80 | }).then(mml => console.log(mml)); 81 | }).catch(err => console.log(err)); 82 | -------------------------------------------------------------------------------- /component/mml2chtml: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * component/mml2chtml 6 | * 7 | * Uses MathJax v3 to convert a MathML string to an HTML string. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // 28 | // Get the command-line arguments 29 | // 30 | var argv = require('yargs') 31 | .demand(0).strict() 32 | .usage('$0 [options] "math" > file.html') 33 | .options({ 34 | inline: { 35 | boolean: true, 36 | describe: "process as inline math" 37 | }, 38 | em: { 39 | default: 16, 40 | describe: 'em-size in pixels' 41 | }, 42 | ex: { 43 | default: 8, 44 | describe: 'ex-size in pixels' 45 | }, 46 | width: { 47 | default: 80 * 16, 48 | describe: 'width of container in pixels' 49 | }, 50 | css: { 51 | boolean: true, 52 | describe: 'output the required CSS rather than the HTML itself' 53 | }, 54 | fontURL: { 55 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 56 | describe: 'the URL to use for web fonts' 57 | }, 58 | assistiveMml: { 59 | boolean: true, 60 | default: false, 61 | describe: 'whether to include assistive MathML output' 62 | }, 63 | dist: { 64 | boolean: true, 65 | default: false, 66 | describe: 'true to use webpacked version, false to use MathJax source files' 67 | } 68 | }) 69 | .argv; 70 | 71 | // 72 | // Configure MathJax 73 | // 74 | MathJax = { 75 | options: { 76 | enableAssistiveMml: argv.assistiveMml 77 | }, 78 | loader: { 79 | paths: {mathjax: 'mathjax-full/es5'}, 80 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 81 | require: require, 82 | load: ['adaptors/liteDOM', 'input/mml/entities'] 83 | }, 84 | chtml: { 85 | fontURL: argv.fontURL 86 | }, 87 | startup: { 88 | typeset: false 89 | } 90 | } 91 | 92 | // 93 | // Load the MathJax startup module 94 | // 95 | require('mathjax-full/' + (argv.dist ? 'es5' : 'components/src/mml-chtml') + '/mml-chtml.js'); 96 | 97 | // 98 | // Wait for MathJax to start up, and then typeset the math 99 | // 100 | MathJax.startup.promise.then(() => { 101 | MathJax.mathml2chtmlPromise(argv._[0] || '', { 102 | display: !argv.inline, 103 | em: argv.em, 104 | ex: argv.ex, 105 | containerWidth: argv.width 106 | }).then((node) => { 107 | const adaptor = MathJax.startup.adaptor; 108 | // 109 | // If the --css option was specified, output the CSS, 110 | // Otherwise, output the typeset math as HTML 111 | // 112 | if (argv.css) { 113 | console.log(adaptor.textContent(MathJax.chtmlStylesheet())); 114 | } else { 115 | console.log(adaptor.outerHTML(node)); 116 | }; 117 | }); 118 | }).catch(err => console.log(err)); 119 | -------------------------------------------------------------------------------- /component/mml2chtml-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * component/mml2chtml-page 6 | * 7 | * Uses MathJax v3 to convert all MathML in an HTML document. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // Get the command-line arguments 28 | // 29 | var argv = require('yargs') 30 | .demand(0).strict() 31 | .usage('$0 [options] file.html > converted.html') 32 | .options({ 33 | em: { 34 | default: 16, 35 | describe: 'em-size in pixels' 36 | }, 37 | ex: { 38 | default: 8, 39 | describe: 'ex-size in pixels' 40 | }, 41 | fontURL: { 42 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 43 | describe: 'the URL to use for web fonts' 44 | }, 45 | dist: { 46 | boolean: true, 47 | default: false, 48 | describe: 'true to use webpacked version, false to use MathJax source files' 49 | } 50 | }) 51 | .argv; 52 | 53 | // 54 | // Read the HTML file 55 | // 56 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 57 | 58 | // 59 | // Configure MathJax 60 | // 61 | MathJax = { 62 | loader: { 63 | paths: {mathjax: 'mathjax-full/es5'}, 64 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 65 | require: require, 66 | load: ['adaptors/liteDOM', 'input/mml/entities'] 67 | }, 68 | chtml: { 69 | fontURL: argv.fontURL, 70 | exFactor: argv.ex / argv.em 71 | }, 72 | 'adaptors/liteDOM': { 73 | fontSize: argv.em 74 | }, 75 | startup: { 76 | document: htmlfile 77 | } 78 | } 79 | 80 | // 81 | // Load the MathJax startup module 82 | // 83 | require('mathjax-full/' + (argv.dist ? 'es5' : 'components/src/mml-chtml') + '/mml-chtml.js'); 84 | 85 | // 86 | // Wait for MathJax to start up, and then typeset the math 87 | // 88 | MathJax.startup.promise.then(() => { 89 | const adaptor = MathJax.startup.adaptor; 90 | const html = MathJax.startup.document; 91 | if (Array.from(html.math).length === 0) adaptor.remove(html.outputJax.chtmlStyles); 92 | console.log(adaptor.doctype(html.document)); 93 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 94 | }).catch(err => console.log(err)); 95 | -------------------------------------------------------------------------------- /component/mml2svg-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * component/mml2svg-page 6 | * 7 | * Uses MathJax v3 to convert all MathML in an HTML document. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // Get the command-line arguments 28 | // 29 | var argv = require('yargs') 30 | .demand(0).strict() 31 | .usage('$0 [options] file.html > converted.html') 32 | .options({ 33 | em: { 34 | default: 16, 35 | describe: 'em-size in pixels' 36 | }, 37 | ex: { 38 | default: 8, 39 | describe: 'ex-size in pixels' 40 | }, 41 | fontCache: { 42 | default: 'global', 43 | describe: 'cache type: local, global, none' 44 | }, 45 | dist: { 46 | boolean: true, 47 | default: false, 48 | describe: 'true to use webpacked version, false to use MathJax source files' 49 | } 50 | }) 51 | .argv; 52 | 53 | // 54 | // Read the HTML file 55 | // 56 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 57 | 58 | // 59 | // Configure MathJax 60 | // 61 | MathJax = { 62 | loader: { 63 | paths: {mathjax: 'mathjax-full/es5'}, 64 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 65 | require: require, 66 | load: ['adaptors/liteDOM', 'input/mml/entities'] 67 | }, 68 | svg: { 69 | fontCache: argv.fontCache, 70 | exFactor: argv.ex / argv.em 71 | }, 72 | 'adaptors/liteDOM': { 73 | fontSize: argv.em 74 | }, 75 | startup: { 76 | document: htmlfile 77 | } 78 | } 79 | 80 | // 81 | // Load the MathJax startup module 82 | // 83 | require('mathjax-full/' + (argv.dist ? 'es5' : 'components/src/mml-svg') + '/mml-svg.js'); 84 | 85 | // 86 | // Wait for MathJax to start up, and then typeset the math 87 | // 88 | MathJax.startup.promise.then(() => { 89 | const adaptor = MathJax.startup.adaptor; 90 | const html = MathJax.startup.document; 91 | if (Array.from(html.math).length === 0) { 92 | adaptor.remove(html.outputJax.svgStyles); 93 | const cache = adaptor.elementById(adaptor.body(html.document), 'MJX-SVG-global-cache'); 94 | if (cache) adaptor.remove(cache); 95 | } 96 | console.log(adaptor.doctype(html.document)); 97 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 98 | }).catch(err => console.log(err)); 99 | -------------------------------------------------------------------------------- /component/tex2chtml: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * component/tex2chtml 6 | * 7 | * Uses MathJax v3 to convert a TeX string to an HTML string. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // 28 | // The default TeX packages to use 29 | // 30 | const PACKAGES = 'base, autoload, require, ams, newcommand'; 31 | 32 | // 33 | // Get the command-line arguments 34 | // 35 | var argv = require('yargs') 36 | .demand(0).strict() 37 | .usage('$0 [options] "math" > file.html') 38 | .options({ 39 | inline: { 40 | boolean: true, 41 | describe: "process as inline math" 42 | }, 43 | em: { 44 | default: 16, 45 | describe: 'em-size in pixels' 46 | }, 47 | ex: { 48 | default: 8, 49 | describe: 'ex-size in pixels' 50 | }, 51 | width: { 52 | default: 80 * 16, 53 | describe: 'width of container in pixels' 54 | }, 55 | packages: { 56 | default: PACKAGES, 57 | describe: 'the packages to use, e.g. "base, ams"; use "*" to represent the default packages, e.g, "*, bbox"' 58 | }, 59 | css: { 60 | boolean: true, 61 | describe: 'output the required CSS rather than the HTML itself' 62 | }, 63 | fontURL: { 64 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 65 | describe: 'the URL to use for web fonts' 66 | }, 67 | assistiveMml: { 68 | boolean: true, 69 | default: false, 70 | describe: 'whether to include assistive MathML output' 71 | }, 72 | dist: { 73 | boolean: true, 74 | default: false, 75 | describe: 'true to use webpacked version, false to use MathJax source files' 76 | } 77 | }) 78 | .argv; 79 | 80 | // 81 | // Configure MathJax 82 | // 83 | MathJax = { 84 | options: { 85 | enableAssistiveMml: argv.assistiveMml 86 | }, 87 | loader: { 88 | paths: {mathjax: 'mathjax-full/es5'}, 89 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 90 | require: require, 91 | load: ['adaptors/liteDOM'] 92 | }, 93 | tex: { 94 | packages: argv.packages.replace('\*', PACKAGES).split(/\s*,\s*/) 95 | }, 96 | chtml: { 97 | fontURL: argv.fontURL 98 | }, 99 | startup: { 100 | typeset: false 101 | } 102 | } 103 | 104 | // 105 | // Load the MathJax startup module 106 | // 107 | require('mathjax-full/' + (argv.dist ? 'es5' : 'components/src/tex-chtml') + '/tex-chtml.js'); 108 | 109 | // 110 | // Wait for MathJax to start up, and then typeset the math 111 | // 112 | MathJax.startup.promise.then(() => { 113 | MathJax.tex2chtmlPromise(argv._[0] || '', { 114 | display: !argv.inline, 115 | em: argv.em, 116 | ex: argv.ex, 117 | containerWidth: argv.width 118 | }).then((node) => { 119 | const adaptor = MathJax.startup.adaptor; 120 | // 121 | // If the --css option was specified, output the CSS, 122 | // Otherwise, output the typeset math as HTML 123 | // 124 | if (argv.css) { 125 | console.log(adaptor.textContent(MathJax.chtmlStylesheet())); 126 | } else { 127 | console.log(adaptor.outerHTML(node)); 128 | }; 129 | }); 130 | }).catch(err => console.log(err)); 131 | -------------------------------------------------------------------------------- /component/tex2chtml-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * component/tex2chtml-page 6 | * 7 | * Uses MathJax v3 to convert all TeX in an HTML document. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // The default TeX packages to use 28 | // 29 | const PACKAGES = 'base, autoload, require, ams, newcommand'; 30 | 31 | // 32 | // Get the command-line arguments 33 | // 34 | var argv = require('yargs') 35 | .demand(0).strict() 36 | .usage('$0 [options] file.html > converted.html') 37 | .options({ 38 | em: { 39 | default: 16, 40 | describe: 'em-size in pixels' 41 | }, 42 | ex: { 43 | default: 8, 44 | describe: 'ex-size in pixels' 45 | }, 46 | packages: { 47 | default: PACKAGES, 48 | describe: 'the packages to use, e.g. "base, ams"; use "*" to represent the default packages, e.g, "*, bbox"' 49 | }, 50 | fontURL: { 51 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 52 | describe: 'the URL to use for web fonts' 53 | }, 54 | dist: { 55 | boolean: true, 56 | default: false, 57 | describe: 'true to use webpacked version, false to use MathJax source files' 58 | } 59 | }) 60 | .argv; 61 | 62 | // 63 | // Read the HTML file 64 | // 65 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 66 | 67 | // 68 | // Configure MathJax 69 | // 70 | MathJax = { 71 | loader: { 72 | paths: {mathjax: 'mathjax-full/es5'}, 73 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 74 | require: require, 75 | load: ['adaptors/liteDOM'] 76 | }, 77 | tex: { 78 | packages: argv.packages.replace('\*', PACKAGES).split(/\s*,\s*/) 79 | }, 80 | chtml: { 81 | fontURL: argv.fontURL, 82 | exFactor: argv.ex / argv.em 83 | }, 84 | 'adaptors/liteDOM': { 85 | fontSize: argv.em 86 | }, 87 | startup: { 88 | document: htmlfile 89 | } 90 | } 91 | 92 | // 93 | // Load the MathJax startup module 94 | // 95 | require('mathjax-full/' + (argv.dist ? 'es5' : 'components/src/tex-chtml') + '/tex-chtml.js'); 96 | 97 | // 98 | // Wait for MathJax to start up, and then typeset the math 99 | // 100 | MathJax.startup.promise.then(() => { 101 | const adaptor = MathJax.startup.adaptor; 102 | const html = MathJax.startup.document; 103 | if (Array.from(html.math).length === 0) adaptor.remove(html.outputJax.chtmlStyles); 104 | console.log(adaptor.doctype(html.document)); 105 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 106 | }).catch(err => console.log(err)); 107 | -------------------------------------------------------------------------------- /component/tex2mml: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * component/tex2mml 6 | * 7 | * Uses MathJax v3 to convert a TeX string to a MathML string. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // 28 | // The default TeX packages to use 29 | // 30 | const PACKAGES = 'base, autoload, ams, newcommand, require'; 31 | 32 | // 33 | // Get the command-line arguments 34 | // 35 | var argv = require('yargs') 36 | .demand(0).strict() 37 | .usage('$0 [options] "math"') 38 | .options({ 39 | inline: { 40 | boolean: true, 41 | describe: "process as inline math" 42 | }, 43 | em: { 44 | default: 16, 45 | describe: 'em-size in pixels' 46 | }, 47 | ex: { 48 | default: 8, 49 | describe: 'ex-size in pixels' 50 | }, 51 | width: { 52 | default: 80 * 16, 53 | describe: 'width of container in pixels' 54 | }, 55 | packages: { 56 | default: PACKAGES, 57 | describe: 'the packages to use, e.g. "base, ams"; use "*" to represent the default packages, e.g, "*, bbox"' 58 | }, 59 | dist: { 60 | boolean: true, 61 | default: false, 62 | describe: 'true to use webpacked version, false to use MathJax source files' 63 | } 64 | }) 65 | .argv; 66 | 67 | // 68 | // Configure MathJax 69 | // 70 | MathJax = { 71 | loader: { 72 | paths: {mathjax: 'mathjax-full/es5'}, 73 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 74 | require: require, 75 | load: ['input/tex-full', 'adaptors/liteDOM'] 76 | }, 77 | tex: { 78 | packages: argv.packages.replace('\*', PACKAGES).split(/\s*,\s*/) 79 | } 80 | } 81 | 82 | // 83 | // Load the MathJax startup module 84 | // 85 | require('mathjax-full/' + (argv.dist ? 'es5' : 'components/src/startup') + '/startup.js'); 86 | 87 | // 88 | // Wait for MathJax to start up, and then typeset the math 89 | // 90 | MathJax.startup.promise.then(() => { 91 | MathJax.tex2mmlPromise(argv._[0] || '', { 92 | display: !argv.inline, 93 | em: argv.em, 94 | ex: argv.ex, 95 | containerWidth: argv.width 96 | }).then(mml => console.log(mml)); 97 | }).catch(err => console.log(err)); 98 | -------------------------------------------------------------------------------- /component/tex2mml-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * component/tex2mml-page 6 | * 7 | * Uses MathJax v3 to convert all TeX in an HTML document to MathML. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2020 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 | // The default TeX packages to use 28 | // 29 | const PACKAGES = 'base, autoload, ams, newcommand, require'; 30 | 31 | // 32 | // Get the command-line arguments 33 | // 34 | var argv = require('yargs') 35 | .demand(0).strict() 36 | .usage('$0 [options] "math"') 37 | .options({ 38 | em: { 39 | default: 16, 40 | describe: 'em-size in pixels' 41 | }, 42 | packages: { 43 | default: PACKAGES, 44 | describe: 'the packages to use, e.g. "base, ams"; use "*" to represent the default packages, e.g, "*, bbox"' 45 | }, 46 | dist: { 47 | boolean: true, 48 | default: false, 49 | describe: 'true to use webpacked version, false to use MathJax source files' 50 | } 51 | }) 52 | .argv; 53 | 54 | // 55 | // Read the HTML file 56 | // 57 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 58 | 59 | // 60 | // A renderAction to take the place of typesetting. 61 | // It renders the output to MathML instead. 62 | // 63 | function actionMML(math, doc) { 64 | const adaptor = doc.adaptor; 65 | const mml = MathJax.startup.toMML(math.root); 66 | math.typesetRoot = adaptor.firstChild(adaptor.body(adaptor.parse(mml, 'text/html'))); 67 | } 68 | 69 | // 70 | // Configure MathJax 71 | // 72 | MathJax = { 73 | loader: { 74 | paths: {mathjax: 'mathjax-full/es5'}, 75 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 76 | require: require, 77 | load: ['input/tex-full', 'adaptors/liteDOM'] 78 | }, 79 | options: { 80 | renderActions: { 81 | typeset: [150, (doc) => {for (const math of doc.math) actionMML(math, doc)}, actionMML] 82 | } 83 | }, 84 | tex: { 85 | packages: argv.packages.replace('\*', PACKAGES).split(/\s*,\s*/) 86 | }, 87 | 'adaptors/liteDOM': { 88 | fontSize: argv.em 89 | }, 90 | startup: { 91 | document: htmlfile 92 | } 93 | } 94 | 95 | // 96 | // Load the MathJax startup module 97 | // 98 | require('mathjax-full/' + (argv.dist ? 'es5' : 'components/src/startup') + '/startup.js'); 99 | 100 | // 101 | // Wait for MathJax to start up, and then render the math. 102 | // Then output the resulting HTML file. 103 | // 104 | MathJax.startup.promise.then(() => { 105 | const adaptor = MathJax.startup.adaptor; 106 | const html = MathJax.startup.document; 107 | html.render(); 108 | console.log(adaptor.doctype(html.document)); 109 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 110 | }).catch(err => console.log(err)); 111 | -------------------------------------------------------------------------------- /component/tex2svg-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * component/tex2svg-page 6 | * 7 | * Uses MathJax v3 to convert all TeX in an SVG document. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // The default TeX packages to use 28 | // 29 | const PACKAGES = 'base, autoload, require, ams, newcommand'; 30 | 31 | // 32 | // Get the command-line arguments 33 | // 34 | var argv = require('yargs') 35 | .demand(0).strict() 36 | .usage('$0 [options] file.html > converted.html') 37 | .options({ 38 | em: { 39 | default: 16, 40 | describe: 'em-size in pixels' 41 | }, 42 | ex: { 43 | default: 8, 44 | describe: 'ex-size in pixels' 45 | }, 46 | packages: { 47 | default: PACKAGES, 48 | describe: 'the packages to use, e.g. "base, ams"; use "*" to represent the default packages, e.g, "*, bbox"' 49 | }, 50 | fontCache: { 51 | default: 'global', 52 | describe: 'cache type: local, global, or none' 53 | }, 54 | dist: { 55 | boolean: true, 56 | default: false, 57 | describe: 'true to use webpacked version, false to use MathJax source files' 58 | } 59 | }) 60 | .argv; 61 | 62 | // 63 | // Read the HTML file 64 | // 65 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 66 | 67 | // 68 | // Configure MathJax 69 | // 70 | MathJax = { 71 | loader: { 72 | paths: {mathjax: 'mathjax-full/es5'}, 73 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 74 | require: require, 75 | load: ['adaptors/liteDOM'] 76 | }, 77 | tex: { 78 | packages: argv.packages.replace('\*', PACKAGES).split(/\s*,\s*/) 79 | }, 80 | svg: { 81 | fontCache: argv.fontCache, 82 | exFactor: argv.ex / argv.em 83 | }, 84 | 'adaptors/liteDOM': { 85 | fontSize: argv.em 86 | }, 87 | startup: { 88 | document: htmlfile 89 | } 90 | } 91 | 92 | // 93 | // Load the MathJax startup module 94 | // 95 | require('mathjax-full/' + (argv.dist ? 'es5' : 'components/src/tex-svg') + '/tex-svg.js'); 96 | 97 | // 98 | // Wait for MathJax to start up, and then typeset the math 99 | // 100 | MathJax.startup.promise.then(() => { 101 | const adaptor = MathJax.startup.adaptor; 102 | const html = MathJax.startup.document; 103 | if (Array.from(html.math).length === 0) { 104 | adaptor.remove(html.outputJax.svgStyles); 105 | const cache = adaptor.elementById(adaptor.body(html.document), 'MJX-SVG-global-cache'); 106 | if (cache) adaptor.remove(cache); 107 | } 108 | console.log(adaptor.doctype(html.document)); 109 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 110 | }).catch(err => console.log(err)); 111 | -------------------------------------------------------------------------------- /custom-tex-extension/README.md: -------------------------------------------------------------------------------- 1 | # Custom TeX Extension 2 | 3 | This example shows how to create a custom TeX extension that defines new TeX commands implemented by javascript functions. 4 | 5 | The commands implemented by this example provides the ability to generate MathML token elements from within TeX by hand. This allows more control over the content and attributes of the elements produced. The macros are `\mi`, `\mo`, `\mn`, `\ms`, and `\mtext`, and they each take an argument that is the text to be used as the content of the corresponding MathML element. The text is not further processed by TeX, but the extension does convert sequences of the form `\uNNNN` (where the N's are hexadecimal digits) into the corresponding unicode character; e.g., `\mi{\u2460}` would produce U+2460, a circled digit 1. 6 | 7 | The main code for the extension is 8 | 9 | * [mml.js](mml.js) 10 | 11 | which contains comments describing it in detail. This file can be loaded into your node project either directly, or as an external component. We describe the latter approach here. 12 | 13 | The key portions of the [code](tex2mml) used to do this is described below. 14 | 15 | First, we add the `mml` package to the default list of packages: 16 | 17 | ```js 18 | const PACKAGES = 'base, autoload, require, ams, newcommand, mml'; 19 | ``` 20 | 21 | Then we configure MathJax to know about the new package: 22 | 23 | ```js 24 | MathJax = { 25 | loader: { 26 | paths: { 27 | mathjax: 'mathjax-full/es5', 28 | custom: '.' 29 | }, 30 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 31 | require: require, 32 | load: ['input/tex', 'adaptors/liteDOM', '[custom]/mml'] 33 | }, 34 | tex: {packages: argv.packages.replace('\*', PACKAGES).split(/\s*,\s*/)}, 35 | startup: { 36 | pageReady: () => { 37 | MathJax.tex2mmlPromise(argv._[0] || '', {display: !argv.inline}) 38 | .then(mml => console.log(mml)) 39 | .catch(err => console.log(err)); 40 | } 41 | } 42 | }; 43 | ``` 44 | 45 | The `loader` block adds a new path named `custom` that is tied to the current directory. This is used to load the custom extension (you could have several extensions in the same directory). It also sets `require` for use with node, and asks MathJax to load the `tex` input component, the `liteDOM` adaptor, and the new custom `mml` extension. (Note that we don't load an output jax in this example, since we are only converting to MathML, which is the internal format of MathJax.) 46 | 47 | The `tex` configuration includes the `mml` package in the `package` array by default. 48 | 49 | Finally, the `startup` block configures the `pageReady()` function to do the default startup initialization, and then does a conversion from TeX to MathML, either printing the result, or an error message. This gives you the chance to see the result of the `\mi`, `\mo`, etc. on the resulting MathML representation. 50 | -------------------------------------------------------------------------------- /custom-tex-extension/mml.js: -------------------------------------------------------------------------------- 1 | /************************************************************* 2 | * 3 | * Copyright (c) 2019 The MathJax Consortium 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | 19 | /** 20 | * @fileoverview Configuration file for sample extension that creates 21 | * some MathML token elements. 22 | * 23 | * @author dpvc@mathjax.org (Davide P. Cervone) 24 | */ 25 | 26 | import {Configuration} from '../node_modules/mathjax-full/js/input/tex/Configuration.js'; 27 | import {CommandMap} from '../node_modules/mathjax-full/js/input/tex/SymbolMap.js'; 28 | import TexError from '../node_modules/mathjax-full/js/input/tex/TexError.js'; 29 | 30 | /** 31 | * This function prevents multi-letter mi elements from being 32 | * interpretted as TEXCLASS.OP 33 | */ 34 | function classORD(node) { 35 | this.getPrevClass(node); 36 | return this; 37 | } 38 | 39 | /** 40 | * Convert \uXXXX to corresponding unicode characters within a string 41 | */ 42 | function convertEscapes(text) { 43 | return text.replace(/\\u([0-9A-F]{4})/gi, (match, hex) => String.fromCharCode(parseInt(hex,16))); 44 | } 45 | 46 | /** 47 | * Allowed attributes on any token element other than the ones with default values 48 | */ 49 | const ALLOWED = { 50 | style: true, 51 | href: true, 52 | id: true, 53 | class: true 54 | }; 55 | 56 | /** 57 | * Parse a string as a set of attribute="value" pairs. 58 | */ 59 | function parseAttributes(text, type) { 60 | const attr = {}; 61 | if (text) { 62 | let match; 63 | while ((match = text.match(/^\s*((?:data-)?[a-z][-a-z]*)\s*=\s*(?:"([^"]*)"|(.*?))(?:\s+|,\s*|$)/i))) { 64 | const name = match[1], value = match[2] || match[3] 65 | if (type.defaults.hasOwnProperty(name) || ALLOWED.hasOwnProperty(name) || name.substr(0,5) === 'data-') { 66 | attr[name] = convertEscapes(value); 67 | } else { 68 | throw new TexError('BadAttribute', 'Unknown attribute "%1"', name); 69 | } 70 | text = text.substr(match[0].length); 71 | } 72 | if (text.length) { 73 | throw new TexError('BadAttributeList', 'Can\'t parse as attributes: %1', text); 74 | } 75 | } 76 | return attr; 77 | } 78 | 79 | /** 80 | * The mapping of control sequence to function calls 81 | */ 82 | const MmlMap = new CommandMap('mmlMap', { 83 | mi: ['mmlToken', 'mi'], 84 | mo: ['mmlToken', 'mo'], 85 | mn: ['mmlToken', 'mn'], 86 | ms: ['mmlToken', 'ms'], 87 | mtext: ['mmlToken', 'mtext'] 88 | }, { 89 | mmlToken(parser, name, type) { 90 | const typeClass = parser.configuration.nodeFactory.mmlFactory.getNodeClass(type); 91 | const def = parseAttributes(parser.GetBrackets(name), typeClass); 92 | const text = convertEscapes(parser.GetArgument(name)); 93 | const mml = parser.create('node', type, [parser.create('text', text)], def); 94 | if (type === 'mi') mml.setTeXclass = classORD; 95 | parser.Push(mml); 96 | } 97 | }); 98 | 99 | /** 100 | * The configuration used to enable the MathML macros 101 | */ 102 | const MmlConfiguration = Configuration.create( 103 | 'mml', {handler: {macro: ['mmlMap']}} 104 | ); 105 | -------------------------------------------------------------------------------- /custom-tex-extension/tex2mml: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * custom-tex-extension/tex2mml 6 | * 7 | * Uses MathJax v3 to show how to create and load a custom tex extension. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | * The mml extension defines \mi{}, \mo{}, \mn{}, \ms{}, \mtext{} for creating 28 | * MathML token elements by hand. They take an optional bracketed argument 29 | * that gives the attributes for the element, e.g. 30 | * 31 | * \mi[mathvariant="bold-italic"]{abc} 32 | * 33 | * The text for the token element is taken as a literal string, except for 34 | * the special sequence \uXXXX, which produces the unicode character U+XXXX. 35 | */ 36 | 37 | // 38 | // The default TeX packages to use 39 | // 40 | const PACKAGES = 'base, autoload, require, ams, newcommand, mml'; 41 | 42 | // 43 | // Get the command-line arguments 44 | // 45 | var argv = require('yargs') 46 | .demand(0).strict() 47 | .usage('$0 [options] "math" > file.html') 48 | .options({ 49 | inline: { 50 | boolean: true, 51 | describe: "process as inline math" 52 | }, 53 | packages: { 54 | default: PACKAGES, 55 | describe: 'the packages to use, e.g. "base, ams"; use "*" to represent the default packages, e.g, "*, bbox"' 56 | }, 57 | dist: { 58 | boolean: true, 59 | default: false, 60 | describe: 'true to use webpacked version, false to use mathjax3 source files' 61 | } 62 | }) 63 | .argv; 64 | 65 | // 66 | // Configure MathJax 67 | // 68 | MathJax = { 69 | loader: { 70 | paths: { 71 | mathjax: 'mathjax-full/es5', 72 | custom: '.' 73 | }, 74 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 75 | require: require, 76 | load: ['input/tex', 'adaptors/liteDOM', '[custom]/mml'] 77 | }, 78 | tex: {packages: argv.packages.replace('\*', PACKAGES).split(/\s*,\s*/)}, 79 | startup: { 80 | pageReady: () => { 81 | MathJax.tex2mmlPromise(argv._[0] || '', {display: !argv.inline}) 82 | .then(mml => console.log(mml)) 83 | .catch(err => console.log(err)); 84 | } 85 | } 86 | }; 87 | 88 | // 89 | // Load the MathJax startup module 90 | // 91 | require('mathjax-full/' + (argv.dist ? 'es5' : 'components/src/startup') + '/startup.js'); 92 | 93 | -------------------------------------------------------------------------------- /direct/README.md: -------------------------------------------------------------------------------- 1 | # Non-Component-Based Examples 2 | 3 | The examples in this directory illustrate how to use MathJax by directly importing the MathJax files directly into your project rather than using the MathJax predefined components. While using components is easier, in general, the direct approach gives you the most control over what is included, and when it is loaded. 4 | 5 | All the examples in this directory consist of four main parts: 6 | 7 | 1. Processing the command-line arguments (not described here), 8 | 2. Importing the necessary MathJax files, 9 | 3. Creating the needed MathJax objects, and 10 | 4. Performing the desired conversion using those objects. 11 | 12 | These parts are described below using the `tex2chtml` command as an example. It loads the following MathJax values: 13 | 14 | ```js 15 | const {mathjax} = require('mathjax-full/js/mathjax.js'); 16 | const {TeX} = require('mathjax-full/js/input/tex.js'); 17 | const {CHTML} = require('mathjax-full/js/output/chtml.js'); 18 | const {liteAdaptor} = require('mathjax-full/js/adaptors/liteAdaptor.js'); 19 | const {RegisterHTMLHandler} = require('mathjax-full/js/handlers/html.js'); 20 | const {AssistiveMmlHandler} = require('mathjax-full/js/a11y/assistive-mml.js'); 21 | 22 | const {AllPackages} = require('mathjax-full/js/input/tex/AllPackages.js'); 23 | ``` 24 | 25 | The `TeX` and `CHTML` objects are the input and output jax class constructors, the `liteAdaptor` is the constructor for the liteDOM adaptor, `RegisterHTMLHandler` is a function used to tell MathJax that we want to work with HTML documents (using a particular DOM adaptor), and `AssistiveMmlHandler` is a function to add the assistive-MathML functionality to the registered handler. Finally, `AllPackages` is an array of the package names to use to initialize the TeX input jax; it includes all the available TeX packages except `autoload` and `require` (which rely on the component system to operate), and `physics` and `colorv2`, which have been loaded, but aren't included in the package array by default since `physics` redefines many standard macros and `color` is used rather than `colorv2` for the `\color` macro. You can add `'physics'` to the array if you want to include it yourself. 26 | 27 | Next we create the needed MathJax objects: 28 | 29 | ```js 30 | const adaptor = liteAdaptor(); 31 | const handler = RegisterHTMLHandler(adaptor); 32 | if (argv.assistiveMml) AssistiveMmlHandler(handler); 33 | 34 | const tex = new TeX({packages: argv.packages.split(/\s*,\s*/)}); 35 | const chtml = new CHTML({fontURL: argv.fontURL}); 36 | const html = mathjax.document('', {InputJax: tex, OutputJax: chtml}); 37 | ``` 38 | 39 | Here we create the `liteDOM` adaptor and use it to tell MathJax that we will use HTML documents with the `liteDOM` implementation of the DOM. We extend the handler to include the assistive-MathML support, if requested on the command line. Then we create the input and output jax, and create an empty document (based on the `liteDOM`) with those input and output jax. 40 | 41 | Finally, we do the conversion: 42 | 43 | ```js 44 | const node = html.convert(argv._[0] || '', { 45 | display: !argv.inline, 46 | em: argv.em, 47 | ex: argv.ex, 48 | containerWidth: argv.width 49 | }); 50 | 51 | if (argv.css) { 52 | console.log(adaptor.textContent(chtml.styleSheet(html))); 53 | } else { 54 | console.log(adaptor.outerHTML(node)); 55 | } 56 | ``` 57 | 58 | This uses the `convert()` method of the math document created above, 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. 59 | -------------------------------------------------------------------------------- /direct/am2chtml: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env node 2 | 3 | /************************************************************************* 4 | * 5 | * direct/am2chtml 6 | * 7 | * Uses MathJax v3 to convert a MathML string to an HTML string. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2018 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 packages needed for MathJax 28 | // 29 | const {mathjax} = require('mathjax-full/js/mathjax.js'); 30 | const {AsciiMath} = require('mathjax-full/js/input/asciimath.js'); 31 | const {CHTML} = require('mathjax-full/js/output/chtml.js'); 32 | const {liteAdaptor} = require('mathjax-full/js/adaptors/liteAdaptor.js'); 33 | const {RegisterHTMLHandler} = require('mathjax-full/js/handlers/html.js'); 34 | const {AssistiveMmlHandler} = require('mathjax-full/js/a11y/assistive-mml.js'); 35 | 36 | // 37 | // Get the command-line arguments 38 | // 39 | var argv = require('yargs') 40 | .demand(0).strict() 41 | .usage('$0 [options] "math" > file.html') 42 | .options({ 43 | em: { 44 | default: 16, 45 | describe: 'em-size in pixels' 46 | }, 47 | ex: { 48 | default: 8, 49 | describe: 'ex-size in pixels' 50 | }, 51 | width: { 52 | default: 80 * 16, 53 | describe: 'width of container in pixels' 54 | }, 55 | css: { 56 | boolean: true, 57 | describe: 'output the required CSS rather than the HTML itself' 58 | }, 59 | fontURL: { 60 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 61 | describe: 'the URL to use for web fonts' 62 | }, 63 | assistiveMml: { 64 | boolean: true, 65 | default: false, 66 | describe: 'whether to include assistive MathML output' 67 | } 68 | }) 69 | .argv; 70 | 71 | // 72 | // Create DOM adaptor and register it for HTML documents 73 | // 74 | const adaptor = liteAdaptor({fontSize: argv.em}); 75 | const handler = RegisterHTMLHandler(adaptor); 76 | if (argv.assistiveMml) AssistiveMmlHandler(handler); 77 | 78 | // 79 | // Create input and output jax and a document using them on the content from the HTML file 80 | // 81 | const asciimath = new AsciiMath(); 82 | const chtml = new CHTML({fontURL: argv.fontURL}); 83 | const html = mathjax.document('', {InputJax: asciimath, OutputJax: chtml}); 84 | 85 | // 86 | // Typeset the math from the command line 87 | // 88 | const node = html.convert(argv._[0] || '', { 89 | display: !argv.inline, 90 | em: argv.em, 91 | ex: argv.ex, 92 | containerWidth: argv.width 93 | }); 94 | 95 | // 96 | // If the --css option was specified, output the CSS, 97 | // Otherwise, typeset the math and output the HTML 98 | // 99 | if (argv.css) { 100 | console.log(adaptor.textContent(chtml.styleSheet(html))); 101 | } else { 102 | console.log(adaptor.outerHTML(node)); 103 | } 104 | -------------------------------------------------------------------------------- /direct/am2chtml-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env node 2 | 3 | /************************************************************************* 4 | * 5 | * direct/am2chtml-page 6 | * 7 | * Uses MathJax v3 to convert all AsciiMath in an HTML document. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2018 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 packages needed for MathJax 28 | // 29 | const {mathjax} = require('mathjax-full/js/mathjax.js'); 30 | const {AsciiMath} = require('mathjax-full/js/input/asciimath.js'); 31 | const {CHTML} = require('mathjax-full/js/output/chtml.js'); 32 | const {liteAdaptor} = require('mathjax-full/js/adaptors/liteAdaptor.js'); 33 | const {RegisterHTMLHandler} = require('mathjax-full/js/handlers/html.js'); 34 | const {AssistiveMmlHandler} = require('mathjax-full/js/a11y/assistive-mml.js'); 35 | 36 | require('mathjax-full/js/util/entities/all.js'); 37 | 38 | // 39 | // Get the command-line arguments 40 | // 41 | var argv = require('yargs') 42 | .demand(1).strict() 43 | .usage('$0 [options] file.html > converted.html') 44 | .options({ 45 | em: { 46 | default: 16, 47 | describe: 'em-size in pixels' 48 | }, 49 | ex: { 50 | default: 8, 51 | describe: 'ex-size in pixels' 52 | }, 53 | fontURL: { 54 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 55 | describe: 'the URL to use for web fonts' 56 | } 57 | }) 58 | .argv; 59 | 60 | // 61 | // Read the HTML file 62 | // 63 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 64 | 65 | // 66 | // Create DOM adaptor and register it for HTML documents 67 | // 68 | const adaptor = liteAdaptor({fontSize: argv.em}); 69 | AssistiveMmlHandler(RegisterHTMLHandler(adaptor)); 70 | 71 | // 72 | // Create input and output jax and a document using them on the content from the HTML file 73 | // 74 | const asciimath = new AsciiMath(); 75 | const chtml = new CHTML({fontURL: argv.fontURL, exFactor: argv.ex / argv.em}); 76 | const html = mathjax.document(htmlfile, {InputJax: asciimath, OutputJax: chtml}); 77 | 78 | // 79 | // Typeset the document 80 | // 81 | html.render(); 82 | 83 | // 84 | // If no math was found on the page, remove the stylesheet 85 | // 86 | if (Array.from(html.math).length === 0) adaptor.remove(html.outputJax.chtmlStyles); 87 | 88 | // 89 | // Output the resulting HTML 90 | // 91 | console.log(adaptor.doctype(html.document)); 92 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 93 | -------------------------------------------------------------------------------- /direct/am2mml: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env node 2 | 3 | /************************************************************************* 4 | * 5 | * direct/am2mml 6 | * 7 | * Uses MathJax v3 to convert an AsciiMath string to a MathML string. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2018 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 packages needed for MathJax 28 | // 29 | const {AsciiMath} = require('mathjax-full/js/input/asciimath.js'); 30 | const {HTMLDocument} = require('mathjax-full/js/handlers/html/HTMLDocument.js'); 31 | const {liteAdaptor} = require('mathjax-full/js/adaptors/liteAdaptor.js'); 32 | const {STATE} = require('mathjax-full/js/core/MathItem.js'); 33 | 34 | // 35 | // Get the command-line arguments 36 | // 37 | var argv = require('yargs') 38 | .demand(0).strict() 39 | .usage('$0 [options] "math" > file.html') 40 | .argv; 41 | 42 | // 43 | // Create the input jax 44 | // 45 | const asciimath = new AsciiMath(); 46 | 47 | // 48 | // Create an HTML document using a LiteDocument and the input jax 49 | // 50 | const html = new HTMLDocument('', liteAdaptor(), {InputJax: asciimath}); 51 | 52 | // 53 | // Create a MathML serializer 54 | // 55 | const {SerializedMmlVisitor} = require('mathjax-full/js/core/MmlTree/SerializedMmlVisitor.js'); 56 | const visitor = new SerializedMmlVisitor(); 57 | const toMathML = (node => visitor.visitTree(node, html)); 58 | 59 | // 60 | // Convert the math from the command line to serialzied MathML 61 | // 62 | console.log(toMathML(html.convert(argv._[0] || '', {display: !argv.inline, end: STATE.CONVERT}))); 63 | -------------------------------------------------------------------------------- /direct/mml2chtml: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env node 2 | 3 | /************************************************************************* 4 | * 5 | * direct/mml2chtml 6 | * 7 | * Uses MathJax v3 to convert a MathML string to an HTML string. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2018 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 packages needed for MathJax 28 | // 29 | const {mathjax} = require('mathjax-full/js/mathjax.js'); 30 | const {MathML} = require('mathjax-full/js/input/mathml.js'); 31 | const {CHTML} = require('mathjax-full/js/output/chtml.js'); 32 | const {liteAdaptor} = require('mathjax-full/js/adaptors/liteAdaptor.js'); 33 | const {RegisterHTMLHandler} = require('mathjax-full/js/handlers/html.js'); 34 | const {AssistiveMmlHandler} = require('mathjax-full/js/a11y/assistive-mml.js'); 35 | 36 | // 37 | // Get the command-line arguments 38 | // 39 | var argv = require('yargs') 40 | .demand(0).strict() 41 | .usage('$0 [options] "math" > file.html') 42 | .options({ 43 | em: { 44 | default: 16, 45 | describe: 'em-size in pixels' 46 | }, 47 | ex: { 48 | default: 8, 49 | describe: 'ex-size in pixels' 50 | }, 51 | width: { 52 | default: 80 * 16, 53 | describe: 'width of container in pixels' 54 | }, 55 | css: { 56 | boolean: true, 57 | describe: 'output the required CSS rather than the HTML itself' 58 | }, 59 | fontURL: { 60 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 61 | describe: 'the URL to use for web fonts' 62 | }, 63 | assistiveMml: { 64 | boolean: true, 65 | default: false, 66 | describe: 'whether to include assistive MathML output' 67 | } 68 | }) 69 | .argv; 70 | 71 | // 72 | // Create DOM adaptor and register it for HTML documents 73 | // 74 | const adaptor = liteAdaptor({fontSize: argv.em}); 75 | const handler = RegisterHTMLHandler(adaptor); 76 | if (argv.assistiveMml) AssistiveMmlHandler(handler); 77 | 78 | // 79 | // Create input and output jax and a document using them on the content from the HTML file 80 | // 81 | const mml = new MathML(); 82 | const chtml = new CHTML({fontURL: argv.fontURL}); 83 | const html = mathjax.document('', {InputJax: mml, OutputJax: chtml}); 84 | 85 | // 86 | // Typeset the math from the command line 87 | // 88 | const node = html.convert(argv._[0] || '', { 89 | display: !argv.inline, 90 | em: argv.em, 91 | ex: argv.ex, 92 | containerWidth: argv.width 93 | }); 94 | 95 | // 96 | // If the --css option was specified, output the CSS, 97 | // Otherwise, typeset the math and output the HTML 98 | // 99 | if (argv.css) { 100 | console.log(adaptor.textContent(chtml.styleSheet(html))); 101 | } else { 102 | console.log(adaptor.outerHTML(node)); 103 | } 104 | -------------------------------------------------------------------------------- /direct/mml2chtml-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env node 2 | 3 | /************************************************************************* 4 | * 5 | * direct/mml2chtml-page 6 | * 7 | * Uses MathJax v3 to convert all MathML in an HTML document. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2018 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 packages needed for MathJax 28 | // 29 | const {mathjax} = require('mathjax-full/js/mathjax.js'); 30 | const {MathML} = require('mathjax-full/js/input/mathml.js'); 31 | const {CHTML} = require('mathjax-full/js/output/chtml.js'); 32 | const {liteAdaptor} = require('mathjax-full/js/adaptors/liteAdaptor.js'); 33 | const {RegisterHTMLHandler} = require('mathjax-full/js/handlers/html.js'); 34 | const {AssistiveMmlHandler} = require('mathjax-full/js/a11y/assistive-mml.js'); 35 | 36 | require('mathjax-full/js/util/entities/all.js'); 37 | 38 | // 39 | // Get the command-line arguments 40 | // 41 | var argv = require('yargs') 42 | .demand(1).strict() 43 | .usage('$0 [options] file.html > converted.html') 44 | .options({ 45 | em: { 46 | default: 16, 47 | describe: 'em-size in pixels' 48 | }, 49 | ex: { 50 | default: 8, 51 | describe: 'ex-size in pixels' 52 | }, 53 | fontURL: { 54 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 55 | describe: 'the URL to use for web fonts' 56 | } 57 | }) 58 | .argv; 59 | 60 | // 61 | // Read the HTML file 62 | // 63 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 64 | 65 | // 66 | // Create DOM adaptor and register it for HTML documents 67 | // 68 | const adaptor = liteAdaptor({fontSize: argv.em}); 69 | AssistiveMmlHandler(RegisterHTMLHandler(adaptor)); 70 | 71 | // 72 | // Create input and output jax and a document using them on the content from the HTML file 73 | // 74 | const mathml = new MathML(); 75 | const chtml = new CHTML({fontURL: argv.fontURL, exFactor: argv.ex / argv.em}); 76 | const html = mathjax.document(htmlfile, {InputJax: mathml, OutputJax: chtml}); 77 | 78 | // 79 | // Typeset the document 80 | // 81 | html.render(); 82 | 83 | // 84 | // If no math was found on the page, remove the stylesheet 85 | // 86 | if (Array.from(html.math).length === 0) adaptor.remove(html.outputJax.chtmlStyles); 87 | 88 | // 89 | // Output the resulting HTML 90 | // 91 | console.log(adaptor.doctype(html.document)); 92 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 93 | -------------------------------------------------------------------------------- /direct/mml2svg: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env node 2 | 3 | /************************************************************************* 4 | * 5 | * direct/mml2svg 6 | * 7 | * Uses MathJax v3 to convert a MathML string to an SVG string. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2018 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 packages needed for MathJax 28 | // 29 | const {mathjax} = require('mathjax-full/js/mathjax.js'); 30 | const {MathML} = require('mathjax-full/js/input/mathml.js'); 31 | const {SVG} = require('mathjax-full/js/output/svg.js'); 32 | const {liteAdaptor} = require('mathjax-full/js/adaptors/liteAdaptor.js'); 33 | const {RegisterHTMLHandler} = require('mathjax-full/js/handlers/html.js'); 34 | const {AssistiveMmlHandler} = require('mathjax-full/js/a11y/assistive-mml.js'); 35 | 36 | // 37 | // Get the command-line arguments 38 | // 39 | var argv = require('yargs') 40 | .demand(0).strict() 41 | .usage('$0 [options] "math" > file.html') 42 | .options({ 43 | em: { 44 | default: 16, 45 | describe: 'em-size in pixels' 46 | }, 47 | ex: { 48 | default: 8, 49 | describe: 'ex-size in pixels' 50 | }, 51 | width: { 52 | default: 80 * 16, 53 | describe: 'width of container in pixels' 54 | }, 55 | styles: { 56 | boolean: true, 57 | default: true, 58 | describe: 'include css styles for stand-alone image' 59 | }, 60 | container: { 61 | boolean: true, 62 | describe: 'include element' 63 | }, 64 | css: { 65 | boolean: true, 66 | describe: 'output the required CSS rather than the SVG itself' 67 | }, 68 | fontCache: { 69 | boolean: true, 70 | default: true, 71 | describe: 'whether to use a local font cache or not' 72 | }, 73 | assistiveMml: { 74 | boolean: true, 75 | default: false, 76 | describe: 'whether to include assistive MathML output' 77 | } 78 | }) 79 | .argv; 80 | 81 | // 82 | // Minimal CSS needed for stand-alone image 83 | // 84 | const CSS = [ 85 | 'svg a{fill:blue;stroke:blue}', 86 | '[data-mml-node="merror"]>g{fill:red;stroke:red}', 87 | '[data-mml-node="merror"]>rect[data-background]{fill:yellow;stroke:none}', 88 | '[data-frame],[data-line]{stroke-width:70px;fill:none}', 89 | '.mjx-dashed{stroke-dasharray:140}', 90 | '.mjx-dotted{stroke-linecap:round;stroke-dasharray:0,140}', 91 | 'use[data-c]{stroke-width:3px}' 92 | ].join(''); 93 | 94 | // 95 | // Create DOM adaptor and register it for HTML documents 96 | // 97 | const adaptor = liteAdaptor(); 98 | const handler = RegisterHTMLHandler(adaptor); 99 | if (argv.assistiveMml) AssistiveMmlHandler(handler); 100 | 101 | // 102 | // Create input and output jax and a document using them on the content from the HTML file 103 | // 104 | const mml = new MathML(); 105 | const svg = new SVG({fontCache: (argv.fontCache ? 'local' : 'none')}); 106 | const html = mathjax.document('', {InputJax: mml, OutputJax: svg}); 107 | 108 | // 109 | // Typeset the math from the command line 110 | // 111 | const node = html.convert(argv._[0] || '', { 112 | display: !argv.inline, 113 | em: argv.em, 114 | ex: argv.ex, 115 | containerWidth: argv.width 116 | }); 117 | 118 | // 119 | // If the --css option was specified, output the CSS, 120 | // Otherwise, typeset the math and output the HTML 121 | // 122 | if (argv.css) { 123 | console.log(adaptor.textContent(svg.styleSheet(html))); 124 | } else { 125 | let html = (argv.container ? adaptor.outerHTML(node) : adaptor.innerHTML(node)); 126 | console.log(argv.styles ? html.replace(//, ``) : html); 127 | } 128 | -------------------------------------------------------------------------------- /direct/mml2svg-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env node 2 | 3 | /************************************************************************* 4 | * 5 | * diret/mml2svg-page 6 | * 7 | * Uses MathJax v3 to convert all MathML in an HTML document. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2018 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 packages needed for MathJax 28 | // 29 | const {mathjax} = require('mathjax-full/js/mathjax.js'); 30 | const {MathML} = require('mathjax-full/js/input/mathml.js'); 31 | const {SVG} = require('mathjax-full/js/output/svg.js'); 32 | const {liteAdaptor} = require('mathjax-full/js/adaptors/liteAdaptor.js'); 33 | const {RegisterHTMLHandler} = require('mathjax-full/js/handlers/html.js'); 34 | const {AssistiveMmlHandler} = require('mathjax-full/js/a11y/assistive-mml.js'); 35 | 36 | require('mathjax-full/js/util/entities/all.js'); 37 | 38 | // 39 | // Get the command-line arguments 40 | // 41 | var argv = require('yargs') 42 | .demand(1).strict() 43 | .usage('$0 [options] file.html > converted.html') 44 | .options({ 45 | em: { 46 | default: 16, 47 | describe: 'em-size in pixels' 48 | }, 49 | ex: { 50 | default: 8, 51 | describe: 'ex-size in pixels' 52 | }, 53 | fontCache: { 54 | default: 'global', 55 | describe: 'cache type: local, global, none' 56 | } 57 | }) 58 | .argv; 59 | 60 | // 61 | // Read the HTML file 62 | // 63 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 64 | 65 | // 66 | // Create DOM adaptor and register it for HTML documents 67 | // 68 | const adaptor = liteAdaptor({fontSize: argv.em}); 69 | AssistiveMmlHandler(RegisterHTMLHandler(adaptor)); 70 | 71 | // 72 | // Create input and output jax and a document using them on the content from the HTML file 73 | // 74 | const mathml = new MathML(); 75 | const svg = new SVG({fontCache: argv.fontCache, exFactor: argv.ex / argv.em}); 76 | const html = mathjax.document(htmlfile, {InputJax: mathml, OutputJax: svg}); 77 | 78 | // 79 | // Typeset the document 80 | // 81 | html.render(); 82 | 83 | // 84 | // If no math was found on the page, remove the stylesheet and font cache (if any) 85 | // 86 | if (Array.from(html.math).length === 0) { 87 | adaptor.remove(html.outputJax.svgStyles); 88 | const cache = adaptor.elementById(adaptor.body(html.document), 'MJX-SVG-global-cache'); 89 | if (cache) adaptor.remove(cache); 90 | } 91 | 92 | // 93 | // Output the resulting HTML 94 | // 95 | console.log(adaptor.doctype(html.document)); 96 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 97 | -------------------------------------------------------------------------------- /direct/tex2chtml: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env node 2 | 3 | /************************************************************************* 4 | * 5 | * direct/tex2chtml 6 | * 7 | * Uses MathJax v3 to convert a TeX string to an HTML string. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2018 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 packages needed for MathJax 28 | // 29 | const {mathjax} = require('mathjax-full/js/mathjax.js'); 30 | const {TeX} = require('mathjax-full/js/input/tex.js'); 31 | const {CHTML} = require('mathjax-full/js/output/chtml.js'); 32 | const {liteAdaptor} = require('mathjax-full/js/adaptors/liteAdaptor.js'); 33 | const {RegisterHTMLHandler} = require('mathjax-full/js/handlers/html.js'); 34 | const {AssistiveMmlHandler} = require('mathjax-full/js/a11y/assistive-mml.js'); 35 | 36 | const {AllPackages} = require('mathjax-full/js/input/tex/AllPackages.js'); 37 | 38 | // 39 | // Get the command-line arguments 40 | // 41 | var argv = require('yargs') 42 | .demand(0).strict() 43 | .usage('$0 [options] "math" > file.html') 44 | .options({ 45 | inline: { 46 | boolean: true, 47 | describe: "process as inline math" 48 | }, 49 | em: { 50 | default: 16, 51 | describe: 'em-size in pixels' 52 | }, 53 | ex: { 54 | default: 8, 55 | describe: 'ex-size in pixels' 56 | }, 57 | width: { 58 | default: 80 * 16, 59 | describe: 'width of container in pixels' 60 | }, 61 | packages: { 62 | default: AllPackages.sort().join(', '), 63 | describe: 'the packages to use, e.g. "base, ams"' 64 | }, 65 | css: { 66 | boolean: true, 67 | describe: 'output the required CSS rather than the HTML itself' 68 | }, 69 | fontURL: { 70 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 71 | describe: 'the URL to use for web fonts' 72 | }, 73 | assistiveMml: { 74 | boolean: true, 75 | default: false, 76 | describe: 'whether to include assistive MathML output' 77 | } 78 | }) 79 | .argv; 80 | 81 | // 82 | // Create DOM adaptor and register it for HTML documents 83 | // 84 | const adaptor = liteAdaptor(); 85 | const handler = RegisterHTMLHandler(adaptor); 86 | if (argv.assistiveMml) AssistiveMmlHandler(handler); 87 | 88 | // 89 | // Create input and output jax and a document using them on the content from the HTML file 90 | // 91 | const tex = new TeX({packages: argv.packages.split(/\s*,\s*/)}); 92 | const chtml = new CHTML({fontURL: argv.fontURL}); 93 | const html = mathjax.document('', {InputJax: tex, OutputJax: chtml}); 94 | 95 | // 96 | // Typeset the math from the command line 97 | // 98 | const node = html.convert(argv._[0] || '', { 99 | display: !argv.inline, 100 | em: argv.em, 101 | ex: argv.ex, 102 | containerWidth: argv.width 103 | }); 104 | 105 | // 106 | // If the --css option was specified, output the CSS, 107 | // Otherwise, typeset the math and output the HTML 108 | // 109 | if (argv.css) { 110 | console.log(adaptor.textContent(chtml.styleSheet(html))); 111 | } else { 112 | console.log(adaptor.outerHTML(node)); 113 | } 114 | -------------------------------------------------------------------------------- /direct/tex2chtml-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env node 2 | 3 | /************************************************************************* 4 | * 5 | * direct/tex2chtml-page 6 | * 7 | * Uses MathJax v3 to convert all TeX in an HTML document. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2018 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 packages needed for MathJax 28 | // 29 | const {mathjax} = require('mathjax-full/js/mathjax.js'); 30 | const {TeX} = require('mathjax-full/js/input/tex.js'); 31 | const {CHTML} = require('mathjax-full/js/output/chtml.js'); 32 | const {liteAdaptor} = require('mathjax-full/js/adaptors/liteAdaptor.js'); 33 | const {RegisterHTMLHandler} = require('mathjax-full/js/handlers/html.js'); 34 | const {AssistiveMmlHandler} = require('mathjax-full/js/a11y/assistive-mml.js'); 35 | 36 | const {AllPackages} = require('mathjax-full/js/input/tex/AllPackages.js'); 37 | 38 | require('mathjax-full/js/util/entities/all.js'); 39 | 40 | // 41 | // Get the command-line arguments 42 | // 43 | var argv = require('yargs') 44 | .demand(1).strict() 45 | .usage('$0 [options] file.html > converted.html') 46 | .options({ 47 | em: { 48 | default: 16, 49 | describe: 'em-size in pixels' 50 | }, 51 | ex: { 52 | default: 8, 53 | describe: 'ex-size in pixels' 54 | }, 55 | packages: { 56 | default: AllPackages.sort().join(', '), 57 | describe: 'the packages to use, e.g. "base, ams"' 58 | }, 59 | fontURL: { 60 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 61 | describe: 'the URL to use for web fonts' 62 | } 63 | }) 64 | .argv; 65 | 66 | // 67 | // Read the HTML file 68 | // 69 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 70 | 71 | // 72 | // Create DOM adaptor and register it for HTML documents 73 | // 74 | const adaptor = liteAdaptor({fontSize: argv.em}); 75 | AssistiveMmlHandler(RegisterHTMLHandler(adaptor)); 76 | 77 | // 78 | // Create input and output jax and a document using them on the content from the HTML file 79 | // 80 | const tex = new TeX({packages: argv.packages.split(/\s*,\s*/), inlineMath: [['$','$']]}); 81 | const chtml = new CHTML({fontURL: argv.fontURL, exFactor: argv.ex / argv.em}); 82 | const html = mathjax.document(htmlfile, {InputJax: tex, OutputJax: chtml}); 83 | 84 | // 85 | // Typeset the document 86 | // 87 | html.render(); 88 | 89 | // 90 | // If no math was found on the page, remove the stylesheet 91 | // 92 | if (Array.from(html.math).length === 0) adaptor.remove(html.outputJax.chtmlStyles); 93 | 94 | // 95 | // Output the resulting HTML 96 | // 97 | console.log(adaptor.doctype(html.document)); 98 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 99 | -------------------------------------------------------------------------------- /direct/tex2mml: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env node 2 | 3 | /************************************************************************* 4 | * 5 | * direct/tex2mml 6 | * 7 | * Uses MathJax v3 to convert a TeX string to a MathML string. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2018 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 packages needed for MathJax 28 | // 29 | const {TeX} = require('mathjax-full/js/input/tex.js'); 30 | const {HTMLDocument} = require('mathjax-full/js/handlers/html/HTMLDocument.js'); 31 | const {liteAdaptor} = require('mathjax-full/js/adaptors/liteAdaptor.js'); 32 | const {STATE} = require('mathjax-full/js/core/MathItem.js'); 33 | 34 | const {AllPackages} = require('mathjax-full/js/input/tex/AllPackages.js'); 35 | 36 | // 37 | // Busproofs requires an output jax, which we aren't using 38 | // 39 | const packages = AllPackages.filter((name) => name !== 'bussproofs'); 40 | 41 | // 42 | // Get the command-line arguments 43 | // 44 | var argv = require('yargs') 45 | .demand(0).strict() 46 | .usage('$0 [options] "math" > file.html') 47 | .options({ 48 | inline: { 49 | boolean: true, 50 | describe: "process as inline math" 51 | }, 52 | packages: { 53 | default: packages.sort().join(', '), 54 | describe: 'the packages to use, e.g. "base, ams"' 55 | } 56 | }) 57 | .argv; 58 | 59 | // 60 | // Create the input jax 61 | // 62 | const tex = new TeX({packages: argv.packages.split(/\s*,\s*/)}); 63 | 64 | // 65 | // Create an HTML document using a LiteDocument and the input jax 66 | // 67 | const html = new HTMLDocument('', liteAdaptor(), {InputJax: tex}); 68 | 69 | // 70 | // Create a MathML serializer 71 | // 72 | const {SerializedMmlVisitor} = require('mathjax-full/js/core/MmlTree/SerializedMmlVisitor.js'); 73 | const visitor = new SerializedMmlVisitor(); 74 | const toMathML = (node => visitor.visitTree(node, html)); 75 | 76 | // 77 | // Convert the math from the command line to serialzied MathML 78 | // 79 | console.log(toMathML(html.convert(argv._[0] || '', {display: !argv.inline, end: STATE.CONVERT}))); 80 | -------------------------------------------------------------------------------- /direct/tex2mml-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env node 2 | 3 | /************************************************************************* 4 | * 5 | * direct/tex2mml-page 6 | * 7 | * Uses MathJax v3 to convert all TeX in an HTML document to MathML. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2020 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 packages needed for MathJax 28 | // 29 | const {mathjax} = require('mathjax-full/js/mathjax.js'); 30 | const {TeX} = require('mathjax-full/js/input/tex.js'); 31 | const {RegisterHTMLHandler} = require('mathjax-full/js/handlers/html.js'); 32 | const {liteAdaptor} = require('mathjax-full/js/adaptors/liteAdaptor.js'); 33 | const {STATE} = require('mathjax-full/js/core/MathItem.js'); 34 | 35 | const {AllPackages} = require('mathjax-full/js/input/tex/AllPackages.js'); 36 | 37 | // 38 | // Busproofs requires an output jax, which we aren't using 39 | // 40 | const packages = AllPackages.filter((name) => name !== 'bussproofs'); 41 | 42 | // 43 | // Get the command-line arguments 44 | // 45 | var argv = require('yargs') 46 | .demand(0).strict() 47 | .usage('$0 [options] "math" > file.html') 48 | .options({ 49 | em: { 50 | default: 16, 51 | describe: 'em-size in pixels' 52 | }, 53 | packages: { 54 | default: packages.sort().join(', '), 55 | describe: 'the packages to use, e.g. "base, ams"' 56 | } 57 | }) 58 | .argv; 59 | 60 | // 61 | // Read the HTML file 62 | // 63 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 64 | 65 | // 66 | // Create DOM adaptor and register it for HTML documents 67 | // 68 | const adaptor = liteAdaptor({fontSize: argv.em}); 69 | const handler = RegisterHTMLHandler(adaptor); 70 | 71 | // 72 | // Create a MathML serializer 73 | // 74 | const {SerializedMmlVisitor} = require('mathjax-full/js/core/MmlTree/SerializedMmlVisitor.js'); 75 | const visitor = new SerializedMmlVisitor(); 76 | const toMathML = (node => visitor.visitTree(node, html)); 77 | 78 | // 79 | // A renderAction to take the place of typesetting. 80 | // It renders the output to MathML instead. 81 | // 82 | function actionMML(math, doc) { 83 | const adaptor = doc.adaptor; 84 | const mml = toMathML(math.root); 85 | math.typesetRoot = adaptor.firstChild(adaptor.body(adaptor.parse(mml, 'text/html'))); 86 | } 87 | 88 | // 89 | // Create an HTML document using the html file and a new TeX input jax 90 | // 91 | const html = mathjax.document(htmlfile, { 92 | renderActions: { 93 | typeset: [150, (doc) => {for (const math of doc.math) actionMML(math, doc)}, actionMML] 94 | }, 95 | InputJax: new TeX({packages: argv.packages.split(/\s*,\s*/)}) 96 | }); 97 | 98 | // 99 | // Render the document and print the results 100 | // 101 | html.render(); 102 | console.log(adaptor.doctype(html.document)); 103 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 104 | -------------------------------------------------------------------------------- /direct/tex2svg-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env node 2 | 3 | /************************************************************************* 4 | * 5 | * direct/tex2svg-page 6 | * 7 | * Uses MathJax v3 to convert all TeX in an HTML document. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2018 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 packages needed for MathJax 28 | // 29 | const {mathjax} = require('mathjax-full/js/mathjax.js'); 30 | const {TeX} = require('mathjax-full/js/input/tex.js'); 31 | const {SVG} = require('mathjax-full/js/output/svg.js'); 32 | const {liteAdaptor} = require('mathjax-full/js/adaptors/liteAdaptor.js'); 33 | const {RegisterHTMLHandler} = require('mathjax-full/js/handlers/html.js'); 34 | const {AssistiveMmlHandler} = require('mathjax-full/js/a11y/assistive-mml.js'); 35 | 36 | const {AllPackages} = require('mathjax-full/js/input/tex/AllPackages.js'); 37 | 38 | require('mathjax-full/js/util/entities/all.js'); 39 | 40 | // 41 | // Get the command-line arguments 42 | // 43 | var argv = require('yargs') 44 | .demand(1).strict() 45 | .usage('$0 [options] file.html > converted.html') 46 | .options({ 47 | em: { 48 | default: 16, 49 | describe: 'em-size in pixels' 50 | }, 51 | ex: { 52 | default: 8, 53 | describe: 'ex-size in pixels' 54 | }, 55 | packages: { 56 | default: AllPackages.sort().join(', '), 57 | describe: 'the packages to use, e.g. "base, ams"' 58 | }, 59 | fontCache: { 60 | default: 'global', 61 | describe: 'cache type: local, global, none' 62 | } 63 | }) 64 | .argv; 65 | 66 | // 67 | // Read the HTML file 68 | // 69 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 70 | 71 | // 72 | // Create DOM adaptor and register it for HTML documents 73 | // 74 | const adaptor = liteAdaptor({fontSize: argv.em}); 75 | AssistiveMmlHandler(RegisterHTMLHandler(adaptor)); 76 | 77 | // 78 | // Create input and output jax and a document using them on the content from the HTML file 79 | // 80 | const tex = new TeX({packages: argv.packages.split(/\s*,\s*/)}); 81 | const svg = new SVG({fontCache: argv.fontCache, exFactor: argv.ex / argv.em}); 82 | const html = mathjax.document(htmlfile, {InputJax: tex, OutputJax: svg}); 83 | 84 | // 85 | // Typeset the document 86 | // 87 | html.render(); 88 | 89 | // 90 | // If no math was found on the page, remove the stylesheet and font cache (if any) 91 | // 92 | if (Array.from(html.math).length === 0) { 93 | adaptor.remove(html.outputJax.svgStyles); 94 | const cache = adaptor.elementById(adaptor.body(html.document), 'MJX-SVG-global-cache'); 95 | if (cache) adaptor.remove(cache); 96 | } 97 | 98 | // 99 | // Output the resulting HTML 100 | // 101 | console.log(adaptor.doctype(html.document)); 102 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 103 | -------------------------------------------------------------------------------- /jsdom/README.md: -------------------------------------------------------------------------------- 1 | # MathJax in JSDOM 2 | 3 | This example shows how to run MathJax within a [JSDOM](https://github.com/jsdom/jsdom) instance. Although MathJax provides a lightweight DOM implementation (called LiteDOM) for use in node applications, it is limited in its scope, and you may want to work within a more full-featured DOM. For example, if you plan to manipulate the DOM as though you were using a browser, it may be more convenient to to use JSDOM than LiteDOM. Note, however, that because JSDOM implements a lot more of the DOM functionality, it is slower and larger. 4 | 5 | ## The Examples (Simple Approach) 6 | 7 | The `tex2chtml` and `tex2chtml-page` examples in this directory are nearly identical to the ones in the [`simple`](../simple) directory, with the exception that they use the `jsdomAdaptor` rather than the `liteAdaptor`. The key changes are to replace the `loader` block of the configuration to 8 | 9 | ``` javascript 10 | loader: { 11 | paths: {jsdom: `${__dirname}/adaptor`}, 12 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 13 | load: ['[jsdom]/adaptor' + (argv.dist ? '.min.js' : ''), 'tex-chtml'] 14 | }, 15 | ``` 16 | 17 | which sets up a `[jsdom]` path prefix, and loads the `jsdomAdaptor` from that location, and the addition of 18 | 19 | ``` javascript 20 | JSDOM: require('jsdom').JSDOM, 21 | ``` 22 | 23 | to the configuration, since the `jsdomAdaptor` needs access to that. 24 | 25 | 26 | ## The Examples (Direct Approach) 27 | 28 | The `tex2svg` and `tex2svg-page` examples in this directory are nearly identical to the ones in the [`direct`](../direct) directory, with the exception that they use the `jsdomAdaptor` rather than the `liteAdaptor`. The key changes are using 29 | 30 | ``` javascript 31 | const {jsdomAdaptor} = require('mathjax-full/js/adaptors/jsdomAdaptor.js'); 32 | ``` 33 | 34 | instead of the corresponding `liteAdaptor` require, the addition of 35 | 36 | ``` javascript 37 | const {JSDOM} = require('jsdom'); 38 | ``` 39 | 40 | in order to load the JSDOM library, and 41 | 42 | ``` javascript 43 | const adaptor = jsdomAdaptor(JSDOM); 44 | ``` 45 | 46 | rather than the corresponding `liteAdaptor()` call. The rest of the code is identical to the direct approach with the lite adaptor. 47 | 48 | Note that there is no JSDOM component, so you can't use the component-based approaches to MathJax with JSDOM. It would be possible to construct a custom component for the JSDOM adaptor, if you needed to use the components approach. 49 | 50 | 51 | 52 | ## Installation 53 | 54 | In order to try out this example you must install its dependencies. Since the code relies on JSDOM, that needs to be installed, so this directory contains a separate `package.json` file, and you should do the following: 55 | 56 | ``` bash 57 | cd MathJax-demos-node/jsdom 58 | npm install --production 59 | ``` 60 | 61 | (If you wish to rebuild the jsdom adaptor component, then leave off the `--production` so that the developer dependencies will be isntalled.) 62 | 63 | The example files should be executables that you can run. On non-unix systems, you may need to call 64 | 65 | ``` bash 66 | node -r esm 67 | ``` 68 | 69 | where `` is the name of the example file. Some examples take an argument (like a TeX string) that follows the `` above. 70 | 71 | To rebuild the JSDOM adaptor component (in the [`adaptor`](adaptor) directory), do the following: 72 | 73 | ``` bash 74 | cd MathJax-demos-node/jsdom/adaptor 75 | ../node_modules/mathjax-full/components/bin/pack 76 | ``` 77 | -------------------------------------------------------------------------------- /jsdom/adaptor/adaptor.js: -------------------------------------------------------------------------------- 1 | const {jsdomAdaptor} = require('mathjax-full/js/adaptors/jsdomAdaptor.js'); 2 | 3 | if (MathJax.startup) { 4 | MathJax.startup.registerConstructor('jsdomAdaptor', () => jsdomAdaptor(MathJax.config.JSDOM)); 5 | MathJax.startup.useAdaptor('jsdomAdaptor', true); 6 | } 7 | -------------------------------------------------------------------------------- /jsdom/adaptor/adaptor.min.js: -------------------------------------------------------------------------------- 1 | !function(){var t,o={157:function(t,o){"use strict";Object.defineProperty(o,"__esModule",{value:!0}),o.HTMLAdaptor=MathJax._.adaptors.HTMLAdaptor.HTMLAdaptor},225:function(t,o){"use strict";Object.defineProperty(o,"__esModule",{value:!0}),o.isObject=MathJax._.util.Options.isObject,o.APPEND=MathJax._.util.Options.APPEND,o.REMOVE=MathJax._.util.Options.REMOVE,o.OPTIONS=MathJax._.util.Options.OPTIONS,o.Expandable=MathJax._.util.Options.Expandable,o.expandable=MathJax._.util.Options.expandable,o.makeArray=MathJax._.util.Options.makeArray,o.keys=MathJax._.util.Options.keys,o.copy=MathJax._.util.Options.copy,o.insert=MathJax._.util.Options.insert,o.defaultOptions=MathJax._.util.Options.defaultOptions,o.userOptions=MathJax._.util.Options.userOptions,o.selectOptions=MathJax._.util.Options.selectOptions,o.selectOptionsFromKeys=MathJax._.util.Options.selectOptionsFromKeys,o.separateOptions=MathJax._.util.Options.separateOptions,o.lookup=MathJax._.util.Options.lookup},705:function(t,o,n){"use strict";var e,r=this&&this.__extends||(e=function(t,o){return(e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,o){t.__proto__=o}||function(t,o){for(var n in o)Object.prototype.hasOwnProperty.call(o,n)&&(t[n]=o[n])})(t,o)},function(t,o){if("function"!=typeof o&&null!==o)throw new TypeError("Class extends value "+String(o)+" is not a constructor or null");function n(){this.constructor=t}e(t,o),t.prototype=null===o?Object.create(o):(n.prototype=o.prototype,new n)});Object.defineProperty(o,"__esModule",{value:!0}),o.jsdomAdaptor=o.JsdomAdaptor=void 0;var i=n(157),a=n(225),s=function(t){function o(o,n){void 0===n&&(n=null);var e=t.call(this,o)||this,r=e.constructor;return e.options=a.userOptions(a.defaultOptions({},r.OPTIONS),n),e}return r(o,t),o.prototype.fontSize=function(t){return this.options.fontSize},o.prototype.fontFamily=function(t){return this.options.fontFamily},o.prototype.nodeSize=function(t,n,e){void 0===n&&(n=1),void 0===e&&(e=null);var r=this.textContent(t),i=Array.from(r.replace(o.cjkPattern,"")).length;return[(Array.from(r).length-i)*this.options.cjkCharWidth+i*this.options.unknownCharWidth,this.options.unknownCharHeight]},o.prototype.nodeBBox=function(t){return{left:0,right:0,top:0,bottom:0}},o.OPTIONS={fontSize:16,fontFamily:"Times",cjkCharWidth:1,unknownCharWidth:.6,unknownCharHeight:.8},o.cjkPattern=new RegExp(["[","\u1100-\u115f","\u2329\u232a","\u2e80-\u303e","\u3040-\u3247","\u3250-\u4dbf","\u4e00-\ua4c6","\ua960-\ua97c","\uac00-\ud7a3","\uf900-\ufaff","\ufe10-\ufe19","\ufe30-\ufe6b","\uff01-\uff60\uffe0-\uffe6","\ud82c\udc00-\ud82c\udc01","\ud83c\ude00-\ud83c\ude51","\ud840\udc00-\ud8bf\udffd","]"].join(""),"gu"),o}(i.HTMLAdaptor);o.JsdomAdaptor=s,o.jsdomAdaptor=function(t,o){return void 0===o&&(o=null),new s((new t).window,o)}}},n={};function e(t){var r=n[t];if(void 0!==r)return r.exports;var i=n[t]={exports:{}};return o[t].call(i.exports,i,i.exports,e),i.exports}t=e(705).jsdomAdaptor,MathJax.startup&&(MathJax.startup.registerConstructor("jsdomAdaptor",(function(){return t(MathJax.config.JSDOM)})),MathJax.startup.useAdaptor("jsdomAdaptor",!0))}(); -------------------------------------------------------------------------------- /jsdom/adaptor/webpack.config.js: -------------------------------------------------------------------------------- 1 | const PACKAGE = require('mathjax-full/components/webpack.common.js'); 2 | 3 | module.exports = PACKAGE( 4 | 'adaptor', // the package to build 5 | '../node_modules/mathjax-full/js', // location of the MathJax js library 6 | ['components/src/core/lib'], // packages to link to 7 | __dirname, // our directory 8 | '.' // dist directory 9 | ); 10 | -------------------------------------------------------------------------------- /jsdom/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "MathJax-jsdom", 3 | "version": "3.2.0", 4 | "description": "MathJax v3 running in a jsdom instance", 5 | "dependencies": { 6 | "jsdom": "^16.6.0", 7 | "mathjax-full": "^3.2.0", 8 | "yargs": "^17.0.1" 9 | }, 10 | "devDependencies": { 11 | "@babel/core": "^7.14.6", 12 | "@babel/preset-env": "^7.14.5", 13 | "babel-loader": "^8.2.2", 14 | "terser-webpack-plugin": "^5.1.3", 15 | "webpack": "^5.39.1", 16 | "webpack-cli": "^4.7.2" 17 | }, 18 | "repository": { 19 | "type": "git", 20 | "url": "https://github.com/mathjax/MathJax-demos-node/" 21 | }, 22 | "scripts": { 23 | "make-adaptor": "node_modules/mathjax-full/components/bin/makeAll adaptor" 24 | }, 25 | "license": "Apache-2.0" 26 | } 27 | -------------------------------------------------------------------------------- /jsdom/tex2chtml: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * jsdom/tex2chtml 6 | * 7 | * Uses MathJax v3 to convert a TeX string to an HTML string using JSDOM. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2020 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 | // 28 | // The default TeX packages to use 29 | // 30 | const PACKAGES = 'base, autoload, require, ams, newcommand'; 31 | 32 | // 33 | // Get the command-line arguments 34 | // 35 | const argv = require('yargs') 36 | .demand(0).strict() 37 | .usage('$0 [options] "math" > file.html') 38 | .options({ 39 | inline: { 40 | boolean: true, 41 | describe: "process as inline math" 42 | }, 43 | em: { 44 | default: 16, 45 | describe: 'em-size in pixels' 46 | }, 47 | ex: { 48 | default: 8, 49 | describe: 'ex-size in pixels' 50 | }, 51 | width: { 52 | default: 80 * 16, 53 | describe: 'width of container in pixels' 54 | }, 55 | packages: { 56 | default: PACKAGES, 57 | describe: 'the packages to use, e.g. "base, ams"; use "*" to represent the default packages, e.g, "*, bbox"' 58 | }, 59 | css: { 60 | boolean: true, 61 | describe: 'output the required CSS rather than the HTML itself' 62 | }, 63 | fontURL: { 64 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 65 | describe: 'the URL to use for web fonts' 66 | }, 67 | assistiveMml: { 68 | boolean: true, 69 | default: false, 70 | describe: 'whether to include assistive MathML output' 71 | }, 72 | dist: { 73 | boolean: true, 74 | default: false, 75 | describe: 'true to use webpacked version, false to use MathJax source files' 76 | } 77 | }) 78 | .argv; 79 | 80 | // 81 | // Load MathJax and initialize MathJax and typeset the given math 82 | // 83 | require('mathjax-full').init({ 84 | // 85 | // The MathJax configuration 86 | // 87 | options: { 88 | enableAssistiveMml: argv.assistiveMml 89 | }, 90 | loader: { 91 | paths: {jsdom: `${__dirname}/adaptor`}, 92 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 93 | load: ['[jsdom]/adaptor' + (argv.dist ? '.min.js' : ''), 'tex-chtml'] 94 | }, 95 | JSDOM: require('jsdom').JSDOM, 96 | tex: { 97 | packages: argv.packages.replace('\*', PACKAGES).split(/\s*,\s*/) 98 | }, 99 | chtml: { 100 | fontURL: argv.fontURL 101 | }, 102 | startup: { 103 | typeset: false 104 | } 105 | }).then((MathJax) => { 106 | // 107 | // Typeset and display the math 108 | // 109 | MathJax.tex2chtmlPromise(argv._[0] || '', { 110 | display: !argv.inline, 111 | em: argv.em, 112 | ex: argv.ex, 113 | containerWidth: argv.width 114 | }).then((node) => { 115 | const adaptor = MathJax.startup.adaptor; 116 | // 117 | // If the --css option was specified, output the CSS, 118 | // Otherwise, output the typeset math as HTML 119 | // 120 | if (argv.css) { 121 | console.log(adaptor.textContent(MathJax.chtmlStylesheet())); 122 | } else { 123 | console.log(adaptor.outerHTML(node)); 124 | }; 125 | }); 126 | }).catch(err => console.log(err)); 127 | -------------------------------------------------------------------------------- /jsdom/tex2chtml-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * jsdom/tex2chtml-page 6 | * 7 | * Uses MathJax v3 to convert all TeX in an HTML document using JSDOM. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2020 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 | // The default TeX packages to use 28 | // 29 | const PACKAGES = 'base, autoload, require, ams, newcommand'; 30 | 31 | // 32 | // Get the command-line arguments 33 | // 34 | const argv = require('yargs') 35 | .demand(0).strict() 36 | .usage('$0 [options] file.html > converted.html') 37 | .options({ 38 | em: { 39 | default: 16, 40 | describe: 'em-size in pixels' 41 | }, 42 | ex: { 43 | default: 8, 44 | describe: 'ex-size in pixels' 45 | }, 46 | packages: { 47 | default: PACKAGES, 48 | describe: 'the packages to use, e.g. "base, ams"; use "*" to represent the default packages, e.g, "*, bbox"' 49 | }, 50 | fontURL: { 51 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 52 | describe: 'the URL to use for web fonts' 53 | }, 54 | dist: { 55 | boolean: true, 56 | default: false, 57 | describe: 'true to use webpacked version, false to use MathJax source files' 58 | } 59 | }) 60 | .argv; 61 | 62 | // 63 | // Read the HTML file 64 | // 65 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 66 | 67 | // 68 | // Load MathJax and initialize MathJax and typeset the given math 69 | // 70 | require('mathjax-full').init({ 71 | // 72 | // The MathJax configuration 73 | // 74 | loader: { 75 | paths: {jsdom: `${__dirname}/adaptor`}, 76 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 77 | load: ['[jsdom]/adaptor' + (argv.dist ? '.min.js' : ''), 'tex-chtml'] 78 | }, 79 | JSDOM: require('jsdom').JSDOM, 80 | tex: { 81 | packages: argv.packages.replace('\*', PACKAGES).split(/\s*,\s*/) 82 | }, 83 | chtml: { 84 | fontURL: argv.fontURL, 85 | exFactor: argv.ex / argv.em 86 | }, 87 | 'adaptors/liteDOM': { 88 | fontSize: argv.em 89 | }, 90 | startup: { 91 | document: htmlfile 92 | } 93 | }).then((MathJax) => { 94 | // 95 | // Display the output 96 | // 97 | const adaptor = MathJax.startup.adaptor; 98 | const html = MathJax.startup.document; 99 | if (Array.from(html.math).length === 0) adaptor.remove(html.outputJax.chtmlStyles); 100 | console.log(adaptor.doctype(html.document)); 101 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 102 | }).catch(err => console.log(err)); 103 | -------------------------------------------------------------------------------- /jsdom/tex2svg-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env node 2 | 3 | /************************************************************************* 4 | * 5 | * jsdom/tex2svg-page 6 | * 7 | * Uses MathJax v3 to convert all TeX in an HTML document using jsdom. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2020 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 packages needed for MathJax 28 | // 29 | const {mathjax} = require('mathjax-full/js/mathjax.js'); 30 | const {TeX} = require('mathjax-full/js/input/tex.js'); 31 | const {SVG} = require('mathjax-full/js/output/svg.js'); 32 | const {jsdomAdaptor} = require('mathjax-full/js/adaptors/jsdomAdaptor.js'); 33 | const {RegisterHTMLHandler} = require('mathjax-full/js/handlers/html.js'); 34 | const {AssistiveMmlHandler} = require('mathjax-full/js/a11y/assistive-mml.js'); 35 | 36 | const {AllPackages} = require('mathjax-full/js/input/tex/AllPackages.js'); 37 | 38 | const {JSDOM} = require('jsdom'); 39 | 40 | require('mathjax-full/js/util/entities/all.js'); 41 | 42 | // 43 | // Get the command-line arguments 44 | // 45 | const argv = require('yargs') 46 | .demand(1).strict() 47 | .usage('$0 [options] file.html > converted.html') 48 | .options({ 49 | packages: { 50 | default: AllPackages.sort().join(', '), 51 | describe: 'the packages to use, e.g. "base, ams"' 52 | }, 53 | fontCache: { 54 | default: 'global', 55 | describe: 'cache type: local, global, none' 56 | } 57 | }) 58 | .argv; 59 | 60 | // 61 | // Read the HTML file 62 | // 63 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 64 | 65 | // 66 | // Create DOM adaptor and register it for HTML documents 67 | // 68 | const adaptor = jsdomAdaptor(JSDOM); 69 | AssistiveMmlHandler(RegisterHTMLHandler(adaptor)); 70 | 71 | // 72 | // Create input and output jax and a document using them on the content from the HTML file 73 | // 74 | const tex = new TeX({packages: argv.packages.split(/\s*,\s*/)}); 75 | const svg = new SVG({fontCache: argv.fontCache}); 76 | const html = mathjax.document(htmlfile, {InputJax: tex, OutputJax: svg}); 77 | 78 | // 79 | // Typeset the document 80 | // 81 | html.render(); 82 | 83 | // 84 | // If no math was found on the page, remove the stylesheet and font cache (if any) 85 | // 86 | if (Array.from(html.math).length === 0) { 87 | adaptor.remove(html.outputJax.svgStyles); 88 | const cache = adaptor.elementById(adaptor.body(html.document), 'MJX-SVG-global-cache'); 89 | if (cache) adaptor.remove(cache); 90 | } 91 | 92 | // 93 | // Output the resulting HTML 94 | // 95 | console.log(adaptor.doctype(html.document)); 96 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 97 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "MathJax-demos-node", 3 | "version": "3.2.0", 4 | "description": "Demos using MathJax v3 in node", 5 | "dependencies": { 6 | "esm": "^3.2.25", 7 | "mathjax-full": "^3.2.0", 8 | "yargs": "^17.0.1" 9 | }, 10 | "devDependencies": { 11 | "@babel/core": "^7.14.6", 12 | "@babel/preset-env": "^7.14.5", 13 | "babel-loader": "^8.2.2", 14 | "terser-webpack-plugin": "^5.1.3", 15 | "webpack": "^5.39.1", 16 | "webpack-cli": "^4.7.2" 17 | }, 18 | "repository": { 19 | "type": "git", 20 | "url": "https://github.com/mathjax/MathJax-demos-node/" 21 | }, 22 | "keywords": [ 23 | "MathJax", 24 | "examples", 25 | "nodejs" 26 | ], 27 | "license": "Apache-2.0" 28 | } 29 | -------------------------------------------------------------------------------- /preload/am2chtml: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * preload/am2chtml 6 | * 7 | * Uses MathJax v3 to convert an AsciiMath string to an HTML string. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // 28 | // Get the command-line arguments 29 | // 30 | var argv = require('yargs') 31 | .demand(0).strict() 32 | .usage('$0 [options] "math" > file.html') 33 | .options({ 34 | em: { 35 | default: 16, 36 | describe: 'em-size in pixels' 37 | }, 38 | ex: { 39 | default: 8, 40 | describe: 'ex-size in pixels' 41 | }, 42 | width: { 43 | default: 80 * 16, 44 | describe: 'width of container in pixels' 45 | }, 46 | css: { 47 | boolean: true, 48 | describe: 'output the required CSS rather than the HTML itself' 49 | }, 50 | fontURL: { 51 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 52 | describe: 'the URL to use for web fonts' 53 | }, 54 | assistiveMml: { 55 | boolean: true, 56 | default: false, 57 | describe: 'whether to include assistive MathML output' 58 | } 59 | }) 60 | .argv; 61 | 62 | // 63 | // Configure MathJax 64 | // 65 | MathJax = { 66 | options: {enableAssistiveMml: argv.assistiveMml}, 67 | chtml: {fontURL: argv.fontURL}, 68 | startup: {typeset: false} 69 | }; 70 | 71 | // 72 | // Load all the needed components 73 | // 74 | require('mathjax-full/components/src/startup/lib/startup.js'); 75 | require('mathjax-full/components/src/core/core.js'); 76 | require('mathjax-full/components/src/adaptors/liteDOM/liteDOM.js'); 77 | require('mathjax-full/components/src/input/asciimath/asciimath.js'); 78 | require('mathjax-full/components/src/output/chtml/chtml.js'); 79 | require('mathjax-full/components/src/output/chtml/fonts/tex/tex.js'); 80 | require('mathjax-full/components/src/a11y/assistive-mml/assistive-mml.js'); 81 | require('mathjax-full/components/src/startup/startup.js'); 82 | 83 | // 84 | // Let MathJax know these are loaded 85 | // 86 | MathJax.loader.preLoad( 87 | 'core', 88 | 'adaptors/liteDOM', 89 | 'input/asciimath', 90 | 'output/chtml', 91 | 'output/chtml/fonts/tex', 92 | 'a11y/assistive-mml' 93 | ); 94 | 95 | // 96 | // Create the MathJax methods for the input and output that is loaded 97 | // 98 | MathJax.config.startup.ready(); 99 | 100 | // 101 | // Typeset the math from the command line 102 | // 103 | const adaptor = MathJax.startup.adaptor; 104 | const node = MathJax.asciimath2chtml(argv._[0] || '', { 105 | display: false, 106 | em: argv.em, 107 | ex: argv.ex, 108 | containerWidth: argv.width 109 | }); 110 | 111 | // 112 | // If the --css option was specified, output the CSS, 113 | // Otherwise, output the typeset math as HTML 114 | // 115 | if (argv.css) { 116 | console.log(adaptor.textContent(MathJax.chtmlStylesheet())); 117 | } else { 118 | console.log(adaptor.outerHTML(node)); 119 | }; 120 | -------------------------------------------------------------------------------- /preload/am2chtml-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * preload/am2chtml-page 6 | * 7 | * Uses MathJax v3 to convert all AsciiMath in an HTML document. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // Get the command-line arguments 28 | // 29 | var argv = require('yargs') 30 | .demand(0).strict() 31 | .usage('$0 [options] file.html > converted.html') 32 | .options({ 33 | em: { 34 | default: 16, 35 | describe: 'em-size in pixels' 36 | }, 37 | ex: { 38 | default: 8, 39 | describe: 'ex-size in pixels' 40 | }, 41 | fontURL: { 42 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 43 | describe: 'the URL to use for web fonts' 44 | } 45 | }) 46 | .argv; 47 | 48 | // 49 | // Read the HTML file 50 | // 51 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 52 | 53 | // 54 | // Configure MathJax 55 | // 56 | MathJax = { 57 | chtml: { 58 | fontURL: argv.fontURL, 59 | exFactor: argv.ex / argv.em 60 | }, 61 | 'adaptors/liteDOM': { 62 | fontSize: argv.em 63 | }, 64 | startup: { 65 | document: htmlfile, 66 | typeset: false 67 | }, 68 | loader: { 69 | require: require 70 | } 71 | }; 72 | 73 | // 74 | // Load all the needed components 75 | // 76 | require('mathjax-full/components/src/startup/lib/startup.js'); 77 | require('mathjax-full/components/src/core/core.js'); 78 | require('mathjax-full/components/src/adaptors/liteDOM/liteDOM.js'); 79 | require('mathjax-full/components/src/input/asciimath/asciimath.js'); 80 | require('mathjax-full/components/src/output/chtml/chtml.js'); 81 | require('mathjax-full/components/src/output/chtml/fonts/tex/tex.js'); 82 | require('mathjax-full/components/src/a11y/assistive-mml/assistive-mml.js'); 83 | require('mathjax-full/components/src/startup/startup.js'); 84 | 85 | // 86 | // Let MathJax know these are loaded 87 | // 88 | MathJax.loader.preLoad( 89 | 'core', 90 | 'adaptors/liteDOM', 91 | 'input/asciimath', 92 | 'output/chtml', 93 | 'output/chtml/fonts/tex', 94 | 'a11y/assistive-mml' 95 | ); 96 | 97 | // 98 | // Create the MathJax methods for the input and output that is loaded 99 | // 100 | MathJax.config.startup.ready(); 101 | 102 | // 103 | // Typeset the document 104 | // 105 | MathJax.typeset(); 106 | const adaptor = MathJax.startup.adaptor; 107 | const html = MathJax.startup.document; 108 | 109 | // 110 | // If no math was found on the page, remove the stylesheet 111 | // 112 | if (Array.from(html.math).length === 0) adaptor.remove(html.outputJax.chtmlStyles); 113 | 114 | // 115 | // Output the resulting HTML 116 | // 117 | console.log(adaptor.doctype(html.document)); 118 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 119 | -------------------------------------------------------------------------------- /preload/am2mml: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * preload/am2mml 6 | * 7 | * Uses MathJax v3 to convert an AsciiMath string to a MathML string. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // 28 | // Get the command-line arguments 29 | // 30 | var argv = require('yargs') 31 | .demand(0).strict() 32 | .usage('$0 [options] "math"') 33 | .options({ 34 | em: { 35 | default: 16, 36 | describe: 'em-size in pixels' 37 | }, 38 | ex: { 39 | default: 8, 40 | describe: 'ex-size in pixels' 41 | }, 42 | width: { 43 | default: 80 * 16, 44 | describe: 'width of container in pixels' 45 | } 46 | }) 47 | .argv; 48 | 49 | // 50 | // Configure MathJax 51 | // 52 | MathJax = { 53 | startup: {typeset: false}, 54 | loader: {require: require} 55 | }; 56 | 57 | // 58 | // Load all the needed components 59 | // 60 | require('mathjax-full/components/src/startup/lib/startup.js'); 61 | require('mathjax-full/components/src/core/core.js'); 62 | require('mathjax-full/components/src/adaptors/liteDOM/liteDOM.js'); 63 | require('mathjax-full/components/src/input/asciimath/asciimath.js'); 64 | require('mathjax-full/components/src/startup/startup.js'); 65 | 66 | // 67 | // Let MathJax know these are loaded 68 | // 69 | MathJax.loader.preLoad( 70 | 'core', 71 | 'adaptors/liteDOM', 72 | 'input/asciimath', 73 | ); 74 | 75 | // 76 | // Create the MathJax methods for the input and output that is loaded 77 | // 78 | MathJax.config.startup.ready(); 79 | 80 | // 81 | // Convert the math from the command line 82 | // 83 | const adaptor = MathJax.startup.adaptor; 84 | console.log(MathJax.asciimath2mml(argv._[0] || '', { 85 | display: false, 86 | em: argv.em, 87 | ex: argv.ex, 88 | containerWidth: argv.width 89 | })); 90 | -------------------------------------------------------------------------------- /preload/mml2chtml: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * preload/mml2chtml 6 | * 7 | * Uses MathJax v3 to convert a MathML string to an HTML string. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // 28 | // Get the command-line arguments 29 | // 30 | var argv = require('yargs') 31 | .demand(0).strict() 32 | .usage('$0 [options] "math" > file.html') 33 | .options({ 34 | inline: { 35 | boolean: true, 36 | describe: "process as inline math" 37 | }, 38 | em: { 39 | default: 16, 40 | describe: 'em-size in pixels' 41 | }, 42 | ex: { 43 | default: 8, 44 | describe: 'ex-size in pixels' 45 | }, 46 | width: { 47 | default: 80 * 16, 48 | describe: 'width of container in pixels' 49 | }, 50 | css: { 51 | boolean: true, 52 | describe: 'output the required CSS rather than the HTML itself' 53 | }, 54 | fontURL: { 55 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 56 | describe: 'the URL to use for web fonts' 57 | }, 58 | assistiveMml: { 59 | boolean: true, 60 | default: false, 61 | describe: 'whether to include assistive MathML output' 62 | } 63 | }) 64 | .argv; 65 | 66 | // 67 | // Configure MathJax 68 | // 69 | MathJax = { 70 | options: {enableAssistiveMml: argv.assistiveMml}, 71 | chtml: {fontURL: argv.fontURL}, 72 | startup: {typeset: false} 73 | }; 74 | 75 | // 76 | // Load all the needed components 77 | // 78 | require('mathjax-full/components/src/startup/lib/startup.js'); 79 | require('mathjax-full/components/src/core/core.js'); 80 | require('mathjax-full/components/src/adaptors/liteDOM/liteDOM.js'); 81 | require('mathjax-full/components/src/input/mml/mml.js'); 82 | require('mathjax-full/components/src/output/chtml/chtml.js'); 83 | require('mathjax-full/components/src/output/chtml/fonts/tex/tex.js'); 84 | require('mathjax-full/components/src/a11y/assistive-mml/assistive-mml.js'); 85 | require('mathjax-full/components/src/startup/startup.js'); 86 | 87 | // 88 | // Let MathJax know these are loaded 89 | // 90 | MathJax.loader.preLoad( 91 | 'core', 92 | 'adaptors/liteDOM', 93 | 'input/mml', 94 | 'output/chtml', 95 | 'output/chtml/fonts/tex', 96 | 'a11y/assistive-mml' 97 | ); 98 | 99 | // 100 | // Create the MathJax methods for the input and output that is loaded 101 | // 102 | MathJax.config.startup.ready(); 103 | 104 | // 105 | // Typeset the math from the command line 106 | // 107 | const adaptor = MathJax.startup.adaptor; 108 | const node = MathJax.mathml2chtml(argv._[0] || '', { 109 | display: !argv.inline, 110 | em: argv.em, 111 | ex: argv.ex, 112 | containerWidth: argv.width 113 | }); 114 | 115 | // 116 | // If the --css option was specified, output the CSS, 117 | // Otherwise, output the typeset math as HTML 118 | // 119 | if (argv.css) { 120 | console.log(adaptor.textContent(MathJax.chtmlStylesheet())); 121 | } else { 122 | console.log(adaptor.outerHTML(node)); 123 | }; 124 | -------------------------------------------------------------------------------- /preload/mml2chtml-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * preload/mml2chtml-page 6 | * 7 | * Uses MathJax v3 to convert all MathML in an HTML document. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // Get the command-line arguments 28 | // 29 | var argv = require('yargs') 30 | .demand(0).strict() 31 | .usage('$0 [options] file.html > converted.html') 32 | .options({ 33 | em: { 34 | default: 16, 35 | describe: 'em-size in pixels' 36 | }, 37 | ex: { 38 | default: 8, 39 | describe: 'ex-size in pixels' 40 | }, 41 | fontURL: { 42 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 43 | describe: 'the URL to use for web fonts' 44 | }, 45 | assistiveMml: { 46 | boolean: true, 47 | default: false, 48 | describe: 'whether to include assistive MathML output' 49 | } 50 | }) 51 | .argv; 52 | 53 | // 54 | // Read the HTML file 55 | // 56 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 57 | 58 | // 59 | // Configure MathJax 60 | // 61 | MathJax = { 62 | chtml: { 63 | fontURL: argv.fontURL, 64 | exFactor: argv.ex / argv.em 65 | }, 66 | 'adaptors/liteDOM': { 67 | fontSize: argv.em 68 | }, 69 | startup: { 70 | document: htmlfile, 71 | typeset: false 72 | }, 73 | loader: { 74 | require: require 75 | } 76 | }; 77 | 78 | // 79 | // Load all the needed components 80 | // 81 | require('mathjax-full/components/src/startup/lib/startup.js'); 82 | require('mathjax-full/components/src/core/core.js'); 83 | require('mathjax-full/components/src/adaptors/liteDOM/liteDOM.js'); 84 | require('mathjax-full/components/src/input/mml/mml.js'); 85 | require('mathjax-full/components/src/output/chtml/chtml.js'); 86 | require('mathjax-full/components/src/output/chtml/fonts/tex/tex.js'); 87 | require('mathjax-full/components/src/a11y/assistive-mml/assistive-mml.js'); 88 | require('mathjax-full/components/src/startup/startup.js'); 89 | 90 | // 91 | // Let MathJax know these are loaded 92 | // 93 | MathJax.loader.preLoad( 94 | 'core', 95 | 'adaptors/liteDOM', 96 | 'input/mml', 97 | 'output/chtml', 98 | 'output/chtml/fonts/tex', 99 | 'a11y/assistive-mml' 100 | ); 101 | 102 | // 103 | // Create the MathJax methods for the input and output that is loaded 104 | // 105 | MathJax.config.startup.ready(); 106 | 107 | // 108 | // Typeset the document 109 | // 110 | MathJax.typeset(); 111 | const adaptor = MathJax.startup.adaptor; 112 | const html = MathJax.startup.document; 113 | 114 | // 115 | // If no math was found on the page, remove the stylesheet 116 | // 117 | if (Array.from(html.math).length === 0) adaptor.remove(html.outputJax.chtmlStyles); 118 | 119 | // 120 | // Output the resulting HTML 121 | // 122 | console.log(adaptor.doctype(html.document)); 123 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 124 | -------------------------------------------------------------------------------- /preload/mml2svg: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * preload/mml2chtml 6 | * 7 | * Uses MathJax v3 to convert a MathML string to an SVG string. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // 28 | // Get the command-line arguments 29 | // 30 | var argv = require('yargs') 31 | .demand(0).strict() 32 | .usage('$0 [options] "math" > file.html') 33 | .options({ 34 | inline: { 35 | boolean: true, 36 | describe: "process as inline math" 37 | }, 38 | em: { 39 | default: 16, 40 | describe: 'em-size in pixels' 41 | }, 42 | ex: { 43 | default: 8, 44 | describe: 'ex-size in pixels' 45 | }, 46 | width: { 47 | default: 80 * 16, 48 | describe: 'width of container in pixels' 49 | }, 50 | styles: { 51 | boolean: true, 52 | default: true, 53 | describe: 'include css styles for stand-alone image' 54 | }, 55 | container: { 56 | boolean: true, 57 | describe: 'include element' 58 | }, 59 | css: { 60 | boolean: true, 61 | describe: 'output the required CSS rather than the HTML itself' 62 | }, 63 | fontCache: { 64 | boolean: true, 65 | default: true, 66 | describe: 'whether to use a local font cache or not' 67 | }, 68 | assistiveMml: { 69 | boolean: true, 70 | default: false, 71 | describe: 'whether to include assistive MathML output' 72 | } 73 | }) 74 | .argv; 75 | 76 | // 77 | // Minimal CSS needed for stand-alone image 78 | // 79 | const CSS = [ 80 | 'svg a{fill:blue;stroke:blue}', 81 | '[data-mml-node="merror"]>g{fill:red;stroke:red}', 82 | '[data-mml-node="merror"]>rect[data-background]{fill:yellow;stroke:none}', 83 | '[data-frame],[data-line]{stroke-width:70px;fill:none}', 84 | '.mjx-dashed{stroke-dasharray:140}', 85 | '.mjx-dotted{stroke-linecap:round;stroke-dasharray:0,140}', 86 | 'use[data-c]{stroke-width:3px}' 87 | ].join(''); 88 | 89 | // 90 | // Configure MathJax 91 | // 92 | MathJax = { 93 | options: {enableAssistiveMml: argv.assistiveMml}, 94 | svg: {fontCache: (argv.fontCache ? 'local' : 'none')}, 95 | startup: {typeset: false} 96 | }; 97 | 98 | // 99 | // Load the needed components 100 | // 101 | require('mathjax-full/components/src/mml-svg/mml-svg.js'); 102 | require('mathjax-full/components/src/adaptors/liteDOM/liteDOM.js'); 103 | 104 | // 105 | // Let MathJax know these are loaded 106 | // 107 | MathJax.loader.preLoad( 108 | 'mml-svg', 109 | 'adaptors/liteDOM' 110 | ); 111 | 112 | // 113 | // Create the MathJax methods for the input and output that is loaded 114 | // 115 | MathJax.config.startup.ready(); 116 | 117 | // 118 | // Typeset the math from the command line 119 | // 120 | const adaptor = MathJax.startup.adaptor; 121 | const node = MathJax.mathml2svg(argv._[0] || '', { 122 | display: !argv.inline, 123 | em: argv.em, 124 | ex: argv.ex, 125 | containerWidth: argv.width 126 | }); 127 | 128 | // 129 | // If the --css option was specified, output the CSS, 130 | // Otherwise, output the typeset math as SVG 131 | // 132 | if (argv.css) { 133 | console.log(adaptor.textContent(MathJax.svgStylesheet())); 134 | } else { 135 | let html = (argv.container ? adaptor.outerHTML(node) : adaptor.innerHTML(node)); 136 | console.log(argv.styles ? html.replace(//, ``) : html); 137 | }; 138 | -------------------------------------------------------------------------------- /preload/mml2svg-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * preload/mml2svg-page 6 | * 7 | * Uses MathJax v3 to convert all MathML in an HTML document. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // Get the command-line arguments 28 | // 29 | var argv = require('yargs') 30 | .demand(0).strict() 31 | .usage('$0 [options] file.html > converted.html') 32 | .options({ 33 | em: { 34 | default: 16, 35 | describe: 'em-size in pixels' 36 | }, 37 | ex: { 38 | default: 8, 39 | describe: 'ex-size in pixels' 40 | }, 41 | fontCache: { 42 | default: 'global', 43 | describe: 'cache type: local, global, none' 44 | }, 45 | }) 46 | .argv; 47 | 48 | // 49 | // Read the HTML file 50 | // 51 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 52 | 53 | // 54 | // Configure MathJax 55 | // 56 | MathJax = { 57 | svg: { 58 | fontCache: argv.fontCache, 59 | exFactor: argv.ex / argv.em 60 | }, 61 | 'adaptors/liteDOM': { 62 | fontSize: argv.em 63 | }, 64 | startup: { 65 | document: htmlfile, 66 | typeset: false 67 | }, 68 | loader: { 69 | require: require 70 | } 71 | }; 72 | 73 | // 74 | // Load the needed components 75 | // 76 | require('mathjax-full/components/src/mml-svg/mml-svg.js'); 77 | require('mathjax-full/components/src/adaptors/liteDOM/liteDOM.js'); 78 | 79 | // 80 | // Let MathJax know these are loaded 81 | // 82 | MathJax.loader.preLoad( 83 | 'mml-svg', 84 | 'adaptors/liteDOM' 85 | ); 86 | 87 | // 88 | // Create the MathJax methods for the input and output that is loaded 89 | // 90 | MathJax.config.startup.ready(); 91 | 92 | // 93 | // Typeset the document 94 | // 95 | MathJax.typeset(); 96 | const adaptor = MathJax.startup.adaptor; 97 | const html = MathJax.startup.document; 98 | 99 | // 100 | // If no math was found on the page, remove the stylesheet and font cache (if any) 101 | // 102 | if (Array.from(html.math).length === 0) { 103 | adaptor.remove(html.outputJax.svgStyles); 104 | const cache = adaptor.elementById(adaptor.body(html.document), 'MJX-SVG-global-cache'); 105 | if (cache) adaptor.remove(cache); 106 | } 107 | 108 | // 109 | // Output the resulting HTML 110 | // 111 | console.log(adaptor.doctype(html.document)); 112 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 113 | -------------------------------------------------------------------------------- /preload/tex2chtml: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * preload/tex2chtml 6 | * 7 | * Uses MathJax v3 to convert a TeX string to an HTML string. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // 28 | // The default TeX packages to use 29 | // 30 | const PACKAGES = 'base, autoload, require, ams, newcommand'; 31 | 32 | // 33 | // Get the command-line arguments 34 | // 35 | var argv = require('yargs') 36 | .demand(0).strict() 37 | .usage('$0 [options] "math" > file.html') 38 | .options({ 39 | inline: { 40 | boolean: true, 41 | describe: "process as inline math" 42 | }, 43 | em: { 44 | default: 16, 45 | describe: 'em-size in pixels' 46 | }, 47 | ex: { 48 | default: 8, 49 | describe: 'ex-size in pixels' 50 | }, 51 | width: { 52 | default: 80 * 16, 53 | describe: 'width of container in pixels' 54 | }, 55 | packages: { 56 | default: PACKAGES, 57 | describe: 'the packages to use, e.g. "base, ams"; use "*" to represent the default packages, e.g, "*, bbox"' 58 | }, 59 | css: { 60 | boolean: true, 61 | describe: 'output the required CSS rather than the HTML itself' 62 | }, 63 | fontURL: { 64 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 65 | describe: 'the URL to use for web fonts' 66 | }, 67 | assistiveMml: { 68 | boolean: true, 69 | default: false, 70 | describe: 'whether to include assistive MathML output' 71 | } 72 | }) 73 | .argv; 74 | 75 | // 76 | // Configure MathJax 77 | // 78 | MathJax = { 79 | options: {enableAssistiveMml: argv.assistiveMml}, 80 | tex: {packages: argv.packages.replace('\*', PACKAGES).split(/\s*,\s*/)}, 81 | chtml: {fontURL: argv.fontURL}, 82 | startup: {typeset: false} 83 | }; 84 | 85 | // 86 | // Load all the needed components 87 | // 88 | require('mathjax-full/components/src/startup/lib/startup.js'); 89 | require('mathjax-full/components/src/core/core.js'); 90 | require('mathjax-full/components/src/adaptors/liteDOM/liteDOM.js'); 91 | require('mathjax-full/components/src/input/tex-base/tex-base.js'); 92 | require('mathjax-full/components/src/input/tex/extensions/all-packages/all-packages.js'); 93 | require('mathjax-full/components/src/output/chtml/chtml.js'); 94 | require('mathjax-full/components/src/output/chtml/fonts/tex/tex.js'); 95 | require('mathjax-full/components/src/a11y/assistive-mml/assistive-mml.js'); 96 | require('mathjax-full/components/src/startup/startup.js'); 97 | 98 | // 99 | // Let MathJax know these are loaded 100 | // 101 | MathJax.loader.preLoad( 102 | 'core', 103 | 'adaptors/liteDOM', 104 | 'input/tex-base', 105 | '[tex]/all-packages', 106 | 'output/chtml', 107 | 'output/chtml/fonts/tex', 108 | 'a11y/assistive-mml' 109 | ); 110 | 111 | // 112 | // Create the MathJax methods for the input and output that is loaded 113 | // 114 | MathJax.config.startup.ready(); 115 | 116 | // 117 | // Typeset the math from the command line 118 | // 119 | const adaptor = MathJax.startup.adaptor; 120 | const node = MathJax.tex2chtml(argv._[0] || '', { 121 | display: !argv.inline, 122 | em: argv.em, 123 | ex: argv.ex, 124 | containerWidth: argv.width 125 | }); 126 | 127 | // 128 | // If the --css option was specified, output the CSS, 129 | // Otherwise, output the typeset math as HTML 130 | // 131 | if (argv.css) { 132 | console.log(adaptor.textContent(MathJax.chtmlStylesheet())); 133 | } else { 134 | console.log(adaptor.outerHTML(node)); 135 | }; 136 | -------------------------------------------------------------------------------- /preload/tex2chtml-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * preload/tex2chtml-page 6 | * 7 | * Uses MathJax v3 to convert all TeX in an HTML document. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // The default TeX packages to use 28 | // 29 | const PACKAGES = 'base, autoload, require, ams, newcommand'; 30 | 31 | // 32 | // Get the command-line arguments 33 | // 34 | var argv = require('yargs') 35 | .demand(0).strict() 36 | .usage('$0 [options] file.html > converted.html') 37 | .options({ 38 | em: { 39 | default: 16, 40 | describe: 'em-size in pixels' 41 | }, 42 | ex: { 43 | default: 8, 44 | describe: 'ex-size in pixels' 45 | }, 46 | packages: { 47 | default: PACKAGES, 48 | describe: 'the packages to use, e.g. "base, ams"; use "*" to represent the default packages, e.g, "*, bbox"' 49 | }, 50 | fontURL: { 51 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 52 | describe: 'the URL to use for web fonts' 53 | } 54 | }) 55 | .argv; 56 | 57 | // 58 | // Read the HTML file 59 | // 60 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 61 | 62 | // 63 | // Configure MathJax 64 | // 65 | MathJax = { 66 | tex: { 67 | packages: argv.packages.replace('\*', PACKAGES).split(/\s*,\s*/) 68 | }, 69 | chtml: { 70 | fontURL: argv.fontURL, 71 | exFactor: argv.ex / argv.em 72 | }, 73 | 'adaptors/liteDOM': { 74 | fontSize: argv.em 75 | }, 76 | startup: { 77 | document: htmlfile, 78 | typeset: false 79 | }, 80 | loader: { 81 | require: require 82 | } 83 | }; 84 | 85 | // 86 | // Load all the needed components 87 | // 88 | require('mathjax-full/components/src/startup/lib/startup.js'); 89 | require('mathjax-full/components/src/core/core.js'); 90 | require('mathjax-full/components/src/adaptors/liteDOM/liteDOM.js'); 91 | require('mathjax-full/components/src/input/tex-base/tex-base.js'); 92 | require('mathjax-full/components/src/input/tex/extensions/all-packages/all-packages.js'); 93 | require('mathjax-full/components/src/output/chtml/chtml.js'); 94 | require('mathjax-full/components/src/output/chtml/fonts/tex/tex.js'); 95 | require('mathjax-full/components/src/startup/startup.js'); 96 | 97 | // 98 | // Let MathJax know these are loaded 99 | // 100 | MathJax.loader.preLoad( 101 | 'core', 102 | 'adaptors/liteDOM', 103 | 'input/tex-base', 104 | '[tex]/all-packages', 105 | 'output/chtml', 106 | 'output/chtml/fonts/tex' 107 | ); 108 | 109 | // 110 | // Create the MathJax methods for the input and output that is loaded 111 | // 112 | MathJax.config.startup.ready(); 113 | 114 | // 115 | // Typeset the page 116 | // 117 | MathJax.typeset(); 118 | const adaptor = MathJax.startup.adaptor; 119 | const html = MathJax.startup.document; 120 | 121 | // 122 | // If no math was found on the page, remove the stylesheet 123 | // 124 | if (Array.from(html.math).length === 0) adaptor.remove(html.outputJax.chtmlStyles); 125 | 126 | // 127 | // Output the resulting HTML 128 | // 129 | console.log(adaptor.doctype(html.document)); 130 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 131 | -------------------------------------------------------------------------------- /preload/tex2mml: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * preload/tex2mml 6 | * 7 | * Uses MathJax v3 to convert a TeX string to a MathML string. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // 28 | // The default TeX packages to use 29 | // 30 | const PACKAGES = 'base, autoload, require, ams, newcommand'; 31 | 32 | // 33 | // Get the command-line arguments 34 | // 35 | var argv = require('yargs') 36 | .demand(0).strict() 37 | .usage('$0 [options] "math"') 38 | .options({ 39 | inline: { 40 | boolean: true, 41 | describe: "process as inline math" 42 | }, 43 | em: { 44 | default: 16, 45 | describe: 'em-size in pixels' 46 | }, 47 | ex: { 48 | default: 8, 49 | describe: 'ex-size in pixels' 50 | }, 51 | width: { 52 | default: 80 * 16, 53 | describe: 'width of container in pixels' 54 | }, 55 | packages: { 56 | default: PACKAGES, 57 | describe: 'the packages to use, e.g. "base, ams"; use "*" to represent the default packages, e.g, "*, bbox"' 58 | } 59 | }) 60 | .argv; 61 | 62 | // 63 | // Configure MathJax 64 | // 65 | MathJax = { 66 | tex: {packages: argv.packages.replace('\*', PACKAGES).split(/\s*,\s*/)}, 67 | startup: {typeset: false}, 68 | loader: {require: require} 69 | }; 70 | 71 | // 72 | // Load all the needed components 73 | // 74 | require('mathjax-full/components/src/startup/lib/startup.js'); 75 | require('mathjax-full/components/src/core/core.js'); 76 | require('mathjax-full/components/src/adaptors/liteDOM/liteDOM.js'); 77 | require('mathjax-full/components/src/input/tex-base/tex-base.js'); 78 | require('mathjax-full/components/src/input/tex/extensions/all-packages/all-packages.js'); 79 | require('mathjax-full/components/src/startup/startup.js'); 80 | 81 | // 82 | // Let MathJax know these are loaded 83 | // 84 | MathJax.loader.preLoad( 85 | 'core', 86 | 'adaptors/liteDOM', 87 | 'input/tex-base', 88 | '[tex]/all-packages' 89 | ); 90 | 91 | // 92 | // Create the MathJax methods for the input and output that is loaded 93 | // 94 | MathJax.config.startup.ready(); 95 | 96 | // 97 | // Convert the math from the command line 98 | // 99 | const adaptor = MathJax.startup.adaptor; 100 | console.log(MathJax.tex2mml(argv._[0] || '', { 101 | display: !argv.inline, 102 | em: argv.em, 103 | ex: argv.ex, 104 | containerWidth: argv.width 105 | })); 106 | -------------------------------------------------------------------------------- /preload/tex2mml-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * preload/tex2mml-page 6 | * 7 | * Uses MathJax v3 to convert all TeX in an HTML document to MathML. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2020 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 | // 28 | // The default TeX packages to use 29 | // 30 | const PACKAGES = 'base, autoload, require, ams, newcommand'; 31 | 32 | // 33 | // Get the command-line arguments 34 | // 35 | var argv = require('yargs') 36 | .demand(0).strict() 37 | .usage('$0 [options] "math"') 38 | .options({ 39 | em: { 40 | default: 16, 41 | describe: 'em-size in pixels' 42 | }, 43 | packages: { 44 | default: PACKAGES, 45 | describe: 'the packages to use, e.g. "base, ams"; use "*" to represent the default packages, e.g, "*, bbox"' 46 | } 47 | }) 48 | .argv; 49 | 50 | // 51 | // Read the HTML file 52 | // 53 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 54 | 55 | // 56 | // A renderAction to take the place of typesetting. 57 | // It renders the output to MathML instead. 58 | // 59 | function actionMML(math, doc) { 60 | const adaptor = doc.adaptor; 61 | const mml = MathJax.startup.toMML(math.root); 62 | math.typesetRoot = adaptor.firstChild(adaptor.body(adaptor.parse(mml, 'text/html'))); 63 | } 64 | 65 | // 66 | // Configure MathJax 67 | // 68 | MathJax = { 69 | tex: { 70 | packages: argv.packages.replace('\*', PACKAGES).split(/\s*,\s*/) 71 | }, 72 | loader: { 73 | require: require 74 | }, 75 | options: { 76 | renderActions: { 77 | typeset: [150, (doc) => {for (const math of doc.math) actionMML(math, doc)}, actionMML] 78 | } 79 | }, 80 | 'adaptors/liteDOM': { 81 | fontSize: argv.em 82 | }, 83 | startup: { 84 | document: htmlfile 85 | } 86 | }; 87 | 88 | // 89 | // Load all the needed components 90 | // 91 | require('mathjax-full/components/src/startup/lib/startup.js'); 92 | require('mathjax-full/components/src/core/core.js'); 93 | require('mathjax-full/components/src/adaptors/liteDOM/liteDOM.js'); 94 | require('mathjax-full/components/src/input/tex-base/tex-base.js'); 95 | require('mathjax-full/components/src/input/tex/extensions/all-packages/all-packages.js'); 96 | require('mathjax-full/components/src/startup/startup.js'); 97 | 98 | // 99 | // Let MathJax know these are loaded 100 | // 101 | MathJax.loader.preLoad( 102 | 'core', 103 | 'adaptors/liteDOM', 104 | 'input/tex-base', 105 | '[tex]/all-packages', 106 | 'startup' 107 | ); 108 | 109 | // 110 | // Create the MathJax methods for the input and output that is loaded 111 | // 112 | MathJax.config.startup.ready(); 113 | 114 | // 115 | // Render the math, then output the resulting HTML file. 116 | // 117 | const adaptor = MathJax.startup.adaptor; 118 | const html = MathJax.startup.document; 119 | 120 | html.render(); 121 | console.log(adaptor.doctype(html.document)); 122 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 123 | -------------------------------------------------------------------------------- /preload/tex2svg-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * preload/tex2svg-page 6 | * 7 | * Uses MathJax v3 to convert all TeX in an HTML document. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // The default TeX packages to use 28 | // 29 | const PACKAGES = 'base, autoload, require, ams, newcommand'; 30 | 31 | // 32 | // Get the command-line arguments 33 | // 34 | var argv = require('yargs') 35 | .demand(0).strict() 36 | .usage('$0 [options] file.html > converted.html') 37 | .options({ 38 | em: { 39 | default: 16, 40 | describe: 'em-size in pixels' 41 | }, 42 | ex: { 43 | default: 8, 44 | describe: 'ex-size in pixels' 45 | }, 46 | packages: { 47 | default: PACKAGES, 48 | describe: 'the packages to use, e.g. "base, ams"; use "*" to represent the default packages, e.g, "*, bbox"' 49 | }, 50 | fontCache: { 51 | default: 'global', 52 | describe: 'cache type: local, global, none' 53 | }, 54 | }) 55 | .argv; 56 | 57 | // 58 | // Read the HTML file 59 | // 60 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 61 | 62 | // 63 | // Configure MathJax 64 | // 65 | MathJax = { 66 | tex: { 67 | packages: argv.packages.replace('\*', PACKAGES).split(/\s*,\s*/) 68 | }, 69 | svg: { 70 | fontCache: argv.fontCache, 71 | exFactor: argv.ex / argv.em 72 | }, 73 | 'adaptors/liteDOM': { 74 | fontSize: argv.em 75 | }, 76 | startup: { 77 | document: htmlfile, 78 | typeset: false 79 | }, 80 | loader: { 81 | require: require 82 | } 83 | }; 84 | 85 | // 86 | // Load all the needed components 87 | // 88 | require('mathjax-full/components/src/startup/lib/startup.js'); 89 | require('mathjax-full/components/src/core/core.js'); 90 | require('mathjax-full/components/src/adaptors/liteDOM/liteDOM.js'); 91 | require('mathjax-full/components/src/input/tex-base/tex-base.js'); 92 | require('mathjax-full/components/src/input/tex/extensions/all-packages/all-packages.js'); 93 | require('mathjax-full/components/src/output/svg/svg.js'); 94 | require('mathjax-full/components/src/output/svg/fonts/tex/tex.js'); 95 | require('mathjax-full/components/src/a11y/assistive-mml/assistive-mml.js'); 96 | require('mathjax-full/components/src/startup/startup.js'); 97 | 98 | // 99 | // Let MathJax know these are loaded 100 | // 101 | MathJax.loader.preLoad( 102 | 'core', 103 | 'adaptors/liteDOM', 104 | 'input/tex-base', 105 | '[tex]/all-packages', 106 | 'output/svg', 107 | 'output/svg/fonts/tex', 108 | 'a11y/assistive-mml' 109 | ); 110 | 111 | // 112 | // Create the MathJax methods for the input and output that is loaded 113 | // 114 | MathJax.config.startup.ready(); 115 | 116 | // 117 | // Typeset the document 118 | // 119 | MathJax.typeset(); 120 | const adaptor = MathJax.startup.adaptor; 121 | const html = MathJax.startup.document; 122 | 123 | // 124 | // If no math was found on the page, remove the stylesheet and font cache (if any) 125 | // 126 | if (Array.from(html.math).length === 0) { 127 | adaptor.remove(html.outputJax.svgStyles); 128 | const cache = adaptor.elementById(adaptor.body(html.document), 'MJX-SVG-global-cache'); 129 | if (cache) adaptor.remove(cache); 130 | } 131 | 132 | // 133 | // Output the resulting HTML 134 | // 135 | console.log(adaptor.doctype(html.document)); 136 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 137 | -------------------------------------------------------------------------------- /puppeteer/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mathjax-puppeteer", 3 | "version": "3.2.0", 4 | "description": "MathJax v3 running in headless Chrome via puppeteer", 5 | "dependencies": { 6 | "mathjax-full": "^3.2.0", 7 | "puppeteer": "^9.0.0", 8 | "yargs": "^17.0.1" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/mathjax/MathJax-demos-node/" 13 | }, 14 | "license": "Apache-2.0" 15 | } 16 | -------------------------------------------------------------------------------- /puppeteer/puppeteer.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | MathJax in Puppeteer 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /simple/am2chtml: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * simple/am2chtml 6 | * 7 | * Uses MathJax v3 to convert an AsciiMath string to an HTML string. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // 28 | // Get the command-line arguments 29 | // 30 | var argv = require('yargs') 31 | .demand(0).strict() 32 | .usage('$0 [options] "math" > file.html') 33 | .options({ 34 | em: { 35 | default: 16, 36 | describe: 'em-size in pixels' 37 | }, 38 | ex: { 39 | default: 8, 40 | describe: 'ex-size in pixels' 41 | }, 42 | width: { 43 | default: 80 * 16, 44 | describe: 'width of container in pixels' 45 | }, 46 | css: { 47 | boolean: true, 48 | describe: 'output the required CSS rather than the HTML itself' 49 | }, 50 | fontURL: { 51 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 52 | describe: 'the URL to use for web fonts' 53 | }, 54 | assistiveMml: { 55 | boolean: true, 56 | default: false, 57 | describe: 'whether to include assistive MathML output' 58 | }, 59 | dist: { 60 | boolean: true, 61 | default: false, 62 | describe: 'true to use webpacked version, false to use MathJax source files' 63 | } 64 | }) 65 | .argv; 66 | 67 | // 68 | // Load MathJax and initialize MathJax and typeset the given math 69 | // 70 | require('mathjax-full').init({ 71 | // 72 | // The MathJax configuration 73 | // 74 | options: { 75 | enableAssistiveMml: argv.assistiveMml 76 | }, 77 | loader: { 78 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 79 | load: ['input/asciimath', 'output/chtml', 'adaptors/liteDOM', 'a11y/assistive-mml'] 80 | }, 81 | chtml: { 82 | fontURL: argv.fontURL 83 | }, 84 | startup: { 85 | typeset: false 86 | } 87 | }).then((MathJax) => { 88 | // 89 | // Typeset and display the math 90 | // 91 | MathJax.asciimath2chtmlPromise(argv._[0] || '', { 92 | display: !argv.inline, 93 | em: argv.em, 94 | ex: argv.ex, 95 | containerWidth: argv.width 96 | }).then((node) => { 97 | const adaptor = MathJax.startup.adaptor; 98 | // 99 | // If the --css option was specified, output the CSS, 100 | // Otherwise, output the typeset math as HTML 101 | // 102 | if (argv.css) { 103 | console.log(adaptor.textContent(MathJax.chtmlStylesheet())); 104 | } else { 105 | console.log(adaptor.outerHTML(node)); 106 | }; 107 | }); 108 | }).catch(err => console.log(err)); 109 | -------------------------------------------------------------------------------- /simple/am2chtml-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * simple/am2chtml-page 6 | * 7 | * Uses MathJax v3 to convert all AsciiMath in an HTML document. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // Get the command-line arguments 28 | // 29 | var argv = require('yargs') 30 | .demand(0).strict() 31 | .usage('$0 [options] file.html > converted.html') 32 | .options({ 33 | em: { 34 | default: 16, 35 | describe: 'em-size in pixels' 36 | }, 37 | ex: { 38 | default: 8, 39 | describe: 'ex-size in pixels' 40 | }, 41 | fontURL: { 42 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 43 | describe: 'the URL to use for web fonts' 44 | }, 45 | dist: { 46 | boolean: true, 47 | default: false, 48 | describe: 'true to use webpacked version, false to use MathJax source files' 49 | } 50 | }) 51 | .argv; 52 | 53 | // 54 | // Read the HTML file 55 | // 56 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 57 | 58 | // 59 | // Load MathJax and initialize MathJax and typeset the given math 60 | // 61 | require('mathjax-full').init({ 62 | // 63 | // The MathJax configuration 64 | // 65 | loader: { 66 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 67 | load: ['input/asciimath', 'output/chtml', 'adaptors/liteDOM', 'a11y/assistive-mml'] 68 | }, 69 | chtml: { 70 | fontURL: argv.fontURL, 71 | exFactor: argv.ex / argv.em 72 | }, 73 | 'adaptors/liteDOM': { 74 | fontSize: argv.em 75 | }, 76 | startup: { 77 | document: htmlfile 78 | } 79 | }).then((MathJax) => { 80 | // 81 | // Display the output 82 | // 83 | const adaptor = MathJax.startup.adaptor; 84 | const html = MathJax.startup.document; 85 | if (Array.from(html.math).length === 0) adaptor.remove(html.outputJax.chtmlStyles); 86 | console.log(adaptor.doctype(html.document)); 87 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 88 | }).catch(err => console.log(err)); 89 | -------------------------------------------------------------------------------- /simple/am2mml: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * simple/am2mml 6 | * 7 | * Uses MathJax v3 to convert an AsciiMath string to a MathML string. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // 28 | // Get the command-line arguments 29 | // 30 | var argv = require('yargs') 31 | .demand(0).strict() 32 | .usage('$0 [options] "math"') 33 | .options({ 34 | em: { 35 | default: 16, 36 | describe: 'em-size in pixels' 37 | }, 38 | ex: { 39 | default: 8, 40 | describe: 'ex-size in pixels' 41 | }, 42 | width: { 43 | default: 80 * 16, 44 | describe: 'width of container in pixels' 45 | }, 46 | dist: { 47 | boolean: true, 48 | default: false, 49 | describe: 'true to use webpacked version, false to use MathJax source files' 50 | } 51 | }) 52 | .argv; 53 | 54 | // 55 | // Load MathJax and initialize MathJax and typeset the given math 56 | // 57 | require('mathjax-full').init({ 58 | // 59 | // The MathJax configuration 60 | // 61 | loader: { 62 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 63 | load: ['input/asciimath', 'adaptors/liteDOM'] 64 | } 65 | }).then((MathJax) => { 66 | // 67 | // Convert and display the math 68 | // 69 | MathJax.asciimath2mmlPromise(argv._[0] || '', { 70 | display: !argv.inline, 71 | em: argv.em, 72 | ex: argv.ex, 73 | containerWidth: argv.width 74 | }).then(mml => console.log(mml)); 75 | }).catch(err => console.log(err)); 76 | -------------------------------------------------------------------------------- /simple/mml2chtml: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * simple/mml2chtml 6 | * 7 | * Uses MathJax v3 to convert a MathML string to an HTML string. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // 28 | // Get the command-line arguments 29 | // 30 | var argv = require('yargs') 31 | .demand(0).strict() 32 | .usage('$0 [options] "math" > file.html') 33 | .options({ 34 | inline: { 35 | boolean: true, 36 | describe: "process as inline math" 37 | }, 38 | em: { 39 | default: 16, 40 | describe: 'em-size in pixels' 41 | }, 42 | ex: { 43 | default: 8, 44 | describe: 'ex-size in pixels' 45 | }, 46 | width: { 47 | default: 80 * 16, 48 | describe: 'width of container in pixels' 49 | }, 50 | css: { 51 | boolean: true, 52 | describe: 'output the required CSS rather than the HTML itself' 53 | }, 54 | fontURL: { 55 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 56 | describe: 'the URL to use for web fonts' 57 | }, 58 | assistiveMml: { 59 | boolean: true, 60 | default: false, 61 | describe: 'whether to include assistive MathML output' 62 | }, 63 | dist: { 64 | boolean: true, 65 | default: false, 66 | describe: 'true to use webpacked version, false to use MathJax source files' 67 | } 68 | }) 69 | .argv; 70 | 71 | // 72 | // Load MathJax and initialize MathJax and typeset the given math 73 | // 74 | require('mathjax-full').init({ 75 | // 76 | // The MathJax configuration 77 | // 78 | options: { 79 | enableAssistiveMml: argv.assistiveMml 80 | }, 81 | loader: { 82 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 83 | load: ['adaptors/liteDOM', 'input/mml', 'output/chtml', 'a11y/assistive-mml', 'input/mml/entities'] 84 | }, 85 | chtml: { 86 | fontURL: argv.fontURL 87 | }, 88 | startup: { 89 | typeset: false 90 | } 91 | }).then((MathJax) => { 92 | // 93 | // Typeset and display the math 94 | // 95 | MathJax.mathml2chtmlPromise(argv._[0] || '', { 96 | display: !argv.inline, 97 | em: argv.em, 98 | ex: argv.ex, 99 | containerWidth: argv.width 100 | }).then((node) => { 101 | const adaptor = MathJax.startup.adaptor; 102 | // 103 | // If the --css option was specified, output the CSS, 104 | // Otherwise, output the typeset math as HTML 105 | // 106 | if (argv.css) { 107 | console.log(adaptor.textContent(MathJax.chtmlStylesheet())); 108 | } else { 109 | console.log(adaptor.outerHTML(node)); 110 | }; 111 | }); 112 | }).catch(err => console.log(err)); 113 | -------------------------------------------------------------------------------- /simple/mml2chtml-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * simple/mml2chtml-page 6 | * 7 | * Uses MathJax v3 to convert all MathML in an HTML document. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // Get the command-line arguments 28 | // 29 | var argv = require('yargs') 30 | .demand(0).strict() 31 | .usage('$0 [options] file.html > converted.html') 32 | .options({ 33 | em: { 34 | default: 16, 35 | describe: 'em-size in pixels' 36 | }, 37 | ex: { 38 | default: 8, 39 | describe: 'ex-size in pixels' 40 | }, 41 | fontURL: { 42 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 43 | describe: 'the URL to use for web fonts' 44 | }, 45 | dist: { 46 | boolean: true, 47 | default: false, 48 | describe: 'true to use webpacked version, false to use MathJax source files' 49 | } 50 | }) 51 | .argv; 52 | 53 | // 54 | // Read the HTML file 55 | // 56 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 57 | 58 | // 59 | // Load MathJax and initialize MathJax and typeset the given math 60 | // 61 | require('mathjax-full').init({ 62 | // 63 | // The MathJax configuration 64 | // 65 | loader: { 66 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 67 | load: ['adaptors/liteDOM', 'input/mml', 'output/chtml', 'a11y/assistive-mml', 'input/mml/entities'] 68 | }, 69 | chtml: { 70 | fontURL: argv.fontURL, 71 | exFactor: argv.ex / argv.em 72 | }, 73 | 'adaptors/liteDOM': { 74 | fontSize: argv.em 75 | }, 76 | startup: { 77 | document: htmlfile 78 | } 79 | }).then((MathJax) => { 80 | // 81 | // Display the output 82 | // 83 | const adaptor = MathJax.startup.adaptor; 84 | const html = MathJax.startup.document; 85 | if (Array.from(html.math).length === 0) adaptor.remove(html.outputJax.chtmlStyles); 86 | console.log(adaptor.doctype(html.document)); 87 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 88 | }).catch(err => console.log(err)); 89 | -------------------------------------------------------------------------------- /simple/mml2svg-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * simple/mml2svg-page 6 | * 7 | * Uses MathJax v3 to convert all MathML in an HTML document. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // Get the command-line arguments 28 | // 29 | var argv = require('yargs') 30 | .demand(0).strict() 31 | .usage('$0 [options] file.html > converted.html') 32 | .options({ 33 | em: { 34 | default: 16, 35 | describe: 'em-size in pixels' 36 | }, 37 | ex: { 38 | default: 8, 39 | describe: 'ex-size in pixels' 40 | }, 41 | fontCache: { 42 | default: 'global', 43 | describe: 'cache type: local, global, none' 44 | }, 45 | dist: { 46 | boolean: true, 47 | default: false, 48 | describe: 'true to use webpacked version, false to use MathJax source files' 49 | } 50 | }) 51 | .argv; 52 | 53 | // 54 | // Read the HTML file 55 | // 56 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 57 | 58 | // 59 | // Load MathJax and initialize MathJax and typeset the given math 60 | // 61 | require('mathjax-full').init({ 62 | // 63 | // The MathJax configuration 64 | // 65 | loader: { 66 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 67 | load: ['adaptors/liteDOM', 'input/mml', 'output/svg', 'a11y/assistive-mml', 'input/mml/entities'] 68 | }, 69 | svg: { 70 | fontCache: argv.fontCache, 71 | exFactor: argv.ex / argv.em 72 | }, 73 | 'adaptors/liteDOM': { 74 | fontSize: argv.em 75 | }, 76 | startup: { 77 | document: htmlfile 78 | } 79 | }).then((MathJax) => { 80 | // 81 | // Display the output 82 | // 83 | const adaptor = MathJax.startup.adaptor; 84 | const html = MathJax.startup.document; 85 | if (Array.from(html.math).length === 0) { 86 | adaptor.remove(html.outputJax.svgStyles); 87 | const cache = adaptor.elementById(adaptor.body(html.document), 'MJX-SVG-global-cache'); 88 | if (cache) adaptor.remove(cache); 89 | } 90 | console.log(adaptor.doctype(html.document)); 91 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 92 | }).catch(err => console.log(err)); 93 | -------------------------------------------------------------------------------- /simple/tex2chtml: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * simple/tex2chtml 6 | * 7 | * Uses MathJax v3 to convert a TeX string to an HTML string. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // 28 | // The default TeX packages to use 29 | // 30 | const PACKAGES = 'base, autoload, require, ams, newcommand'; 31 | 32 | // 33 | // Get the command-line arguments 34 | // 35 | var argv = require('yargs') 36 | .demand(0).strict() 37 | .usage('$0 [options] "math" > file.html') 38 | .options({ 39 | inline: { 40 | boolean: true, 41 | describe: "process as inline math" 42 | }, 43 | em: { 44 | default: 16, 45 | describe: 'em-size in pixels' 46 | }, 47 | ex: { 48 | default: 8, 49 | describe: 'ex-size in pixels' 50 | }, 51 | width: { 52 | default: 80 * 16, 53 | describe: 'width of container in pixels' 54 | }, 55 | packages: { 56 | default: PACKAGES, 57 | describe: 'the packages to use, e.g. "base, ams"; use "*" to represent the default packages, e.g, "*, bbox"' 58 | }, 59 | css: { 60 | boolean: true, 61 | describe: 'output the required CSS rather than the HTML itself' 62 | }, 63 | fontURL: { 64 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 65 | describe: 'the URL to use for web fonts' 66 | }, 67 | assistiveMml: { 68 | boolean: true, 69 | default: false, 70 | describe: 'whether to include assistive MathML output' 71 | }, 72 | dist: { 73 | boolean: true, 74 | default: false, 75 | describe: 'true to use webpacked version, false to use MathJax source files' 76 | } 77 | }) 78 | .argv; 79 | 80 | // 81 | // Load MathJax and initialize MathJax and typeset the given math 82 | // 83 | require('mathjax-full').init({ 84 | // 85 | // The MathJax configuration 86 | // 87 | options: { 88 | enableAssistiveMml: argv.assistiveMml 89 | }, 90 | loader: { 91 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 92 | load: ['adaptors/liteDOM', 'tex-chtml'] 93 | }, 94 | tex: { 95 | packages: argv.packages.replace('\*', PACKAGES).split(/\s*,\s*/) 96 | }, 97 | chtml: { 98 | fontURL: argv.fontURL 99 | }, 100 | startup: { 101 | typeset: false 102 | } 103 | }).then((MathJax) => { 104 | // 105 | // Typeset and display the math 106 | // 107 | MathJax.tex2chtmlPromise(argv._[0] || '', { 108 | display: !argv.inline, 109 | em: argv.em, 110 | ex: argv.ex, 111 | containerWidth: argv.width 112 | }).then((node) => { 113 | const adaptor = MathJax.startup.adaptor; 114 | // 115 | // If the --css option was specified, output the CSS, 116 | // Otherwise, output the typeset math as HTML 117 | // 118 | if (argv.css) { 119 | console.log(adaptor.textContent(MathJax.chtmlStylesheet())); 120 | } else { 121 | console.log(adaptor.outerHTML(node)); 122 | }; 123 | }); 124 | }).catch(err => console.log(err)); 125 | -------------------------------------------------------------------------------- /simple/tex2chtml-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * simple/tex2chtml-page 6 | * 7 | * Uses MathJax v3 to convert all TeX in an HTML document. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // The default TeX packages to use 28 | // 29 | const PACKAGES = 'base, autoload, require, ams, newcommand'; 30 | 31 | // 32 | // Get the command-line arguments 33 | // 34 | var argv = require('yargs') 35 | .demand(0).strict() 36 | .usage('$0 [options] file.html > converted.html') 37 | .options({ 38 | em: { 39 | default: 16, 40 | describe: 'em-size in pixels' 41 | }, 42 | ex: { 43 | default: 8, 44 | describe: 'ex-size in pixels' 45 | }, 46 | packages: { 47 | default: PACKAGES, 48 | describe: 'the packages to use, e.g. "base, ams"; use "*" to represent the default packages, e.g, "*, bbox"' 49 | }, 50 | fontURL: { 51 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 52 | describe: 'the URL to use for web fonts' 53 | }, 54 | dist: { 55 | boolean: true, 56 | default: false, 57 | describe: 'true to use webpacked version, false to use MathJax source files' 58 | } 59 | }) 60 | .argv; 61 | 62 | // 63 | // Read the HTML file 64 | // 65 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 66 | 67 | // 68 | // Load MathJax and initialize MathJax and typeset the given math 69 | // 70 | require('mathjax-full').init({ 71 | // 72 | // The MathJax configuration 73 | // 74 | loader: { 75 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 76 | load: ['adaptors/liteDOM', 'tex-chtml'] 77 | }, 78 | tex: { 79 | packages: argv.packages.replace('\*', PACKAGES).split(/\s*,\s*/) 80 | }, 81 | chtml: { 82 | fontURL: argv.fontURL, 83 | exFactor: argv.ex / argv.em 84 | }, 85 | 'adaptors/liteDOM': { 86 | fontSize: argv.em 87 | }, 88 | startup: { 89 | document: htmlfile 90 | } 91 | }).then((MathJax) => { 92 | // 93 | // Display the output 94 | // 95 | const adaptor = MathJax.startup.adaptor; 96 | const html = MathJax.startup.document; 97 | if (Array.from(html.math).length === 0) adaptor.remove(html.outputJax.chtmlStyles); 98 | console.log(adaptor.doctype(html.document)); 99 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 100 | }).catch(err => console.log(err)); 101 | -------------------------------------------------------------------------------- /simple/tex2mml: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * simple/tex2mml 6 | * 7 | * Uses MathJax v3 to convert a TeX string to a MathML string. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // 28 | // The default TeX packages to use 29 | // 30 | const PACKAGES = 'base, autoload, ams, newcommand, require'; 31 | 32 | // 33 | // Get the command-line arguments 34 | // 35 | var argv = require('yargs') 36 | .demand(0).strict() 37 | .usage('$0 [options] "math"') 38 | .options({ 39 | inline: { 40 | boolean: true, 41 | describe: "process as inline math" 42 | }, 43 | em: { 44 | default: 16, 45 | describe: 'em-size in pixels' 46 | }, 47 | ex: { 48 | default: 8, 49 | describe: 'ex-size in pixels' 50 | }, 51 | width: { 52 | default: 80 * 16, 53 | describe: 'width of container in pixels' 54 | }, 55 | packages: { 56 | default: PACKAGES, 57 | describe: 'the packages to use, e.g. "base, ams"; use "*" to represent the default packages, e.g, "*, bbox"' 58 | }, 59 | dist: { 60 | boolean: true, 61 | default: false, 62 | describe: 'true to use webpacked version, false to use MathJax source files' 63 | } 64 | }) 65 | .argv; 66 | 67 | // 68 | // Load MathJax and initialize MathJax and typeset the given math 69 | // 70 | require('mathjax-full').init({ 71 | // 72 | // The MathJax configuration 73 | // 74 | loader: { 75 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 76 | load: ['input/tex-full', 'adaptors/liteDOM'] 77 | }, 78 | tex: { 79 | packages: argv.packages.replace('\*', PACKAGES).split(/\s*,\s*/) 80 | } 81 | }).then((MathJax) => { 82 | // 83 | // Convert and display the math 84 | // 85 | MathJax.tex2mmlPromise(argv._[0] || '', { 86 | display: !argv.inline, 87 | em: argv.em, 88 | ex: argv.ex, 89 | containerWidth: argv.width 90 | }).then(mml => console.log(mml)); 91 | }).catch(err => console.log(err)); 92 | -------------------------------------------------------------------------------- /simple/tex2mml-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * simple/tex2mml-page 6 | * 7 | * Uses MathJax v3 to convert all TeX in an HTML document to MathML. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2020 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 | // 28 | // The default TeX packages to use 29 | // 30 | const PACKAGES = 'base, autoload, ams, newcommand, require'; 31 | 32 | // 33 | // Get the command-line arguments 34 | // 35 | var argv = require('yargs') 36 | .demand(0).strict() 37 | .usage('$0 [options] "math"') 38 | .options({ 39 | em: { 40 | default: 16, 41 | describe: 'em-size in pixels' 42 | }, 43 | packages: { 44 | default: PACKAGES, 45 | describe: 'the packages to use, e.g. "base, ams"; use "*" to represent the default packages, e.g, "*, bbox"' 46 | }, 47 | dist: { 48 | boolean: true, 49 | default: false, 50 | describe: 'true to use webpacked version, false to use MathJax source files' 51 | } 52 | }) 53 | .argv; 54 | 55 | // 56 | // Read the HTML file 57 | // 58 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 59 | 60 | // 61 | // A renderAction to take the place of typesetting. 62 | // It renders the output to MathML instead. 63 | // 64 | function actionMML(math, doc) { 65 | const adaptor = doc.adaptor; 66 | const mml = MathJax.startup.toMML(math.root); 67 | math.typesetRoot = adaptor.firstChild(adaptor.body(adaptor.parse(mml, 'text/html'))); 68 | } 69 | 70 | // 71 | // Load MathJax and initialize MathJax and typeset the given math 72 | // 73 | require('mathjax-full').init({ 74 | // 75 | // The MathJax configuration 76 | // 77 | loader: { 78 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 79 | load: ['input/tex-full', 'adaptors/liteDOM'] 80 | }, 81 | options: { 82 | renderActions: { 83 | typeset: [150, (doc) => {for (const math of doc.math) actionMML(math, doc)}, actionMML] 84 | } 85 | }, 86 | tex: { 87 | packages: argv.packages.replace('\*', PACKAGES).split(/\s*,\s*/) 88 | }, 89 | 'adaptors/liteDOM': { 90 | fontSize: argv.em 91 | }, 92 | startup: { 93 | document: htmlfile 94 | } 95 | }).then((MathJax) => { 96 | const adaptor = MathJax.startup.adaptor; 97 | const html = MathJax.startup.document; 98 | html.render(); 99 | console.log(adaptor.doctype(html.document)); 100 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 101 | }).catch(err => console.log(err)); 102 | -------------------------------------------------------------------------------- /simple/tex2svg-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * simple/tex2svg-page 6 | * 7 | * Uses MathJax v3 to convert all TeX in an SVG document. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // The default TeX packages to use 28 | // 29 | const PACKAGES = 'base, autoload, require, ams, newcommand'; 30 | 31 | // 32 | // Get the command-line arguments 33 | // 34 | var argv = require('yargs') 35 | .demand(0).strict() 36 | .usage('$0 [options] file.html > converted.html') 37 | .options({ 38 | em: { 39 | default: 16, 40 | describe: 'em-size in pixels' 41 | }, 42 | ex: { 43 | default: 8, 44 | describe: 'ex-size in pixels' 45 | }, 46 | packages: { 47 | default: PACKAGES, 48 | describe: 'the packages to use, e.g. "base, ams"; use "*" to represent the default packages, e.g, "*, bbox"' 49 | }, 50 | fontCache: { 51 | default: 'global', 52 | describe: 'cache type: local, global, or none' 53 | }, 54 | dist: { 55 | boolean: true, 56 | default: false, 57 | describe: 'true to use webpacked version, false to use MathJax source files' 58 | } 59 | }) 60 | .argv; 61 | 62 | // 63 | // Read the HTML file 64 | // 65 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 66 | 67 | // 68 | // Load MathJax and initialize MathJax and typeset the given math 69 | // 70 | require('mathjax-full').init({ 71 | // 72 | // The MathJax configuration 73 | // 74 | loader: { 75 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 76 | load: ['adaptors/liteDOM', 'tex-svg'] 77 | }, 78 | tex: { 79 | packages: argv.packages.replace('\*', PACKAGES).split(/\s*,\s*/) 80 | }, 81 | svg: { 82 | fontCache: argv.fontCache, 83 | exFactor: argv.ex / argv.em 84 | }, 85 | 'adaptors/liteDOM': { 86 | fontSize: argv.em 87 | }, 88 | startup: { 89 | document: htmlfile 90 | } 91 | }).then((MathJax) => { 92 | // 93 | // Display the output 94 | // 95 | const adaptor = MathJax.startup.adaptor; 96 | const html = MathJax.startup.document; 97 | if (Array.from(html.math).length === 0) { 98 | adaptor.remove(html.outputJax.svgStyles); 99 | const cache = adaptor.elementById(adaptor.body(html.document), 'MJX-SVG-global-cache'); 100 | if (cache) adaptor.remove(cache); 101 | } 102 | console.log(adaptor.doctype(html.document)); 103 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 104 | }).catch(err => console.log(err)); 105 | -------------------------------------------------------------------------------- /speech/README.md: -------------------------------------------------------------------------------- 1 | # Speech Generation 2 | 3 | This example shows how to use MathJax's `semantic-enrich` extension to generate and attach speech strings to MathJax output. 4 | 5 | The main steps needed for this are to: 6 | 7 | 1. Load the `a11y/semantic-enrich` extension, 8 | 2. Configure the extension to generate deep or shallow speech strings, and 9 | 3. Add a custom `renderAction` to the math document that removes all non-speech attributes added by the enrichment process. 10 | 11 | Most of the examples in this directory use the MathJax [components approach](../component) to accomplish this, but the `mml2svg` example uses the [direct approach](../direct) in order to illustrate that as well. 12 | 13 | ## Component-Based Example 14 | 15 | We use the [`tex2chtml`](tex2chtml) code as an example. The key pieces are the following lines from the configuration: 16 | 17 | ```js 18 | loader: { 19 | paths: { 20 | mathjax: 'mathjax-full/es5' 21 | }, 22 | load: ['adaptors/liteDOM', 'a11y/semantic-enrich'] 23 | }, 24 | options: { 25 | enableAssistiveMml: false, 26 | sre: { 27 | speech: argv.speech 28 | }, 29 | renderActions: require('./action.js').speechAction 30 | }, 31 | ``` 32 | 33 | The `loader` section sets up the path to the directory with the MathJax components, which is here relative to the `node_modules` directory and includes the `a11y/semantic-enrich` component in the list to be loaded. Note, that unlike in previous versions of MathJax there is no need to set up the path to speech-rule-engine (SRE) as this is internally handled by MathJax. 34 | 35 | The `options` section sets the document options to include the speech level as a `sre` instruction (given by the `--speech` command-line option), and adds a custom `renderAction` to handle the `data-semantic` attributes generated by SRE. Because this code is used by all the examples in this directory, it is stored in a separate file, [`action.js`](action.js). It defines a function that removes any attribute that starts with `data-semantic-` except for `data-semantic-speech`, since the semantic enrichment adds lots of data about the structure of the expression in these attributes. The code is commented, so see that for details. The `enableAssistiveMml` option is disabled, since the speech string for assistive technology is being included, so there is no need for the assistive MathML. In addition the `--sre` command-line option allows you to pass additional pairs of arguments to SRE, e.g., to change locale, rule set, or rule preference settings. 36 | 37 | The rest of the file is the same as the standard `tex2chtml` using components. The `renderAction` configuration is all that is needed for that action to be taken automatically during the usual typesetting or conversion calls. 38 | 39 | ## Direct-Import Example 40 | 41 | The [`mml2svg`](mml2svg) example uses the [direct import](../direct) approach. The key additions in this case are 42 | 43 | ```js 44 | const {EnrichHandler} = require('mathjax-full/js/a11y/semantic-enrich.js'); 45 | ``` 46 | 47 | which loads a function used to augment the HTML handler to include the enrichment functions. It is used in 48 | 49 | ```js 50 | EnrichHandler(RegisterHTMLHandler(adaptor), new MathML()); 51 | ``` 52 | 53 | to modify the handler that is register by `RegisterHTMLHandler()`, creating a new handler that has the enrichment actions included. You need to provide a MathML input handler so that it can parse the serialized MathML returned by the SRE. 54 | 55 | Finally, the lines 56 | 57 | ```js 58 | const html = MathJax.document('', { 59 | InputJax: mml, 60 | OutputJax: svg, 61 | enrichSpeech: argv.speech, 62 | renderActions: require('./action.js').speechAction 63 | }); 64 | ``` 65 | 66 | set the speech-generation level (`enrichSpeech`) and adds the `renderAction` for removing the unwanted `data-semantic-` attributes. 67 | 68 | As above, the rest of the program just uses the standard conversion calls, and the `renderAction` is called automatically at the right time. 69 | 70 | -------------------------------------------------------------------------------- /speech/mml2mml: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * speech/mml2mml 6 | * 7 | * Uses MathJax v3 to convert a MathML string to a MathML string with alttext. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // 28 | // Get the command-line arguments 29 | // 30 | var argv = require('yargs') 31 | .demand(0).strict() 32 | .usage('$0 [options] "math" > file.html') 33 | .options({ 34 | inline: { 35 | boolean: true, 36 | describe: "process as inline math" 37 | }, 38 | sre: { 39 | array: true, 40 | nargs: 2, 41 | describe: 'SRE flags as key value pairs, e.g., "--sre locale de --sre domain clearspeak" generates speech in German with clearspeak rules' 42 | }, 43 | speech: { 44 | default: 'shallow', 45 | describe: 'level of speech: deep, shallow, none' 46 | }, 47 | dist: { 48 | boolean: true, 49 | default: false, 50 | describe: 'true to use webpacked version, false to use mathjax3 source files' 51 | } 52 | }) 53 | .argv; 54 | 55 | const action = require('./action.js'); 56 | 57 | // 58 | // Add a render action to move the computed speech into the alttext attribute. 59 | // 60 | function moveSpeech(math) { 61 | let alttext = ''; 62 | math.root.walkTree(node => { 63 | const attributes = node.attributes.getAllAttributes(); 64 | if (!alttext && attributes['data-semantic-speech']) { 65 | alttext = attributes['data-semantic-speech']; 66 | } 67 | delete attributes['data-semantic-speech']; 68 | }); 69 | math.root.attributes.getAllAttributes()['alttext'] = alttext; 70 | }; 71 | 72 | action.speechAction.alttext = [ 73 | 99, 74 | (doc) => { 75 | for (const math of doc.math) { 76 | moveSpeech(math); 77 | } 78 | }, 79 | (math, doc) => { 80 | moveSpeech(math); 81 | } 82 | ]; 83 | 84 | // 85 | // Configure MathJax 86 | // 87 | MathJax = { 88 | loader: { 89 | paths: {mathjax: 'mathjax-full/es5'}, 90 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 91 | require: require, 92 | load: ['input/mml', 'adaptors/liteDOM', 'a11y/semantic-enrich'] 93 | }, 94 | options: { 95 | sre: {speech: argv.speech}, 96 | renderActions: action.speechAction 97 | } 98 | }; 99 | 100 | // 101 | // Load the MathJax startup module 102 | // 103 | require('mathjax-full/' + (argv.dist ? 'es5' : 'components/src/startup') + '/startup.js'); 104 | 105 | // 106 | // Filling the sre options from command line 107 | // 108 | action.sreconfig(argv.sre); 109 | 110 | // 111 | // Wait for MathJax to start up, and then typeset the math 112 | // 113 | MathJax.startup.promise.then(() => { 114 | MathJax.mathml2mmlPromise(argv._[0] || '', { 115 | display: !argv.inline, 116 | em: argv.em, 117 | ex: argv.ex, 118 | containerWidth: argv.width 119 | }).then(mml => console.log(mml)); 120 | }).catch(err => console.log(err)); 121 | -------------------------------------------------------------------------------- /speech/mml2svg-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * speech/mml2svg-page 6 | * 7 | * Uses MathJax v3 to convert all MathML in an HTML document to SVG with speech. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // Get the command-line arguments 28 | // 29 | var argv = require('yargs') 30 | .demand(0).strict() 31 | .usage('$0 [options] file.html > converted.html') 32 | .options({ 33 | em: { 34 | default: 16, 35 | describe: 'em-size in pixels' 36 | }, 37 | ex: { 38 | default: 8, 39 | describe: 'ex-size in pixels' 40 | }, 41 | speech: { 42 | default: 'shallow', 43 | describe: 'level of speech: deep, shallow, none' 44 | }, 45 | sre: { 46 | array: true, 47 | nargs: 2, 48 | describe: 'SRE flags as key value pairs, e.g., "--sre locale de --sre domain clearspeak" generates speech in German with clearspeak rules' 49 | }, 50 | fontCache: { 51 | default: 'global', 52 | describe: 'cache type: local, global, none' 53 | }, 54 | dist: { 55 | boolean: true, 56 | default: false, 57 | describe: 'true to use webpacked version, false to use mathjax3 source files' 58 | } 59 | }) 60 | .argv; 61 | 62 | // 63 | // Read the HTML file 64 | // 65 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 66 | 67 | // 68 | // Configure MathJax 69 | // 70 | MathJax = { 71 | loader: { 72 | paths: {mathjax: 'mathjax-full/es5'}, 73 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 74 | require: require, 75 | load: ['adaptors/liteDOM', 'a11y/semantic-enrich'] 76 | }, 77 | svg: { 78 | fontCache: argv.fontCache, 79 | exFactor: argv.ex / argv.em 80 | }, 81 | 'adaptors/liteDOM': { 82 | fontSize: argv.em 83 | }, 84 | options: { 85 | enableAssistiveMml: false, 86 | sre: {speech: argv.speech}, 87 | renderActions: require('./action.js').speechAction 88 | }, 89 | startup: { 90 | document: htmlfile 91 | } 92 | } 93 | 94 | // 95 | // Load the MathJax startup module 96 | // 97 | require('mathjax-full/' + (argv.dist ? 'es5' : 'components/src/mml-svg') + '/mml-svg.js'); 98 | 99 | // 100 | // Wait for MathJax to start up, and then typeset the math 101 | // 102 | MathJax.startup.promise.then(() => { 103 | const adaptor = MathJax.startup.adaptor; 104 | const html = MathJax.startup.document; 105 | if (Array.from(html.math).length === 0) { 106 | adaptor.remove(html.outputJax.svgStyles); 107 | const cache = adaptor.elementById(adaptor.body(html.document), 'MJX-SVG-global-cache'); 108 | if (cache) adaptor.remove(cache); 109 | } 110 | console.log(adaptor.doctype(html.document)); 111 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 112 | }).catch(err => console.log(err)); 113 | -------------------------------------------------------------------------------- /speech/tex2chtml-speech-page: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * component/tex2chtml-speech-page 6 | * 7 | * Uses MathJax v3 to convert all TeX in an HTML document. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // The default TeX packages to use 28 | // 29 | const PACKAGES = 'base, autoload, require, ams, newcommand'; 30 | 31 | // 32 | // Get the command-line arguments 33 | // 34 | var argv = require('yargs') 35 | .demand(0).strict() 36 | .usage('$0 [options] file.html > converted.html') 37 | .options({ 38 | em: { 39 | default: 16, 40 | describe: 'em-size in pixels' 41 | }, 42 | ex: { 43 | default: 8, 44 | describe: 'ex-size in pixels' 45 | }, 46 | packages: { 47 | default: PACKAGES, 48 | describe: 'the packages to use, e.g. "base, ams"; use "*" to represent the default packages, e.g, "*, bbox"' 49 | }, 50 | fontURL: { 51 | default: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/output/chtml/fonts/woff-v2', 52 | describe: 'the URL to use for web fonts' 53 | }, 54 | sre: { 55 | array: true, 56 | nargs: 2, 57 | describe: 'SRE flags as key value pairs, e.g., "--sre locale de --sre domain clearspeak" generates speech in German with clearspeak rules' 58 | }, 59 | speech: { 60 | default: 'shallow', 61 | describe: 'level of speech: deep, shallow, none' 62 | }, 63 | dist: { 64 | boolean: true, 65 | default: false, 66 | describe: 'true to use webpacked version, false to use MathJax source files' 67 | } 68 | }) 69 | .argv; 70 | 71 | // 72 | // Read the HTML file 73 | // 74 | const htmlfile = require('fs').readFileSync(argv._[0], 'utf8'); 75 | 76 | const action = require('./action.js'); 77 | 78 | // 79 | // Configure MathJax 80 | // 81 | MathJax = { 82 | options: { 83 | enableAssistiveMml: false, 84 | enableEnrichment: true, 85 | renderActions: action.speechAction, 86 | sre: { 87 | speech: argv.speech 88 | } 89 | }, 90 | loader: { 91 | paths: {mathjax: 'mathjax-full/es5'}, 92 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 93 | require: require, 94 | load: ['adaptors/liteDOM', 'a11y/semantic-enrich'] 95 | }, 96 | tex: { 97 | packages: argv.packages.replace('\*', PACKAGES).split(/\s*,\s*/) 98 | }, 99 | chtml: { 100 | fontURL: argv.fontURL, 101 | exFactor: argv.ex / argv.em 102 | }, 103 | 'adaptors/liteDOM': { 104 | fontSize: argv.em 105 | }, 106 | startup: { 107 | document: htmlfile 108 | } 109 | } 110 | 111 | // 112 | // Load the MathJax startup module 113 | // 114 | require('mathjax-full/' + (argv.dist ? 'es5' : 'components/src/tex-chtml-full-speech') + '/tex-chtml-full-speech.js'); 115 | 116 | // 117 | // Filling the sre options from command line 118 | // 119 | action.sreconfig(argv.sre); 120 | 121 | // 122 | // Wait for MathJax to start up, and then typeset the math 123 | // 124 | MathJax.startup.promise.then(() => { 125 | const adaptor = MathJax.startup.adaptor; 126 | const html = MathJax.startup.document; 127 | if (Array.from(html.math).length === 0) adaptor.remove(html.outputJax.chtmlStyles); 128 | console.log(adaptor.doctype(html.document)); 129 | console.log(adaptor.outerHTML(adaptor.root(html.document))); 130 | }).catch(err => console.log(err)); 131 | -------------------------------------------------------------------------------- /speech/tex2mml: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * speech/tex2mml 6 | * 7 | * Uses MathJax v3 to convert a TeX string to a MathML string with speech. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // 28 | // The default TeX packages to use 29 | // 30 | const PACKAGES = 'base, autoload, require, ams, newcommand'; 31 | 32 | // 33 | // Get the command-line arguments 34 | // 35 | var argv = require('yargs') 36 | .demand(0).strict() 37 | .usage('$0 [options] "math" > file.html') 38 | .options({ 39 | inline: { 40 | boolean: true, 41 | describe: "process as inline math" 42 | }, 43 | packages: { 44 | default: PACKAGES, 45 | describe: 'the packages to use, e.g. "base, ams"; use "*" to represent the default packages, e.g, "*, bbox"' 46 | }, 47 | sre: { 48 | array: true, 49 | nargs: 2, 50 | describe: 'SRE flags as key value pairs, e.g., "--sre locale de --sre domain clearspeak" generates speech in German with clearspeak rules' 51 | }, 52 | speech: { 53 | default: 'shallow', 54 | describe: 'level of speech: deep, shallow, none' 55 | }, 56 | dist: { 57 | boolean: true, 58 | default: false, 59 | describe: 'true to use webpacked version, false to use mathjax3 source files' 60 | } 61 | }) 62 | .argv; 63 | 64 | const action = require('./action.js'); 65 | 66 | // 67 | // Configure MathJax 68 | // 69 | MathJax = { 70 | loader: { 71 | paths: {mathjax: 'mathjax-full/es5'}, 72 | source: (argv.dist ? {} : require('mathjax-full/components/src/source.js').source), 73 | require: require, 74 | load: ['input/tex-full', 'adaptors/liteDOM', 'a11y/semantic-enrich'] 75 | }, 76 | tex: { 77 | packages: argv.packages.replace('\*', PACKAGES).split(/\s*,\s*/) 78 | }, 79 | options: { 80 | sre: {speech: argv.speech}, 81 | renderActions: action.speechAction 82 | } 83 | } 84 | 85 | // 86 | // Load the MathJax startup module 87 | // 88 | require('mathjax-full/' + (argv.dist ? 'es5' : 'components/src/startup') + '/startup.js'); 89 | 90 | // 91 | // Filling the sre options from command line 92 | // 93 | action.sreconfig(argv.sre); 94 | 95 | // 96 | // Wait for MathJax to start up, and then typeset the math 97 | // 98 | MathJax.startup.promise.then(() => { 99 | MathJax.tex2mmlPromise(argv._[0] || '', { 100 | display: !argv.inline, 101 | em: argv.em, 102 | ex: argv.ex, 103 | containerWidth: argv.width 104 | }).then(mml => console.log(mml)); 105 | }).catch(err => console.log(err)); 106 | -------------------------------------------------------------------------------- /speech/tex2speech: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env -S node -r esm 2 | 3 | /************************************************************************* 4 | * 5 | * speech/tex2speech 6 | * 7 | * Uses MathJax v3 to convert a TeX string to a MathML string with speech. 8 | * 9 | * ---------------------------------------------------------------------- 10 | * 11 | * Copyright (c) 2019 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 | // 28 | // Load the packages needed for MathJax 29 | // 30 | const {mathjax} = require('mathjax-full/js/mathjax.js'); 31 | const {TeX} = require('mathjax-full/js/input/tex.js'); 32 | const {Sre} = require('mathjax-full/js/a11y/sre.js'); 33 | const {liteAdaptor} = require('mathjax-full/js/adaptors/liteAdaptor.js'); 34 | const {RegisterHTMLHandler} = require('mathjax-full/js/handlers/html.js'); 35 | const {SerializedMmlVisitor} = require('mathjax-full/js/core/MmlTree/SerializedMmlVisitor.js'); 36 | 37 | 38 | // 39 | // The default TeX packages to use 40 | // 41 | const PACKAGES = 'base, autoload, require, ams, newcommand'; 42 | 43 | 44 | // 45 | // Get the command-line arguments 46 | // 47 | var argv = require('yargs') 48 | .demand(0).strict() 49 | .usage('$0 [options] "math" > file.txt') 50 | .options({ 51 | inline: { 52 | boolean: true, 53 | describe: "process as inline math; generally not relevant" 54 | }, 55 | packages: { 56 | default: PACKAGES, 57 | describe: 'the packages to use, e.g. "base, ams"; use "*" to represent the default packages, e.g, "*, bbox"' 58 | }, 59 | sre: { 60 | array: true, 61 | nargs: 2, 62 | describe: "SRE flags as key value pairs" 63 | }, 64 | dist: { 65 | boolean: true, 66 | default: false, 67 | describe: 'true to use webpacked version, false to use mathjax3 source files' 68 | } 69 | }) 70 | .argv; 71 | 72 | const action = require('./action.js'); 73 | 74 | // 75 | // Create DOM adaptor and register it for HTML documents 76 | // 77 | const adaptor = liteAdaptor(); 78 | const handler = RegisterHTMLHandler(adaptor); 79 | 80 | // 81 | // Create input jax only and a document 82 | // 83 | const tex = new TeX({packages: argv.packages.split(/\s*,\s*/)}); 84 | const html = mathjax.document('', {InputJax: tex}); 85 | 86 | // 87 | // Typeset the math from the command line 88 | // 89 | const node = html.convert(argv._[0] || '', { 90 | display: !argv.inline, 91 | em: argv.em, 92 | ex: argv.ex, 93 | containerWidth: argv.width 94 | }); 95 | 96 | // 97 | // Create a visitor to serialize the node to Mml. 98 | // 99 | const visitor = new SerializedMmlVisitor(); 100 | const mml = visitor.visitTree(node, html); 101 | 102 | // 103 | // Get feature vector for SRE setup. If necessary, compute the path to the 104 | // locale JSON files explicitly. 105 | // 106 | const feature = action.dataPairs(argv.sre); 107 | feature.json = feature.json ? feature.json : 108 | require.resolve('mathjax-full/es5/sre/mathmaps/base.json').replace(/\/base\.json$/, ''); 109 | 110 | // 111 | // Run SRE, by first setting it up and then compute the speech string. 112 | // 113 | // Note, that after setting up the engine, we need to wait for it to be ready, 114 | // so it has finished loading its rule files. 115 | // 116 | Sre.setupEngine(feature) 117 | .then(() => Sre.sreReady()) 118 | .then(() => console.log(Sre.toSpeech(mml))) 119 | .catch(err => console.log(err)); 120 | --------------------------------------------------------------------------------