Skip to content

Errors

By default, invalid TeX does not throw. The result is a span with the class temml-error that holds the source and the error text, colored with ErrorColor. The source is escaped.

// ThrowOnError is false by default: the result shows the source and the error.
var markup = TemmlRenderer.RenderToString(@"\sqrt{", new TemmlOptions { ErrorColor = "#cc0000" });
Console.WriteLine(markup); // <span class="temml-error" ...>...</span>

snippet source | anchor

The markup for \sqrt{ looks like this:

<span class="temml-error" style="color:#cc0000;white-space:pre-line;">\sqrt{
ParseError: Expected &#x27;}&#x27;, got &#x27;EOF&#x27; at end of input: \sqrt{</span>

An unsupported command inside otherwise valid TeX is different: the command name is shown in the error color and the rest of the expression renders.

x + \unknowncommand + y
x+\unknowncommand+y

With ThrowOnError, an unsupported command throws too.

Set ThrowOnError to get a ParseError.

try
{
TemmlRenderer.RenderToString(@"\sqrt{", new TemmlOptions { ThrowOnError = true });
}
catch (ParseError error)
{
Console.WriteLine(error.Message); // the message upstream Temml reports
Console.WriteLine(error.RawMessage);
Console.WriteLine(error.Position); // 6, the offset in the input
}

snippet source | anchor

Member Value
Message The message upstream Temml reports, including the position and the underlined context.
RawMessage The description of the error without the position and context.
Position The zero-based offset of the problem in the input, or null when the error has no location. The message counts from one.

ThrowOnError only covers errors in the TeX. Any other exception propagates whatever the option says. A few malformed inputs, such as x^, make upstream Temml fail with a JavaScript TypeError. The port throws an InvalidOperationException for those, so catch it too when the TeX comes from users.

When the TeX is yours, for example in a static site, set ThrowOnError so a mistake fails the build instead of reaching a page.

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