AllenDowney / ThinkBayes2

Text and code for the forthcoming second edition of Think Bayes, by Allen Downey.
http://allendowney.github.io/ThinkBayes2/
MIT License
1.8k stars 1.49k forks source link

[Workaround] Add mathjax on gh-pages to properly render formulas. #68

Closed joac closed 9 months ago

joac commented 11 months ago

Fixes #67

I used a small script to include MathJax in all html files.

import glob

line =  "  <!-- Scripts loaded after <body> so the DOM is not blocked -->"

payload = """
  <script>
  MathJax = {
    tex: {
      inlineMath: [['$', '$'], ['\\(', '\\)']]
    },
    svg: {
      fontCache: 'global'
    }
  };
  </script>
  <script type="text/javascript" id="MathJax-script" async
    src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js">
  </script>
"""

for filename in glob.glob("**/*.html", recursive=True):
    with open(filename) as f:
        content = f.read()
        content = content.replace(line, f"{line}\n{payload}")
    with open(filename, "w") as f:
        f.write(content)

I did this approach because the sphinx files are not available.

A fixed version is deployed at: https://joac.github.io/ThinkBayes2/chap02.html

AllenDowney commented 11 months ago

Thanks for this, but the problem is that these files are generated by jupyter-book, so if we change them, the changes will get clobbered the next time I generate the files. I am not sure why the generates files are not working -- I think they used to. I have not had a chance to investigate.

joac commented 11 months ago

Looking at the issues on jupyter book, seems a configuration is required: https://github.com/executablebooks/jupyter-book/issues/1212#issuecomment-775772614

AllenDowney commented 11 months ago

Just tried a fresh installation of jupyter book with the configuration you pointed me to, as well as the one in the docs:

https://jupyterbook.org/en/stable/content/math.html?highlight=mathjax

No luck with any of the variations I tried.

The docs say that jb uses MathJax 2 by default, so it seems like none of this should be necessary.

I just pushed the current version of _config.yml and the other files I use to build the HTML version.

@joac I would love to get this fixed, but I keep hitting a wall and then I don't have time to keep going. But if you have any more ideas, they are welcome!