lepture/mistune

Weird behaviours in math plugin

TiagodePAlves opened this issue · 4 comments

I was checking the math plugin and found out four incompatibilities with latex compilers (pdfLaTeX and XeLaTeX) and MathJax:

  • Block math ($$) requires a newline before and after content, effectively changing the delimiters to $$\n and \n$$.
  • Block math should be able to have text before and after it, in the same line.
  • Block math doesn't work with empty content ($$$$), the regex is done with .+ instead of .*.
  • Both math modes don't take care of escaped dollar signs (\$).

So the following markdown:

Dollar sign $\$$

Empty $$$$

No newline $$x$$

Is compiled to:

<p>Dollar sign <span class="math">\(\\)</span>$</p>
<p>Empty <span class="math">\($\)</span>$</p>
<p>No newline <span class="math">\($x\)</span>$</p>

Whereas it should probably be:

<p>Dollar sign <span class="math">\(\$\)</span></p>
<p>Empty <div class="math">$$$$</div></p>
<p>No newline <div class="math">$$x$$</div></p>

For comparison, here are the latex and MathJax versions:

Latex version

Code:

\documentclass{article}

\begin{document}
    Dollar sign $\$$ \\
    Empty $$$$ \\
    No newline $$x$$
\end{document}

Output:
image

MathJax version

Code:

<html>
    <head>
        <script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
        <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
    </head>
    <body>
        <p>Dollar sign \(\$\)</p>
        <p>Empty $$$$</p>
        <p>No newline $$x$$</p>
    </body>
</html>

Output:
image

Also, block math is not allowing multiline content. The following does not work:

$$
\begin{align}
x &= 1 \\
y &= 2
\end{align}
$$

The workaround is to write everything in a single line like

$$
\begin{align} x &= 1 \\ y &= 2 \end{align}
$$

I changed the patterns in mistune/plugins/math.py to:

BLOCK_MATH_PATTERN = r'(?sm)(?!^ {4,})\$\$\s*(?P<math_text>\S.*?)\s*(?<!\\)\$\$'
INLINE_MATH_PATTERN = r'\$\s*(?P<math_text>\S.*?)\s*(?<!\\)\$'

These seem to fix the issues above except $$$$ which is ambiguous and should be avoided (empty block math or a sequence of two empty inline equations?)

The patterns disallow empty formulas to avoid ambiguity, and formulas made only from whitespace.
The dollar sign \$ is allowed both inside and outside math.
Block math can be used like $$here$$ in the middle of text.
Multiline formulas are allowed.

@miguelbarao I've fixed block math plugin for multiline content.

@lepture Thank you. Still, it does not address some of the issues reported above:

$$y = \frac{1}{1-x}$$

should render
$$y = \frac{1}{1-x}$$
as is done here in github.

Will mistune force $$ to be in their own separate lines? I'm asking because it's very common to write simple formulas in a single line like the one above.