xoofx / markdig

A fast, powerful, CommonMark compliant, extensible Markdown processor for .NET
BSD 2-Clause "Simplified" License
4.41k stars 456 forks source link

Block problem when Math rendering (MathJax) #815

Closed KaiDeMori closed 2 months ago

KaiDeMori commented 2 months ago

Good day! And thank you for this great libary. I have been using it for years now 🤗

At the moment, I am struggling to get math rendering to work.

The lines I seek to render are using the \[mathgoeshere\] notation like this:

\[ \gamma = \frac{1}{\sqrt{1 - \frac{v^2}{c^2}}} \]

If I simply put it in a HTML file with MathJax (see attached example) it works fine.

But when I try to use it in a markdig generated HTML page, it doesnt work. The \ in front of the []s are missing and the v^2 gets rendered as <sup>, which I guess must confuse MathJax.

I can't use the $$ notation and all the .md files I have already use the square brackets blocks.

I am normally using many extensions, but I removed all of them for now (for testing the math). Without any extensions loaded, it works perfectly if I add an additional backslash in front of the block brackets \\[texgoeshere\\]

Although there seems to be ways to "fix" this, my question is how it is supposed to work. Like: What is the intended way of doing math?

I couldn't find real examples or a tutorial and would be happy to write some explanation for the docs, when I get it to work.

Of course it would be an honor to answer any questions, as I might not have been able to present the problem in best manner. Please forgive me.

I have read these other related topics:

259

307

and have read the pull request

311

I thought the latter one should support my intended use-case 🤔

These are the extensions I usually load:

      var pipeline = new MarkdownPipelineBuilder()
         .UseAdvancedExtensions()
         .UseEmojiAndSmiley()
         .UseEmphasisExtras()
         .UseSmartyPants()
         .DisableHtml()
         .UseSoftlineBreakAsHardlineBreak()
         .Build();

I don't mind switching to a different math-rendering library, if that's the cause. I am looking for the cleanest solution.

Please have a wonderful time everyone and thanks again for the amazing code!

math_trials.zip

xoofx commented 2 months ago

I can't use the $$ notation and all the .md files I have already use the square brackets blocks.

Markdig does not support math without $$ and #311 still expects wrapping $$ or $. The changes in this PR added escaped brackets in and out.

Check this example on babelmark that is correctly using $$ and the Markdown implementation supporting Math are correctly handling it.

While apart maruku-math, none are supporting without $$ (here)

KaiDeMori commented 2 months ago

Thank you so much for the answer! I know now what to do. BTW, babelmarkIII is a fantastic site. I didn't know that one yet. Thx!

KaiDeMori commented 2 months ago

Sorry, just want to understand something:

Check this example on babelmark that is correctly using $$ and the Markdown implementation supporting Math are correctly handling it.

The example doesn't work for me and only shows a formula for maruku. Should it also work on other implementations? What could I be doing wrong?

xoofx commented 2 months ago

The example doesn't work for me and only shows a formula for maruku. Should it also work on other implementations? What could I be doing wrong?

In Markdig and most Markdown implementations, a math code block:

$$\gamma = \frac{1}{\sqrt{1 - \frac{v^2}{c^2}}}$$

will generate the following HTML fragment:

<p>
  <span class="math">
    \(\gamma = \frac{1}{\sqrt{1 - \frac{v^2}{c^2}}}\)
  </span>
</p>

It is then up to your final HTML page composition to integrate your Math JS library of choice to format these blocks correctly. Markdig today does not support rendering directly to e.g MathML

KaiDeMori commented 2 months ago

I think I finally get it now. 🙈 Thanks for the patience and taking the time! 🙏