Skip to main content

Maths

Twain renders mathematics with KaTeX. The fonts and styles are bundled into the app, so maths renders offline with no network access.

The KaTeX web fonts are not embedded in an HTML export, so maths in an exported file falls back to the reader's system serif and looks a little different. Printing and PDF export render through the app, so they keep the KaTeX fonts.

Inline maths

Wrap an expression in single dollar signs to set it inline with your text:

Euler's identity, $e^{i\pi} + 1 = 0$, is often called beautiful.

Single-dollar inline maths is always on and cannot be turned off, so a pair of ordinary dollar signs in the same paragraph is read as an expression. Writing It costs $5 and the other is $6 renders 5 and the other is as maths. Escape a literal dollar sign as \$ (It costs \$5) and it prints normally.

Display maths

Wrap an expression in double dollar signs for a centred block:

$$
\int_{-\infty}^{\infty} e^{-x^2}\,dx = \sqrt{\pi}
$$

A fenced code block tagged math is treated as display maths as well:

```math
E = mc^2
```

The LaTeX delimiters \(\) and \[\] are not recognised. The backslashes are consumed as Markdown escapes, so \(x^2\) renders as the plain text (x^2).

Maths in the editor

When you edit a Markdown file in the rendered pane, a rendered equation is atomic: you cannot type inside it, though you can delete it as a unit. Editing the text around it writes the equation back out as $…$ or $$…$$ with the original LaTeX preserved. In the source pane you edit the $…$ text directly. Editing is part of Twain Pro.

When an expression is invalid

Twain is lenient. A bad expression never stops the document from rendering. If KaTeX does not recognise a command, that command alone is shown in red and the rest of the expression still renders. If the expression cannot be parsed at all, the whole of its raw source is shown in red, with the KaTeX error message in the hover tooltip. Either way the rest of the document is unaffected, so a single typo never blanks the page.