Math

glu has one math engine and two ways in: TeX between dollars in Markdown, and <math> elements in HTML. Markdown turns the TeX into MathML, so both end in the same place, and raw <math> elements work in Markdown as well. For a worked example, see Typeset TeX math.

A math font is required

The engine needs a font carrying an OpenType MATH table (Latin Modern Math, STIX Two Math, XITS Math, Cambria Math, …). Point the <math> element at it through CSS, whether you write it yourself or it is generated from the dollars:

@font-face {
  font-family: "Latin Modern Math";
  src: url("latinmodern-math.otf");
}

/* The <math> element looks up font-family on itself, so the engine can
   find the MATH-table font. */
math {
  font-family: "Latin Modern Math";
  font-size: 12pt;
}

TeX in Markdown

Set math: true in the frontmatter to write formulas with TeX dollar syntax. Inline math goes between single dollars, display math between double dollars. The TeX is converted to MathML and typeset by the same OpenType-MATH engine as <math> elements in HTML, so it needs no Pandoc, no JavaScript and no external converter.

---
title: TeX math in Markdown
lang: en
css: math.css
math: true
---

The mass-energy equivalence `$E=mc^2$` sits inline with the prose.

The quadratic formula as a display equation:

`$$x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}$$`

(The backticks above are only there so this page does not itself parse the example as math. In your document write the dollars directly: $E=mc^2$ and $$…$$ on their own.)

Math parsing is off by default so that a $ used as a currency sign is never reinterpreted. It is a real Markdown inline parser, not a search and replace, so dollars inside `code spans` and fenced code blocks are left untouched.

Predictable dollars (Pandoc/PLOS rule)

To keep prose with stray dollars safe, opening and closing delimiters follow the same strict rule Pandoc uses and PLOS expects for Markdown submissions:

  • The opening $ must not be followed by whitespace.
  • The closing $ must not be preceded by whitespace.
  • The closing $ must not be directly followed by a digit.

So it costs $5 and $10 stays plain text, while $x+1$ is a formula. Display math ($$…$$) is matched on a single line.

Supported TeX subset

This is a focused subset aimed at typical prose mathematics, not a full LaTeX engine:

Construct Examples
Variables, digits, operators a, 42, 3.14, + - = < >
Superscript / subscript x^2, a_i, x_i^2 (combine to limits/scripts)
Fractions \frac{a}{b}, \frac12
Roots \sqrt{x}, \sqrt[3]{x}
Greek letters \alpha, \beta, \Sigma, \omega, …
Relations / operators \leq, \geq, \neq, \approx, \pm, \times, \cdot, …
Arrows \to, \rightarrow, \Rightarrow, \mapsto
Big operators \sum, \prod, \int: sums and friends stack their limits above/below in display style, integrals keep side scripts (TeX nolimits convention); inline everything is a script pair
Named operators \sin, \cos, \log, \lim, \limsup, \max, …
Primes f', f'', f''' (set as ord atoms after the base, since OpenType math primes are designed pre-raised); f'^2 attaches the superscript after the marks
Stretchy fences \left( … \right), \left\lfloor … \right\rfloor, invisible \left.; delimiters grow with the content via font variants and glyph assemblies
Delimiters \lfloor, \rfloor, \lceil, \rceil, \langle, \rangle, \vert, \Vert, \lbrace, \rbrace
Upright text \mathrm{...}, \text{...}
Math style \displaystyle, \textstyle: set the rest of their group in display or text style
Spacing \, (3/18 em), \: (4/18 em), \; (5/18 em), \! (-3/18 em), \ (1/4 em), \quad (1 em), \qquad (2 em)

Grouping with { … } works everywhere. An unknown command degrades to its name as an upright identifier rather than failing the render, so one stray macro never aborts a document.

Not yet covered: multi-line display blocks, matrices and \begin{} environments, and accents such as \hat or \vec. The \(…\) and \[…\] delimiters are not recognized; use the dollar forms.

MathML in HTML

<math> elements are typeset directly. Embed the formula where it belongs:

<head>
  <meta name="pdf-format" content="PDF/UA-2">
  <title>MathML demo</title>
  <style>
    @font-face { font-family: "LM Math"; src: url("latinmodern-math.otf"); }
    math { font-family: "LM Math"; font-size: 12pt; }
  </style>
</head>
<body>
  <p>The quadratic formula
  <math>
    <mi>x</mi><mo>=</mo>
    <mfrac>
      <mrow><mo>-</mo><mi>b</mi><mo>&#xb1;</mo>
        <msqrt><msup><mi>b</mi><mn>2</mn></msup><mo>-</mo>
        <mn>4</mn><mi>a</mi><mi>c</mi></msqrt></mrow>
      <mrow><mn>2</mn><mi>a</mi></mrow>
    </mfrac>
  </math>
  solves a quadratic equation.</p>
</body>

See Mathematics (MathML) for the supported elements.

Accessibility

Because every formula is MathML in the end, the two ways take the same accessibility path. Under PDF/UA each formula becomes a tagged Formula structure element with a text /Alt. Under PDF/UA-2 the MathML source is embedded as well, as an associated file (application/mathml+xml, AFRelationship /Supplement), so assistive technology can read the semantics. See Tagged PDF (PDF/UA) and PDF/UA tagging, and verify with pdfa11y or veraPDF.