Skip to content

Conversation

gabalafou
Copy link
Contributor

Fixes compatibility issue with newer versions of Sphinx.

See:

@gabalafou gabalafou changed the title Fix nbsphinx_assume_equations to work with newer versions of Sphinx Fix nbsphinx_assume_equations for newer versions of Sphinx Aug 6, 2025
@gabalafou
Copy link
Contributor Author

Somehow I didn't see PR #822 until today. But as noted, it does not work in all cases.

I started seeing un-Jaxified math while working on a different issue in the PyData Sphinx Theme docs. (Specifically, I was trying to figure out why in docs generated by nbsphinx, MathJax is not applied inside an ipywidget Tab.)

This led me to creating this PR.

We can see that with this PR, the math equations get properly rendered in all three of the examples added in #836.

Examples, screenshots

All three examples share one thing in common: there is no other math in the source document apart from the one featured.

Markdown cell with block math equation

Source code:

{
"cell_type": "markdown",
"metadata": {},
"source": [
"This page has a secondary use, though,\n",
"which is testing whether a single block math equation works\n",
"(if there is no other math on the same page):\n",
"\n",
"\\begin{equation}\n",
"\\int\\limits_{-\\infty}^\\infty f(x) \\delta(x - x_0) dx = f(x_0).\n",
"\\end{equation}"
]
}

Screenshot of docs page with this PR applied:

on the uno-rst.html preview docs page, equation rendered properly

Code cell that outputs a Math object

Source code:

# %% [markdown]
# This page has a secondary use,
# which is testing whether a single math output cell works
# (if there is no other math on the same page):
# %%
from IPython.display import Math
eq = Math(r'\int\limits_{-\infty}^\infty f(x) \delta(x - x_0) dx = f(x_0)')
eq

Screenshot of docs page with this PR applied:

on the due-rst.html preview docs page, equation rendered properly

Markdown cell with inline math

Source code:

{
"cell_type": "markdown",
"metadata": {},
"source": [
"This page has a secondary use, though,\n",
"which is testing whether a single inline math equation works\n",
"(if there is no other math on the same page): $\\text{e}^{i\\pi} = -1$."
]
}

Screenshot of docs page with this PR applied:

on the cinque-rst.html preview docs page, equation rendered properly

@gabalafou
Copy link
Contributor Author

gabalafou commented Aug 6, 2025

Even though this PR succeeds with the examples added in #836, it's still worth considering the alternative:

app.set_html_assets_policy("always")

This alternative has the benefit that it's straightforward. It could allow us to have a single code path, so long as we don't care whether this feature (forcing MathJax on nbsphinx-converted pages) works on older versions of Sphinx - because html assets policy was added in 4.1.

I also did a search of GitHub repos for the use of html_assets_policy and it didn't seem to me that this is used much (or at all) beyond loading MathJax. The risk, though, is that may change in the future. The other risk is that suddenly loading MathJax on every documentation page may have unintended consequences.

Overall, this PR is a more conservative change that preserves the status quo from before sphinx-doc/sphinx#13044, but comes with the downside of a split code path and a slightly higher cognitive load.

@mgeier
Copy link
Member

mgeier commented Aug 16, 2025

Thanks for this PR!

Before looking into this, I would like to fix the CI errors, but those are quite annoying, so I don't know when I'll get around to doing that ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants