sympy / sphinx-math-dollar

Sphinx extension to let you write LaTeX math using $$
https://www.sympy.org/sphinx-math-dollar/
MIT License
33 stars 11 forks source link

expression not translated when next line is indented #6

Closed cbm755 closed 5 years ago

cbm755 commented 5 years ago

Is this a bug?

Consider a docstring containing:

    """
    ...
    foo foo foo

    where $j$ is:
        bar bar bar

    ...
    """

Eventually renders as

<p>foo foo foo</p>
<dl class="docutils">
<dt>where $j$ is:</dt>
<dd><p class="first">bar bar bar</p>

(note $j$ is still there).

But if we add a new paragraph:

    foo foo foo

    where $j$ is:

        bar bar bar

gives

<p>foo foo foo</p>
<p>where <span class="math notranslate nohighlight"><span class="MathJax_Preview" style="color: inherit;"></span><span class="MathJax" id="MathJax-Element-120-Frame" tabindex="0" style="position: relative;" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>j</mi></math>" role="presentation"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-1992" style="width: 0.579em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.433em; height: 0px; font-size: 130%;"><span style="position: absolute; clip: rect(1.166em, 1000.42em, 2.319em, -1000em); top: -1.971em; left: 0em;"><span class="mrow" id="MathJax-Span-1993"><span class="mi" id="MathJax-Span-1994" style="font-family: MathJax_Math; font-style: italic;">j</span></span><span style="display: inline-block; width: 0px; height: 1.971em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.328em; border-left: 0px solid; width: 0px; height: 1.25em;"></span></span></nobr>

which I assume is correct (anyway, it has no $$).

I hit this playing with Stirling in combinatorial/numbers.py in SymPy.

asmeurer commented 5 years ago

Thank you for trying this out. I will need to debug it and see how it is being parsed as a doctree AST.

asmeurer commented 5 years ago

It's because we only replace things that are parsed as "paragraph". But the "where $j$ is" is parsed as a definition_list_item.

I think there was some problem matching all text instances. Let me see if I can figure out what it was.

asmeurer commented 5 years ago

It's because Text is used as a subnode of everything. So it would also match ``$...$`` in a code block, like is used in the latex() docstring, because that's Literal(Text). I had assumed that paragraph was the only one we cared about, but that's wrong. We should also support math in things like titles as well.

So I guess we should have a node blacklist, or should it be a whitelist?

Here are the docutils nodes https://github.com/docutils-mirror/docutils/blob/master/docutils/nodes.py.

asmeurer commented 5 years ago

It looks like other than literal, most of the nodes we would want to avoid subclass from FixedTextElement.

We should probably make the blacklist configurable at any rate.