Mathjax: Tell MathJax to only process specific nodes

Created on 3 Feb 2019  路  6Comments  路  Source: mathjax/MathJax

Only certain HTML nodes in my site actually contain LaTeX, so I'd like to be able to tell MathJax to only process those. What I'd like to be able to do is something like this:

<script>
    MathJax.Hub.Config({
        tex2jax: {
            restrict: document.getElementsByTagName("article")
        }
    });
</script>

But instead, the only way I could get the result I want is

  1. Add a class to the <body> - "nomathjax" or something.
  2. Use the ignoreClass property to exclude the whole page.
  3. Add a class to the <article> tags like "yesmathjax".
  4. Use the processClass property to include those tags back in.

It's steps (2) and (4) that bother me. My HTML is mine. I'm finicky about it. I've got it nice and clean and minimal, just the way I like it. I don't want to have to add meaningless CSS classes to it just because a library I'm using needs me to. Actually, in my particular use case, it's impossible for me to do that - I can't modify the page body, all I can do is insert

(and it must be placed BEFORE the script that loads MathJax.js itself). This will restrict MathJax typesetting operations to the contents of the specified elements. It is a global property because it applied to ALL preprocessors.

All 6 comments

You can use

(and it must be placed BEFORE the script that loads MathJax.js itself). This will restrict MathJax typesetting operations to the contents of the specified elements. It is a global property because it applied to ALL preprocessors.

Awesome! I've been waist-deep in docs about Queues and pre processors for the past hour. The Google results on this topic really seemed to suggest there was no way of doing it other than processClass. Time to update some StackOverflow threads, I guess.

The problem with @dpvc solution is that if no elements are found, then mathjax will fallback to the default behaviour of trying to process the entire DOM.

The problem with @dpvc solution is that if no elements are found, then mathjax will fallback to the default behaviour of trying to process the entire DOM.

Is there a way around that?

Sure, a hack like passing an array of a single dummy node (i.e
[document.createElement('div')) if there are no matches (i.e nodes.length
=== 0). But mathjax could make passing null mean don't process anything to
make this less hackish, without a breaking change (altough a better api
would do the opposite, null process all and empty array process nothing).
My 2c

One possible way to handle the case where there might be no elements of the given type is the following:

<script type="text/x-mathjax-config">
(function () {
  var articles = document.getElementsByTagName("article");
  MathJax.Hub.Config({
    elements: articles,
    skipStartupTypeset: (articles.length === 0)
  });
})();
</script>
Was this page helpful?
0 / 5 - 0 ratings

Related issues

mt4c picture mt4c  路  3Comments

kiwi0fruit picture kiwi0fruit  路  3Comments

parhizkari picture parhizkari  路  5Comments

photino picture photino  路  5Comments

Jerska picture Jerska  路  6Comments