Skip to content

Commit 10cf75a

Browse files
committed
Add support for html_math_renderer = 'mathml'
Fixes #233.
1 parent 5d4cdf7 commit 10cf75a

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

docs/additional_samples.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ Math
209209

210210
.. math::
211211
212-
(a + b)^2 = a^2 + 2ab + b^2
212+
(a + b)^2 = a^2 + 2ab + b^2 \\
213213
214214
(a - b)^2 = a^2 - 2ab + b^2
215215
@@ -225,10 +225,8 @@ Math
225225
.. math::
226226
:nowrap:
227227
228-
\begin{eqnarray}
229228
y & = & ax^2 + bx + c \\
230229
f(x) & = & x^2 + 2xy + y^2
231-
\end{eqnarray}
232230
233231
234232

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
"sphinx.ext.intersphinx",
5050
"sphinx.ext.napoleon",
5151
"sphinx.ext.todo",
52-
"sphinx.ext.mathjax",
5352
"sphinx.ext.viewcode",
5453
"sphinx_immaterial.theme_result",
5554
"sphinx_immaterial.kbd_keys",
@@ -97,6 +96,7 @@
9796
html_title = "Sphinx-Immaterial"
9897
html_favicon = "_static/images/favicon.ico" # colored version of material/bookshelf.svg
9998
html_logo = "_static/images/Ybin.gif" # from https://gifer.com/en/Ybin
99+
html_math_renderer = "mathml"
100100

101101
# -- HTML theme specific settings ------------------------------------------------
102102

sphinx_immaterial/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ def setup(app: Sphinx):
285285
)
286286
app.add_html_theme("sphinx_immaterial", os.path.abspath(os.path.dirname(__file__)))
287287

288+
app.setup_extension("sphinx_immaterial.mathml_math_renderer")
289+
288290
# register our custom directives/roles that are tied to this theme
289291
app.setup_extension("sphinx_immaterial.content_tabs")
290292
app.setup_extension("sphinx_immaterial.mermaid_diagrams")
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Extension that enables docutils' MathML-based math rendering."""
2+
3+
import docutils.nodes
4+
import docutils.utils.math
5+
from docutils.writers._html_base import HTMLTranslator as BaseHTMLTranslator
6+
from sphinx.application import Sphinx
7+
8+
# Sphinx overrides Docutils' math rendering. Here, we override Sphinx's
9+
# override to revert to docutils' math rendering.
10+
11+
12+
def visit_math(self: BaseHTMLTranslator, node: docutils.nodes.math):
13+
self.math_output = "mathml"
14+
self.math_output_options = []
15+
BaseHTMLTranslator.visit_math(self, node)
16+
17+
18+
def visit_math_block(self: BaseHTMLTranslator, node: docutils.nodes.math_block):
19+
self.math_output = "mathml"
20+
self.math_output_options = []
21+
# Note: We can't call `BaseHTMLTranslator.visit_math_block` here, because
22+
# that just forwards to `self.visit_math`, which ultimately calls back into
23+
# our `visit_math` function defined above but without the `math_env`
24+
# argument.
25+
BaseHTMLTranslator.visit_math(
26+
self, node, math_env=docutils.utils.math.pick_math_environment(node.astext())
27+
)
28+
29+
30+
def setup(app: Sphinx):
31+
"""Setup the extension."""
32+
app.add_html_math_renderer(
33+
"mathml",
34+
(visit_math, None),
35+
(visit_math_block, None),
36+
)
37+
return {
38+
"parallel_read_safe": True,
39+
"parallel_write_safe": True,
40+
}

0 commit comments

Comments
 (0)