Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tell MathJax to only process specific nodes #2118

Closed
geajack opened this issue Feb 3, 2019 · 8 comments
Closed

Tell MathJax to only process specific nodes #2118

geajack opened this issue Feb 3, 2019 · 8 comments
Labels

Comments

@geajack
Copy link

geajack commented Feb 3, 2019

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 <script> tags into the head.

I don't want to modify my markup, but what I do know - what absolutely anybody who ever wants to restrict MathJax to certain parts of their site knows - is which tags I want to apply MathJax to, and how to get a list of them with Javascript. That should be enough.

Perhaps there's a way of doing this that I'm missing?

@dpvc
Copy link
Member

dpvc commented Feb 3, 2019 via email

@geajack
Copy link
Author

geajack commented Feb 3, 2019

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.

@dpvc dpvc added the Question label Feb 3, 2019
@nitely
Copy link

nitely commented Feb 4, 2019

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.

@geajack
Copy link
Author

geajack commented Feb 4, 2019

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?

@nitely
Copy link

nitely commented Feb 4, 2019 via email

@dpvc
Copy link
Member

dpvc commented Feb 4, 2019

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>

@dpvc dpvc closed this as completed May 12, 2020
@JamesTheAwesomeDude
Copy link

How is this to be done with the v3 config?

@dpvc
Copy link
Member

dpvc commented May 29, 2021

@JamesTheAwesomeDude:

How is this to be done with the v3 config?

MathJax = {
  startup: {
    elements: ['article']
  }
};

See the Startup Options documentation for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants