GitbookIO / plugin-mathjax

MathJAX plugin for GitBook
Apache License 2.0
89 stars 51 forks source link

Page requires refresh to display mathjax formatted data. #34

Open JieqinT opened 7 years ago

JieqinT commented 7 years ago

Here's the screenshot of before a refresh.

screen shot 2017-05-11 at 21 42 47

and this is after

screen shot 2017-05-11 at 21 42 53

.

Switching between articles also trigger the same issue, upon returning to an article with mathjax formatted data, a refresh is required.

kalaider commented 7 years ago

+1

linrongbin16 commented 6 years ago

same problem:

on website, mathjax need refresh to display formulas, but when mathjax use forceSVG:true or create pdf, it report errors:

Error: TypeError: speech.processExpression is not a function
    at GetSpeech (/Users/linrongbin/devenv/project/Way-to-Algorithm/node_modules/gitbook-plugin-mathjax/node_modules/mathjax-node/lib/mj-single.js:540:29)
    at Function.execute (file:///Users/linrongbin/devenv/project/Way-to-Algorithm/node_modules/mathjax/unpacked/MathJax.js:243:26)
    at cb (file:///Users/linrongbin/devenv/project/Way-to-Algorithm/node_modules/mathjax/unpacked/MathJax.js:225:59)
    at Object.Process (file:///Users/linrongbin/devenv/project/Way-to-Algorithm/node_modules/mathjax/unpacked/MathJax.js:495:38)
    at Object.call (file:///Users/linrongbin/devenv/project/Way-to-Algorithm/node_modules/mathjax/unpacked/MathJax.js:508:37)
    at Function.WAITEXECUTE (file:///Users/linrongbin/devenv/project/Way-to-Algorithm/node_modules/mathjax/unpacked/MathJax.js:348:50)
    at cb (file:///Users/linrongbin/devenv/project/Way-to-Algorithm/node_modules/mathjax/unpacked/MathJax.js:225:59)
    at Object.Process (file:///Users/linrongbin/devenv/project/Way-to-Algorithm/node_modules/mathjax/unpacked/MathJax.js:495:38)
    at Object.call (file:///Users/linrongbin/devenv/project/Way-to-Algorithm/node_modules/mathjax/unpacked/MathJax.js:508:37)
    at Function.WAITEXECUTE (file:///Users/linrongbin/devenv/project/Way-to-Algorithm/node_modules/mathjax/unpacked/MathJax.js:348:50)
cjdbarlow commented 6 years ago

Hacky workaround courtesy of @quangounet, but adding a JS loop fixes the problem:

gitbook.events.bind("page.change", function() {
    MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
}
hamishwillee commented 6 years ago

@chrisjake Awesome - works fine if added into the page - thanks! Any plans to push a PR for this?

walkccc commented 6 years ago

My "book.json" looks like this { "plugins": [ "mathjax", "splitter", "toggle-chapters", "scripts" ], "pluginsConfig": { "scripts": { "files": [ "./fix.js" ] } } }

And my "fix.js" looks like this:

After adding these codes, it still can't work properly. Anything wrong with the codes?

cjdbarlow commented 6 years ago

Hi @walkccc Where does the scripts plugin add the JS in the page? If fix.js is loaded before mathjax, it won't work.

I vaguely remember during my troubleshooting process adding both sequentially to the .md file with no luck, so there may also be a race condition occurring.

If your gitbook is small, try manually adding the script to the bottom of each .md file? I ended up placing the script with some other js in a separate plugin that made the site header, but this may not be appropriate in your case.

walkccc commented 6 years ago

Hi @Chrisjake My gitbook is very big(about 500 .md files) I've tried a small case to append the in the end of the .md file. It still not work and require refresh to show the math equation.

hamishwillee commented 6 years ago

@walkccc Just FYI, you are not alone - the solution did not work for me either (I thought it did, but sadly not). I have not tried to debug this yet.

hamishwillee commented 6 years ago

@SamyPesse The root cause of this problem is that the CDN used for Mathjax changed. PR is here: https://github.com/GitbookIO/plugin-mathjax/pull/39

@walkccc @chrisjake etc, the above PR isn't much use to anyone else because head revision of this plugin only work for gitbook>4. If you wanted to use this you could see the PR above for changes and/or use my update_cdn branch/fork in your gitbooks:

"mathjax@git+https://github.com/Dronecode/plugin-mathjax.git#update_cdn",

(although I don't guarantee it won't move).

NOTE: You can't also have the script running that is recommended above, as this will render the maths twice!

southball commented 5 years ago

The problem is still occurring, but it seems I was able to 'fix' it by modifying book/plugin.js to this:

require(["gitbook"], function(gitbook) {
  window.addEventListener('load', function (event) {
    MathJax.Hub.Config({
      tex2jax: {}
    });

    gitbook.events.bind("page.change", function() {
      MathJax.Hub.Typeset()
    });
  });
});

I'm not sure if this will affect the functioning of the plugin, could someone take a look at this and see if this fixes the problem? @SamyPesse

It seems that currently the browser warns that MathJax is not defined as MathJax is accessed before the script is loaded.

Edit: I modified it to use load and not DOMContentLoaded.

hamishwillee commented 5 years ago

@jamiechoi I think updating the mathjax CDN as above is probably a better fix https://github.com/GitbookIO/plugin-mathjax/issues/34#issuecomment-374435329

Joeeyy commented 4 years ago

CDN of mathjax should be changed from cdn.mathjax.org/mathjax to cdnjs.cloudflare.com.

As for gitbook, we can modify one line in {root_of_your_gitbook}/node_modules/gitbook-plugin-mathjax/index.js from 'https://cdn.mathjax.org/mathjax/' + version + '/MathJax.js?config=TeX-AMS-MML_HTMLorMML', to 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.6.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML'. There are also other files using the old cdn url, you can find it by grep command and change it to up-to-date url.

BTW, if you visit https://cdn.mathjax.org/mathjax/2.6-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML and take a look into this code, you'll find why we need to refresh the page to reload those math elements.