Can I somehow customise math processing? #661

Closed
opened 2026-01-29 14:42:24 +00:00 by claunia · 3 comments
Owner

Originally created by @Kazbek on GitHub (Mar 7, 2024).

Im using math like this e=mc^2 and not it output to

<span class="math">\(e=mc^2\)</span>

and want to customize output Latex formula. Not just place it "as is" but process with my string to string function

string processTexFormula(string rawTex)
{
    //actually here will be server prerendering
    return"test_" + rawText;
}

so output will be:

<span class="math">\(test_e=mc^2\)</span>

Can I somehow configure Math processor?
I guess it must affect in this two places: one and two

Originally created by @Kazbek on GitHub (Mar 7, 2024). Im using math like this e=mc^2 and not it output to ``` <span class="math">\(e=mc^2\)</span> ``` and want to customize output Latex formula. Not just place it "as is" but process with my string to string function ``` string processTexFormula(string rawTex) { //actually here will be server prerendering return"test_" + rawText; } ``` so output will be: ``` <span class="math">\(test_e=mc^2\)</span> ``` Can I somehow configure Math processor? I guess it must affect in this two places: [one](https://github.com/xoofx/markdig/blob/fcb56fb03701cc1a07a52769908579adb8927ee2/src/Markdig/Extensions/Mathematics/HtmlMathInlineRenderer.cs#L25) and [two](https://github.com/xoofx/markdig/blob/fcb56fb03701cc1a07a52769908579adb8927ee2/src/Markdig/Extensions/Mathematics/HtmlMathBlockRenderer.cs#L25)
claunia added the question label 2026-01-29 14:42:24 +00:00
Author
Owner

@MihaZupan commented on GitHub (Mar 7, 2024):

Instead of modifying the parser/renderer, the easiest way to do something like this is likely to post-process the syntax tree before you render it

MarkdownPipeline pipeline = new MarkdownPipelineBuilder()
    .UseMathematics()
    .Build();

string markdown = "$e=mc^2$";

MarkdownDocument document = Markdown.Parse(markdown, pipeline);

foreach (MathInline math in document.Descendants<MathInline>())
{
    math.Content = new StringSlice(math.Content.ToString().Replace("e", "42"));
}

string html = document.ToHtml(pipeline);
// <p><span class="math">\(42=mc^2\)</span></p>
@MihaZupan commented on GitHub (Mar 7, 2024): Instead of modifying the parser/renderer, the easiest way to do something like this is likely to post-process the syntax tree before you render it ```c# MarkdownPipeline pipeline = new MarkdownPipelineBuilder() .UseMathematics() .Build(); string markdown = "$e=mc^2$"; MarkdownDocument document = Markdown.Parse(markdown, pipeline); foreach (MathInline math in document.Descendants<MathInline>()) { math.Content = new StringSlice(math.Content.ToString().Replace("e", "42")); } string html = document.ToHtml(pipeline); // <p><span class="math">\(42=mc^2\)</span></p> ```
Author
Owner

@Kazbek commented on GitHub (Mar 8, 2024):

@MihaZupan it works good but escaping string that I put in content. So instead

<span class="katex">

I have:

&lt;span class=&quot;katex&quot;&gt

Can I somehow write just this part unescaped?

@Kazbek commented on GitHub (Mar 8, 2024): @MihaZupan it works good but escaping string that I put in content. So instead ``` <span class="katex"> ``` I have: ``` &lt;span class=&quot;katex&quot;&gt ``` Can I somehow write just this part unescaped?
Author
Owner

@Kazbek commented on GitHub (Mar 19, 2024):

Made my own UseMathematics with custom renderer solved this problem.

@Kazbek commented on GitHub (Mar 19, 2024): Made my own UseMathematics with custom renderer solved this problem.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#661