Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird behaviours in math plugin #330

Open
marmitar opened this issue Dec 13, 2022 · 4 comments
Open

Weird behaviours in math plugin #330

marmitar opened this issue Dec 13, 2022 · 4 comments

Comments

@marmitar
Copy link

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

@miguelbarao
Copy link

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
Copy link

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
Copy link
Owner

lepture commented Jan 11, 2023

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

@miguelbarao
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants