lepture / mistune

A fast yet powerful Python Markdown parser with renderers and plugins.
http://mistune.lepture.com/
BSD 3-Clause "New" or "Revised" License
2.6k stars 250 forks source link

Weird behaviours in math plugin #330

Open marmitar opened 1 year ago

marmitar commented 1 year ago

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

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: ```latex \documentclass{article} \begin{document} Dollar sign $\$$ \\ Empty $$$$ \\ No newline $$x$$ \end{document} ``` Output: ![image](https://user-images.githubusercontent.com/22567908/207233943-40475223-afae-4e64-810f-3b1e95b4a0df.png)
MathJax version Code: ```html

Dollar sign \(\$\)

Empty $$$$

No newline $$x$$

``` Output: ![image](https://user-images.githubusercontent.com/22567908/207234120-918ffc16-0bb4-4f81-8fa9-a628f23b4d90.png)
miguelbarao commented 1 year ago

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}
$$
miguelbarao commented 1 year ago

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.

lepture commented 1 year ago

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

miguelbarao commented 1 year ago

@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.