The MarkView.Avalonia.Math package renders LaTeX formulas with CSharpMath, in .NET: no browser, WebView or JavaScript.
dotnet add package MarkView.Avalonia.Math
viewer.UseMath();
Inline: Einstein's equation is $E = mc^2$, or \(E = mc^2\).
$$
\left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right)
$$
Math is opt-in: $ is common in ordinary text ($5), so a viewer only reads it as math once enabled.
Math needs both the syntax in the pipeline and the renderer. UseMath() does both: it adds the renderer and sets the viewer's pipeline to UseGitHubExtensions() plus Markdig's UseMathematics() ($…$, $$…$$) and the package's UseMathBracketDelimiters() (\(…\), \[…\]).
Since it replaces the viewer's pipeline, build the pipeline yourself to combine math with other syntax, or to enable it for every viewer:
MarkdownViewerDefaults.Extensions.AddMath();
MarkdownViewerDefaults.Pipeline = new MarkdownPipelineBuilder()
.UseGitHubExtensions()
.UseMathematics() // $…$ and $$…$$
.UseMathBracketDelimiters() // \(…\) and \[…\]
.UseThinkingBlocks()
.Build();
Language models and LaTeX sources often write math as \(…\) (inline) and \[…\] (display) rather than with dollars. They render exactly like their dollar counterparts:
The roots of \(ax^2 + bx + c = 0\) are
\[
x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
\]
for any \(a \neq 0\).
\(…\) stays on one line, like $…$.\[…\] may span lines. When it has its lines to itself it is a block, even right under a line of text; when it shares a line with text it is inline.In CommonMark, \( and \[ are an escaped parenthesis and bracket, so with the bracket delimiters on, text such as \(see below\) becomes a formula. They stay escapes when the opening is glued to a letter or digit (arr\[i\], f\(x\)), is never closed, or encloses nothing (\(\)). A formula's lines are read as a paragraph first: a line inside \[…\] that starts like a list item (- , 1. ) or a heading ends the paragraph, as does a blank line (which LaTeX does not allow in display math either), so the \[ is left unclosed and shows as text.
To keep only the dollars, and the CommonMark escapes:
viewer.UseMath(bracketDelimiters: false);
The bracket delimiters also work without the dollars, for documents where $ is common in prose: build the pipeline with UseMathBracketDelimiters() and without UseMathematics().
Each display formula has a copy button in its top right corner, which copies its TeX source without the $$ or \[ delimiters and shows a check mark, as the code block button does. It follows ShowCodeBlockCopyButton and ShowCodeBlockCopyButtonOnHover (see Code blocks). The formula keeps a margin on both sides as wide as the button, so it stays centred and never runs under it. The copy goes through CopyingToClipboard, with MarkdownCopyOrigin.MathBlock as its origin.
Inline formulas have no button. Selecting a formula, inline or not, copies its source with its delimiters.
#FAFAFA dark, #27272A light), and drawn again when the variant changes.$$ or \[ block at the end of the text shows as its source until a blank line or another block follows it; an open \( is closed for display.Invalid LaTeX shows CSharpMath's own error text rather than throwing. A formula past the safety limits (10,000 characters, or braces nested more than 50 deep) shows as its source. An unexpected failure shows the source of a block formula as text, and leaves the last rendering of an inline formula in place.
A display formula is a Border.markdown-math-block, an inline one an Image.markdown-math-inline; see Style classes. The package has no theme file of its own.