👌 IMPROVE: Math Parsing#217
Conversation
This centralises parsing configuration storage and validation, meaning it only has to be updated in a single place. Also moved from using `texmath_plugin` to `dollarmath_plugin`
Codecov Report
@@ Coverage Diff @@
## master #217 +/- ##
==========================================
- Coverage 91.17% 90.97% -0.21%
==========================================
Files 12 12
Lines 1281 1296 +15
==========================================
+ Hits 1168 1179 +11
- Misses 113 117 +4
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
98c18e3 to
2e651d4
Compare
|
Ok @executablebooks/ebpteam and @phaustin I have now hopefully fully solved the Math parsing/rendering. Basically, I wrote a new markdown plugin in executablebooks/markdown-it-py@91bf225, which gives greater control over the parsing process, and fixes the internal escaping issue (as raised in jupyter-book/jupyter-book#750 (comment)). Then here, I use that plugin and also re-configure mathjax such that it does not try to replicate this parsing, and so "respects" if the math paring is disabled and makes it consistent with e.g. TeX/PDF outputs. See https://myst-parser--217.org.readthedocs.build/en/217/using/syntax.html#math-shortcuts, and in particular https://myst-parser--217.org.readthedocs.build/en/217/using/syntax.html#mathjax-and-math-parsing NOTE because This will then feed into executablebooks/MyST-NB#226 and finally into jupyter-book. |
This ensures that math is only parsed by the markdown parser (when enabled)
2e651d4 to
6a58638
Compare
|
|
||
| Since these delimiters are how `sphinx.ext.mathjax` wraps the math content in the built HTML documents. | ||
|
|
||
| To inhibit this override, set `override_mathjax=False`. |
There was a problem hiding this comment.
is there some way for people to provide their own extra mathjax overrides? several folks have asked about this in JB
There was a problem hiding this comment.
Just use mathjax_config? Then now, they will also have to ensure myst_override_mathjax=False
|
the docs addition looks good to me, I don't have time right now to take a look at the implementation so perhaps @mmcky can give a review on that. Two quick thoughts:
Other than those nits I think that this looks really nice! |
Not sure what you mean by this? Are you talking about |
Yes sphinx.ext.imgmath, sphinx.ext.jsmath and https://github.com/hagenw/sphinxcontrib-katex |
|
Right I'm going to merge this, so I can get on to other things, but feel free to have a look through @mmcky and we can always fix anything in post |
|
|
||
| There are a few other options available to control dollar math parsing: | ||
|
|
||
| `myst_dmath_allow_space=False`, will cause inline math to only be parsed if there are no initial / final spaces, e.g. `$a$` but not `$ a$` or `$a $`. |
|
|
||
| `myst_dmath_allow_space=False`, will cause inline math to only be parsed if there are no initial / final spaces, e.g. `$a$` but not `$ a$` or `$a $`. | ||
|
|
||
| `myst_dmath_allow_digits=False`, will cause inline math to only be parsed if there are no initial / final digits, e.g. `$a$` but not `1$a$` or `$a$2`. |
There was a problem hiding this comment.
See the default configuration here: https://myst-parser.readthedocs.io/en/latest/using/intro.html#myst-configuration-options and in the code:
MyST-Parser/myst_parser/main.py
Line 22 in 8206f2a
I'm open to discussion about these though 😄
There was a problem hiding this comment.
as in at present the default is that $ a $ will be parsed as math
There was a problem hiding this comment.
ah nice page. I just noticed you added (off by default) in a couple other places in the text and was wondering if you wanted that here as well?
There was a problem hiding this comment.
If having a default value of myst_dmath_allow_space=True provides a more robust (but less technically correct) parse of the text -- I think it is probably good to have on by default. What do you think?
There was a problem hiding this comment.
provides a more robust (but less technically correct)
Well I don't know if there is a "technichally correct" syntax rule for this, since obviously its not covered by the CommonMark spec. Just to clarify, you think that $ a $ should or should not be recognised a math?
|
|
||
| md.options.update( | ||
| { | ||
| "enable_html_img": config.html_img_enable, |
There was a problem hiding this comment.
@chrisjsewell is there a reason to have different option names here?
There was a problem hiding this comment.
Not really, I just did want to make a breaking change to the internal code. But yeh can be changed at some point
There was a problem hiding this comment.
@chrisjsewell love your work on this! Many people will really love the features added here.
In my testing -- the only other edge case I have found is:
Edge Cases
The following myst
What happens if I use $$
L_x = y
$$produces
Do you mean that this is what is produced using rst (with a sphinx extension), or that this example was taken from an rst document? Currently the dollarmath plugin does not support inline double-dollar. It is on the TODO list though |
Sorry old habits -- I mean
OK Roger that. So the display math needs leading white space These also produce the same output What happens if I use $$L_x = y$$
What happens if I use
$$
L_x = y
$$but this works: $$
L_x = y
$$
What happens if I useso leading white space is a hard requirement for |
|
We may want to update this doc page as the syntax used shows |
|
Yeh pop it in a PR cheers Basically this is the current specification: https://raw.githubusercontent.com/executablebooks/markdown-it-py/master/tests/test_plugins/fixtures/dollar_math.md |
|
If you (or anyone) feels there are any issues then altered/additional tests should be added to that document (which gets run a parametrized pytests) |
So I notice the one line syntax for display math works when next to a Do you want me to add a test case and document |
|
yeh go for it |
|
It wouldn't be too hard to add inline displaymath parsing. However, I would also need to test how sphinx would handle this |

This centralises parsing configuration storage and validation, meaning it only has to be updated in a single place.
Also moved from using
texmath_plugintodollarmath_plugin