Skip to content

Macros and extensions

Put macros in Macros. The key is the command name with its backslash, and the value is its expansion. #1 to #9 stand for arguments. A string converts to TemmlMacro implicitly; a TemmlMacro can also wrap a function, as macros can upstream.

var options = new TemmlOptions
{
Macros = new TemmlMacroDictionary
{
[@"\RR"] = @"\mathbb{R}",
[@"\norm"] = @"\lVert #1 \rVert",
},
};
Console.WriteLine(TemmlRenderer.RenderToString(@"\norm{x} \in \RR", options));

snippet source | anchor

\def\RR{\mathbb{R}} \def\norm#1{\lVert #1 \rVert} \norm{x} \in \RR
‖x‖∈ℝ

\def and \newcommand last for one expression. \gdef and \global\let write into the dictionary you passed in Macros, so a later render that gets the same dictionary sees them. Pass a new dictionary to start over.

// \gdef writes into the dictionary you pass, so later renders see the macro.
var shared = new TemmlOptions { Macros = new TemmlMacroDictionary() };
TemmlRenderer.RenderToString(@"\gdef\half{\frac{1}{2}}", shared);
Console.WriteLine(TemmlRenderer.RenderToString(@"\half + \half = 1", shared));

snippet source | anchor

A dictionary that renders can write to is shared state. Do not use one instance from several threads at once.

DefinePreamble parses a block of definitions, including \definecolor, and returns the macros to pass to later renders.

var preamble = TemmlRenderer.DefinePreamble(
@"\newcommand\d[0]{\operatorname{d}\!}
\def\foo{x^2}
\definecolor{sortaGreen}{RGB}{128,128,0}"
);
var withPreamble = new TemmlOptions { Macros = preamble };
Console.WriteLine(
TemmlRenderer.RenderToString(@"\color{sortaGreen} \int \foo \, \d x", withPreamble)
);

snippet source | anchor

DefineMacro and DefineSymbol change the tables that every render in the process uses. Call them once, at startup, before rendering. Prefer Macros for anything that belongs to one document or user.

// Adds a built-in macro for every later render in the process. Do this once, at startup.
TemmlRenderer.DefineMacro(@"\QQ", @"\mathbb{Q}");
Console.WriteLine(TemmlRenderer.RenderToString(@"\sqrt{2} \notin \QQ"));

snippet source | anchor

// Adds a symbol to the built-in table, also for the whole process.
TemmlRenderer.DefineSymbol(TemmlMode.Math, "mathord", "☺", @"\smiley");
Console.WriteLine(TemmlRenderer.RenderToString(@"x = \smiley"));

snippet source | anchor

Upstream ships mhchem, physics, and texvc as extensions that its temml.cjs bundle includes. Here they are part of the Temml package and always available. There is nothing to load.

// mhchem, physics, and texvc are always available.
Console.WriteLine(TemmlRenderer.RenderToString(@"\ce{2H2 + O2 -> 2H2O}"));
Console.WriteLine(TemmlRenderer.RenderToString(@"\pdv{f}{x}"));
Console.WriteLine(TemmlRenderer.RenderToString(@"\R^n"));

snippet source | anchor

Extension Provides
mhchem \ce for chemical equations and \pu for physical units.
physics Many commands of the LaTeX physics package, such as \dv, \pdv, and \bra.
texvc Commands used on MediaWiki pages, such as \R.
\ce{2H2 + O2 -> 2H2O}
2H2+O2→2H2O
\pdv{f}{x} \qquad \braket{\phi}{\psi}
∂f∂x⟨ϕ⟩ψ

Upstream’s copy-tex extension changes what a browser copies to the clipboard. It has no server equivalent and is not ported.

Temml’s supported functions page lists them, and its support table sorts them alphabetically.

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