DOM and auto-render
The Temml.AngleSharp package renders into an AngleSharp document. It stands for the parts of Temml that work on a browser DOM.
dotnet add package Temml.AngleSharpIts types are in the Temml.Dom namespace. TemmlOptions and ParseError stay in Temml.
Render into an element
Section titled “Render into an element”TemmlDom.Render replaces the content of an element with the rendered math. If the element is already a <math> element, it is filled instead of getting a nested one.
var document = parser.ParseDocument("<!DOCTYPE html><body><div id=\"math\"></div></body>");var element = document.GetElementById("math")!;
TemmlDom.Render(@"x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}", element);
Console.WriteLine(element.InnerHtml);Render the math in a page
Section titled “Render the math in a page”TemmlDom.RenderMathInElement searches the text under an element for math delimiters and renders each expression in place. It is upstream’s renderMathInElement (auto-render), and it ends with the post-processing pass described below.
var document = parser.ParseDocument( @"<!DOCTYPE html><body><p>Euler: \(e^{i\pi} + 1 = 0\)</p><p>$$\int_0^1 x^2\,dx$$</p></body>");
TemmlDom.RenderMathInElement(document.Body!);
Console.WriteLine(document.Body!.InnerHtml);The default delimiters are $$…$$, \(…\), \[…\], the AMS environments equation, align, alignat, gather, and CD (with their starred forms), and \ref{…} and \eqref{…}. A single $ is not a default delimiter, because it would break prices and other uses of $ in text.
Auto-render options
Section titled “Auto-render options”TemmlAutoRenderOptions carries the TemmlOptions settings and the options of auto-render. The delimiter that matched decides DisplayMode.
var article = parser.ParseDocument( @"<!DOCTYPE html><body><p>Inline $a^2$.</p><p class=""no-math"">Costs $5 or $6.</p></body>");
TemmlDom.RenderMathInElement( article.Body!, new TemmlAutoRenderOptions { Fences = "$+", // $...$, $$...$$, and the AMS environments IgnoredClasses = ["no-math"], PreProcess = math => math.Trim(), ErrorCallback = (message, error) => Console.Error.WriteLine(message + error.Message), ThrowOnError = true, // report failures to ErrorCallback and keep the source text });
Console.WriteLine(article.Body!.InnerHtml);| Option | What it does |
|---|---|
Delimiters |
The list of delimiter pairs, each with its display mode. Rules apply in order, so list $$ before $. |
Fences |
A short key for a preset list, used instead of Delimiters. |
IgnoredTags |
Element names that are not searched. By default script, noscript, style, textarea, pre, code, and option. |
IgnoredClasses |
Elements with one of these classes are not searched. |
PreProcess |
Changes each expression before it is rendered. |
ErrorCallback |
Called when an expression fails to parse. The text stays as it was. |
The Fences keys:
| Key | $$…$$ |
$\…`$` |
$…$ |
\[…\] |
\(…\) |
AMS environments and references |
|---|---|---|---|---|---|---|
| not set | yes | yes | yes | yes | ||
$ |
yes | yes | yes | |||
$+ |
yes | yes | yes | yes | ||
( |
yes | yes | ||||
(+ |
yes | yes | yes | |||
ams |
yes | |||||
all |
yes | yes | yes | yes | yes | yes |
A parse error only reaches ErrorCallback when ThrowOnError is set. Otherwise the expression is replaced by error markup.
Macros defined with \gdef carry over from one expression to the next within a call.
References
Section titled “References”\label, \ref, and \eqref need a second pass over the document, after every expression has been rendered. RenderMathInElement runs it. If you render expressions one at a time, call TemmlDom.PostProcess once at the end.
var page = parser.ParseDocument( "<!DOCTYPE html><body><div id=\"equation\"></div><p>See <span id=\"link\"></span>.</p></body>");
TemmlDom.Render( @"\begin{equation} E = mc^2 \label{energy} \end{equation}", page.GetElementById("equation")!, new TemmlOptions { DisplayMode = true });TemmlDom.Render(@"\eqref{energy}", page.GetElementById("link")!);
// Call it once, after every expression has been rendered.TemmlDom.PostProcess(page.Body!);
Console.WriteLine(page.Body!.InnerHtml);PostProcess collects labels from the whole document and fills the references under the element you pass. Every reference needs a matching label: otherwise PostProcess, and so RenderMathInElement, throws an InvalidOperationException.
Without a DOM
Section titled “Without a DOM”If you only build strings, RenderToString is enough and the Temml package has no dependency on AngleSharp. For a page that contains \ref or \eqref, either run PostProcess on the parsed page on the server, or serve upstream’s temmlPostProcess.js and call temml.postProcess(document.body) in the browser.
MIT · Copyright 2020 Ron Kok · unofficial community port of Temml