Esc

comark-mathml

Formulas written in LaTeX, typeset while parsing, in place of Comark's math plugin.

Playground

The pond holds nn ducks, and each pair shares one roll, so the bakery bakes

(n2)=n(n−1)2\binom{n}{2} = \frac{n(n-1)}{2}

rolls a day.

  • $…$ and \(…\) write inline math, and $$…$$, \[…\], and a math fence write display math.
  • Formulas become MathML while parsing, drawn in the browser's math font, so the page loads no script, font, or stylesheet.
  • Prices such as $5 and $10, citations such as \[1\], and anything in code stay text.
  • A formula that doesn't typeset shows its TeX and adds an issue.
  • The MathML keeps the TeX for screen readers and copying.

Write inline math between single $ signs, or between \( and \). As in Pandoc, the opening $ can't come before a space or a digit, and the closing one can't follow a space or come before a digit. Amounts of money stay text.

Playground

A duck with mm grams of bread and kk friends keeps mk+1\frac{m}{k+1} grams.

Write display math between $$ signs, between \[ and \], or in a fence with the language math. A display formula sits centered on its own line and scrolls sideways when it's wider than the page.

Playground
∫0∞e−x2dx=π2\int_0^\infty e^{-x^2}\,dx = \frac{\sqrt{\pi}}{2}∑i=1ni=n(n+1)2\sum_{i=1}^{n} i = \frac{n(n+1)}{2}

Code spans and code blocks never hold math. To show a dollar sign before a letter, escape it as \$x.

Use the macros option to define commands every formula can use.

comarkUi({ math: { macros: { "\\R": "\\mathbb{R}" } } });
ts
  • A $ followed by a digit or a space doesn't open a formula, as in $5 and $10, US$20, and $ x $.
  • Brackets around numbers, such as \[1\] and \[2, 3\], are citations.
  • An invalid formula shows its TeX and adds an issue. While streaming, an unfinished formula may add one until the rest arrives.

Remove comark/plugins/math, KaTeX's stylesheet, and its Math component when you add comark-mathml. Comark's plugin leaves the TeX in the tree for a component to typeset with KaTeX in the reader's browser. This one typesets while parsing, so the page loads no KaTeX.

For KaTeX's own fonts and layout instead of the browser's math font, set output to "html", or to "htmlAndMathml" to keep MathML for screen readers. Install katex and put its stylesheet on the page:

pnpm add katex
import "katex/dist/katex.css";

comarkUi({ math: { output: "htmlAndMathml" } });
ts

Pass options under math in comarkUi(), or to math() alone.

outputDefault: mathml

"mathml" typesets with Temml. "html" and "htmlAndMathml" typeset with KaTeX, which needs katex installed and katex/dist/katex.css on the page.

macrosDefault: {}

TeX commands every formula can use, such as { "\\R": "\\mathbb{R}" }.

  • \href, \url, and \includegraphics don't render.
  • A macro that calls itself stops after 1,000 expansions.
  • Sizes go up to 20em.

Temml writes MathML that Chrome, Firefox, and Safari draw alike. KaTeX writes its MathML for screen readers, and Chrome draws it without \cancel, \boxed, wide accents such as \widehat, or \mathbb letters.

The plugin adds these to meta.issues, as Validation describes:

  • A formula whose TeX doesn't typeset, with the typesetter's reason. Its TeX shows in its place.
  • An output that needs KaTeX when katex isn't installed. Formulas fall back to MathML.
Math `\frac{a}` failed to typeset: Unexpected end of input in a macro argument, expected '}'. Fix the TeX or wrap it in code.
txt

Each has its own rule in Doctor.

comark-ui installs and registers comark-mathml. To use it alone, install it with Comark and a renderer, and register it instead of comark/plugins/math:

pnpm add comark @comark/html comark-mathml
import { createHtmlRenderer } from "@comark/html";
import math, { components } from "comark-mathml/html";

const render = createHtmlRenderer({ plugins: [math()], components });
ts

For Vue, React, or Svelte, import from comark-mathml/vue, comark-mathml/react, or comark-mathml/svelte. The components add the styles on mount. On a server-rendered page, also import comark-mathml/style.css so it doesn't shift. Renderers covers using it with other plugins.

Formulas take the text's color and the system's math font. The rules sit in a comark CSS layer, so any plain rule of yours wins.

Token Used for
--comark-muted The TeX of a formula that didn't typeset
Variable Default Used for
--cmh-font "STIX Two Math", "Cambria Math", "Latin Modern Math", "Noto Sans Math", math The formulas' font
Class Element
.cmh The span around a formula's math
.cmh-display Added to display math, centered and scrolling sideways
.cmh-source Added when the formula didn't typeset and shows its TeX