spatialaudio / nbsphinx

:ledger: Sphinx source parser for Jupyter notebooks
https://nbsphinx.readthedocs.io/
MIT License
451 stars 130 forks source link

errors loading plotly graphs with latex #572

Closed atharva-2001 closed 3 years ago

atharva-2001 commented 3 years ago

I am trying to plot Plotly graphs inside sphinx documentation, but it seems MathJax is causing some issues. In the documentation, MathJax v3 is being loaded, which causes errors because MathJax.hub doesn't exist anymore.

image

MathJax v3 should work with plotly because

window.PlotlyConfig = {MathJaxConfig: 'local'};

is also present in the documentation (see here as well please)

However, if I set nbsphinx_assume_equations = False in conf.py and remove Latex in cells, I am able to see the figure, because MathJax is no longer loaded.

image

Could someone please tell me how to solve this?

mgeier commented 3 years ago

Can you please provide a minimal reproducible example?

atharva-2001 commented 3 years ago

Sure, with nbsphinx_assume_equations = False in conf.py: latex in labels and cells latex only in labels no latex at all see the conf.py file used to make these here

Without any changes in conf.py (nbsphinx_assume_equations = True, default value) latex in labels and cells labels only in latex no latex at all see the conf.py file used to make these here

mgeier commented 3 years ago

Thanks for the examples. The aren't really minimal, though.

atharva-2001 commented 3 years ago

Any updates on this? @mgeier

mgeier commented 3 years ago

Not from my side. I'm waiting for a minimal example for me to reproduce and investigate the problem.

atharva-2001 commented 3 years ago

Could you please check if this works? html file with links to files built with nbsphinx_assume_equations = False in conf.py html file with links to files built with nbsphinx_assume_equations = True in conf.py link to the repository I was not sure what was needed, please let me know if I can do anything more.

mgeier commented 3 years ago

Thanks, this is still more complicated than necessary, but I can work with it!

IMHO, this should just work regardless of the value of nbsphinx_assume_equations.

The problem (or rather one of the problems) seems to be that plotly inserts this code into the HTML page:

        if (window.MathJax) {MathJax.Hub.Config({SVG: {font: "STIX-Web"}});}

... and Sphinx defines window.MathJax because nbsphinx sets some default values for mathjax3_config. Anyone who uses mathjax3_config will have the same problem, even if they don't use nbsphinx.

You should make another minimal example without nbsphinx that uses mathjax3_config, for example

mathjax3_config = {'tex': {'processEscapes': True}}

(EDIT: fixed dict syntax)

If this causes the same error (which I think it does), you can create an issue at the plotly issue tracker. Once that is fixed, you can try it with nbsphinx again and we can continue here.

mgeier commented 3 years ago

In the meantime, you can work around this problem by either downgrading Sphinx to below version 4 or by manually switching to MathJax 2 like this:

mathjax_path = 'https://cdn.jsdelivr.net/npm/mathjax@2/MathJax.js?config=TeX-AMS-MML_HTMLorMML'
mathjax2_config = {
    'tex2jax': {
        'inlineMath': [['$', '$'], ['\\(', '\\)']],
        'processEscapes': True,
        'ignoreClass': 'document',
        'processClass': 'math|output_area',
    }
}
atharva-2001 commented 3 years ago

Thank you very much! I will make an issue in plotly soon.

mgeier commented 2 years ago

In the meantime, https://github.com/plotly/plotly.js/pull/6073 has been merged.

Maybe it works now with MathJax 3?