Skip to content

Quickstart

Temml for .NET converts TeX math to MathML in C#. Browsers display MathML themselves, so a page needs a stylesheet and, for a few glyphs, a small font, but no script. This package is an unofficial community port of Temml by Ron Kok.

The package targets net10.0 and netstandard2.0.

Terminal window
dotnet add package Temml

TemmlRenderer.RenderToString takes a TeX expression, without $ delimiters, and returns MathML markup. In C#, write TeX in a verbatim string (@"...") or a raw string literal, so backslashes stay as written.

var mathml = TemmlRenderer.RenderToString(@"c = \pm\sqrt{a^2 + b^2}");
Console.WriteLine(mathml); // <math>...</math>

snippet source | anchor

A browser shows that expression like this:

c = \pm\sqrt{a^2 + b^2}
c=±a2+b2

Set DisplayMode for an equation on its own line, with large operators and limits above and below.

var display = TemmlRenderer.RenderToString(
@"\sum_{k=1}^{n} k = \frac{n(n+1)}{2}",
new TemmlOptions { DisplayMode = true }
);
Console.WriteLine(display); // <math display="block" ...>...</math>

snippet source | anchor

\sum_{k=1}^{n} k = \frac{n(n+1)}{2}
∑k=1nk=n(n+1)2

RenderToMathMLTree returns the build tree before it becomes a string. Change it, then call ToMarkup.

var tree = TemmlRenderer.RenderToMathMLTree(@"e^{i\pi} + 1 = 0");
tree.Classes.Add("euler");
Console.WriteLine(tree.ToMarkup()); // <math class="euler">...</math>

snippet source | anchor

Serve one of the Temml stylesheets together with Temml.woff2, and write the markup into an HTML5 page. The files are embedded in the package. See Styles and fonts.

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/css/Temml-Local.css">
</head>
<body>
<!-- the string RenderToString returned -->
</body>
</html>
  • Options lists every setting.
  • Errors explains what happens with invalid TeX.
  • DOM and auto-render renders into an AngleSharp document, or finds the math in a whole page.
  • Security covers TeX from users.

MIT · Copyright 2020 Ron Kok · unofficial community port of Temml