Skip to content

Commit a76926c

Browse files
committed
Add support for html_math_renderer = 'mathml'
Fixes #233.
1 parent 6713712 commit a76926c

File tree

8 files changed

+1275
-4
lines changed

8 files changed

+1275
-4
lines changed

docs/additional_samples.rst

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

212212
.. math::
213213
214-
(a + b)^2 = a^2 + 2ab + b^2
214+
(a + b)^2 = a^2 + 2ab + b^2 \\
215215
216216
(a - b)^2 = a^2 - 2ab + b^2
217217
@@ -227,10 +227,8 @@ Math
227227
.. math::
228228
:nowrap:
229229
230-
\begin{eqnarray}
231230
y & = & ax^2 + bx + c \\
232231
f(x) & = & x^2 + 2xy + y^2
233-
\end{eqnarray}
234232
235233
236234

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

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,5 @@ or ``theme.conf`` for more details.
141141
rst_basics
142142
rst-cheatsheet/rst-cheatsheet
143143
additional_samples
144+
math
144145
myst_typography

docs/math.rst

Lines changed: 1179 additions & 0 deletions
Large diffs are not rendered by default.

sphinx_immaterial/__init__.py

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

304+
app.setup_extension("sphinx_immaterial.mathml_math_renderer")
305+
304306
# register our custom directives/roles that are tied to this theme
305307
app.setup_extension("sphinx_immaterial.content_tabs")
306308
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), # type: ignore[arg-type]
35+
(visit_math_block, None), # type: ignore[arg-type]
36+
)
37+
return {
38+
"parallel_read_safe": True,
39+
"parallel_write_safe": True,
40+
}

src/assets/stylesheets/main.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,4 @@
8585

8686
@import "main/sphinx";
8787
@import "main/api";
88+
@import "main/mathml";
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
math .boldsymbol {
2+
font-weight: bold;
3+
}
4+
5+
mtable[columnalign="right left"] {
6+
> mtr {
7+
> mtd:nth-child(1) {
8+
text-align: -webkit-right;
9+
}
10+
> mtd:nth-child(2) {
11+
text-align: -webkit-left;
12+
}
13+
}
14+
}
15+
16+
mtable[columnalign="right left right"] {
17+
> mtr {
18+
> mtd:nth-child(1) {
19+
text-align: -webkit-right;
20+
}
21+
> mtd:nth-child(2) {
22+
text-align: -webkit-left;
23+
}
24+
> mtd:nth-child(3) {
25+
text-align: -webkit-right;
26+
}
27+
}
28+
}
29+
30+
mtable[columnspacing="0 2em"] {
31+
> mtr {
32+
> mtd:nth-child(1) {
33+
padding-left: 0px;
34+
padding-right: 0px;
35+
}
36+
> mtd:nth-child(2) {
37+
padding-left: 0px;
38+
padding-right: 2em;
39+
}
40+
}
41+
}
42+
43+
mtable[columnspacing="0"] {
44+
> mtr {
45+
> mtd {
46+
padding-left: 0px;
47+
padding-right: 0px;
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)