@@ -1490,6 +1490,9 @@ class ForceEquations(docutils.transforms.Transform):
14901490 def apply (self ):
14911491 env = self .document .settings .env
14921492 if env .config .nbsphinx_assume_equations :
1493+ # sphinx >= 8.2
1494+ self .document ['nbsphinx_assume_equations' ] = True
1495+ # sphinx < 8.2
14931496 env .get_domain ('math' ).data ['has_equations' ][env .docname ] = True
14941497
14951498
@@ -1700,6 +1703,16 @@ def env_purge_doc(app, env, docname):
17001703 env .nbsphinx_widgets .discard (docname )
17011704
17021705
1706+ def set_flag_to_add_mathjax (app , pagename , templatename , context , doctree ):
1707+ """Tell Sphinx to load MathJax for pages created by this extension.
1708+
1709+ Unless ``nbsphinx_assume_equations`` config variable was set to false.
1710+ """
1711+ if doctree and doctree .get ('nbsphinx_assume_equations' ):
1712+ # This context variable is used by Sphinx starting in version 8.2
1713+ context ['has_maths_elements' ] = True
1714+
1715+
17031716def html_page_context (app , pagename , templatename , context , doctree ):
17041717 """Add CSS files for code cells and galleries."""
17051718 # NB: the CSS files are copied in html_collect_pages().
@@ -2095,6 +2108,10 @@ def setup(app):
20952108 app .connect ('builder-inited' , builder_inited )
20962109 app .connect ('config-inited' , config_inited )
20972110 app .connect ('html-page-context' , html_page_context )
2111+ # Give our handler higher priority so that it executes before MathJax's. We
2112+ # need to write the context variable `has_maths_elements` before the MathJax
2113+ # Sphinx extension reads it.
2114+ app .connect ('html-page-context' , set_flag_to_add_mathjax , priority = 100 )
20982115 app .connect ('html-collect-pages' , html_collect_pages )
20992116 app .connect ('env-purge-doc' , env_purge_doc )
21002117 app .connect ('env-updated' , env_updated )
0 commit comments