mathjax/MathJax-demos-node

Cannot process german umlaute when using demo example tex2svg-page

cjohn001 opened this issue · 2 comments

Hello together,
I have the following latex code I would like to preprocess according to the direct/tex2svg-page example.
I am using "mathjax-full": "4.0.0-alpha.1"

\begin {equation*}
			\textbf {Aktivität}\left (\textbf {T}\right ) = \textbf {Aktiv}\left (\textbf {T}\right ) + \textbf {BMR}\left
			(\textbf {T}\right ) 
\end {equation*}

The problem is the german umlaut ä within the word "Aktivität". I can replace it with "a and than things work. However, I do not want to rewrite the entire text or write a parser to clean up things. Therefore I am wondering if there might be a solution to the issue.
One further note, the crash seems to be related to AssistiveMmlHandler. When I do not use it, I do not get the crash. However, the latex equation is than not processed.
The error message looks as follows:

Error: MathJax retry
    at retryAfter (/Users/cjohn/Entwicklung/MND/my-nutri-diary/mjax-html/node_modules/mathjax-full/js/util/Retries.js:25:15)
    at MathJaxModernFont2.FontData2.getChar (/Users/cjohn/Entwicklung/MND/my-nutri-diary/mjax-html/node_modules/mathjax-full/js/output/common/FontData.js:580:30)
    at SvgTextNode.CommonWrapper2.getVariantChar (/Users/cjohn/Entwicklung/MND/my-nutri-diary/mjax-html/node_modules/mathjax-full/js/output/common/Wrapper.js:741:30)
    at SvgTextNode.CommonTextNodeMixin2.computeBBox (/Users/cjohn/Entwicklung/MND/my-nutri-diary/mjax-html/node_modules/mathjax-full/js/output/common/Wrappers/TextNode.js:74:46)
    at SvgTextNode.CommonWrapper2.getBBox (/Users/cjohn/Entwicklung/MND/my-nutri-diary/mjax-html/node_modules/mathjax-full/js/output/common/Wrapper.js:203:14)
    at SvgTextNode.CommonWrapper2.getOuterBBox (/Users/cjohn/Entwicklung/MND/my-nutri-diary/mjax-html/node_modules/mathjax-full/js/output/common/Wrapper.js:211:25)
    at SvgMtext.CommonWrapper2.computeBBox (/Users/cjohn/Entwicklung/MND/my-nutri-diary/mjax-html/node_modules/mathjax-full/js/output/common/Wrapper.js:254:35)
    at SvgMtext.CommonWrapper2.getBBox (/Users/cjohn/Entwicklung/MND/my-nutri-diary/mjax-html/node_modules/mathjax-full/js/output/common/Wrapper.js:203:14)
    at SvgMtext.CommonWrapper2.getOuterBBox (/Users/cjohn/Entwicklung/MND/my-nutri-diary/mjax-html/node_modules/mathjax-full/js/output/common/Wrapper.js:211:25)
    at SvgInferredMrowNTD.CommonMrowMixin2.computeBBox (/Users/cjohn/Entwicklung/MND/my-nutri-diary/mjax-html/node_modules/mathjax-full/js/output/common/Wrappers/mrow.js:179:39) {
  retry: Promise { <pending> }
}
Can't load './output/svg/fonts/svg/dynamic/latin-b': No asyncLoad method specified

Thanks for your help!

dpvc commented

Because the v4 fonts include many more characters than the original MathJax fonts, they are broken into smaller pieces so that web users don't have to download the entire font and all the data about it even though the page they are reading uses only a few characters.

The accented Latin characters are one of the batches that are loaded dynamically when needed, and that is what is happening in your case. Because you are not using MathJax components, but rather are using direct access to the MathJax modules, you need to take these issues into account yourself. The node examples haven't been updated to v4 yet (since v4 is only in alpha release), which is one reason you are seeing this.

The "MathJax retry" error is because the tex2svg-page example isn't designed to handle dynamically loaded components like those used in the v4 fonts, since they weren't available in v3 and so didn't need to be taken into account. With v4, you now need to use promise-based methods that are used for dynamically loading MathJax components. MathJax throws a "retry" error when it has to wait for something to load and then try to typeset an expression again, and the mathjax.handleRetriesFor() function is used to properly wait for and handle those retry requests.

The is the first issue that you are seeing. The second is the "No asyncLoad method" error. Because the means used to access dynamically loaded content is very different in a browser than in node (the former uses script tags, while the latter used require() calls), MathJax needs to be told how to do that. This is done by specifying an asyncLoad function. For node programs, you use require('mathjax-full/js/util/asyncLoad/node.js') to cause that function to be defined.

Finally, you will also need to tell MathJax where to find the dynamic font files that you are using (these were originally stored in mathjax-full/js/output/fonts but with the fonts now being in individually loaded npm packages, MathJax will look in the wrong place unless you tell it otherwise (this will be fixed in the official v4 release). You do that by specifying the dynamicPrefix option for the SVG output jax that you create.

Here is a version of tex2svg-page that includes the needed changes.

#! /usr/bin/env node -r esm

/*************************************************************************
 *
 *  direct/tex2svg-page
 *
 *  Uses MathJax v3 to convert all TeX in an HTML document.
 *
 * ----------------------------------------------------------------------
 *
 *  Copyright (c) 2018 The MathJax Consortium
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

//
//  Load the packages needed for MathJax
//
const {mathjax} = require('mathjax-full/js/mathjax.js');
const {TeX} = require('mathjax-full/js/input/tex.js');
const {SVG} = require('mathjax-full/js/output/svg.js');
const {liteAdaptor} = require('mathjax-full/js/adaptors/liteAdaptor.js');
const {RegisterHTMLHandler} = require('mathjax-full/js/handlers/html.js');
const {AssistiveMmlHandler} = require('mathjax-full/js/a11y/assistive-mml.js');

require('mathjax-full/js/util/asyncLoad/node.js');

const {AllPackages} = require('mathjax-full/js/input/tex/AllPackages.js');
require('mathjax-full/js/util/entities/all.js');

//
//  Get the command-line arguments
//
var argv = require('yargs')
    .demand(1).strict()
    .usage('$0 [options] file.html > converted.html')
    .options({
        em: {
            default: 16,
            describe: 'em-size in pixels'
        },
        ex: {
            default: 8,
            describe: 'ex-size in pixels'
        },
        packages: {
            default: AllPackages.sort().join(', '),
            describe: 'the packages to use, e.g. "base, ams"'
        },
        fontCache: {
            default: 'global',
            describe: 'cache type: local, global, none'
        }
    })
    .argv;

//
//  Read the HTML file
//
const htmlfile = require('fs').readFileSync(argv._[0], 'utf8');

//
//  Create DOM adaptor and register it for HTML documents
//
const adaptor = liteAdaptor({fontSize: argv.em});
AssistiveMmlHandler(RegisterHTMLHandler(adaptor));

//
//  Create input and output jax and a document using them on the content from the HTML file
//
const tex = new TeX({packages: argv.packages.split(/\s*,\s*/)});
const svg = new SVG({
  fontCache: argv.fontCache,
  exFactor: argv.ex / argv.em,
  dynamicPrefix: 'mathjax-modern-font/js/output/fonts/mathjax-modern/svg/dynamic'
});
const html = mathjax.document(htmlfile, {InputJax: tex, OutputJax: svg});

//
//  Typeset the document
//
mathjax.handleRetriesFor(() => { html.render(); }).then(() => {

  //
  //  If no math was found on the page, remove the stylesheet and font cache (if any)
  //
  if (Array.from(html.math).length === 0) {
    adaptor.remove(html.outputJax.svgStyles);
    const cache = adaptor.elementById(adaptor.body(html.document), 'MJX-SVG-global-cache');
    if (cache) adaptor.remove(cache);
  }
  //
  //  Output the resulting HTML
  //
  console.log(adaptor.doctype(html.document));
  console.log(adaptor.outerHTML(adaptor.root(html.document)));

}).catch((err) => console.log(err));

@dpvc Thank you very much for the code. It is working now :)