Skip to content

Math

Use KaTeX for inline and display mathematics at build time. Readers download no script.

Write formulas in TeX and Hugo turns them into HTML and MathML with KaTeX while it builds the site, so readers download one local stylesheet and no script, and nothing is sent to a remote math service. Use \(…\) inside a sentence, $$…$$ or \[…\] for a formula on its own line, and the math and chem fences where you cannot change the site configuration. For TikZ drawings or macro packages KaTeX does not support, use a pre-rendered image.

Site prerequisites

The $$, \[…\] and \(…\) delimiters work only after you enable Goldmark’s passthrough extension in your site’s own configuration; Hugo does not inherit markup settings from a theme, so OINK cannot do it for you. A math: true key, in front matter or in params, does not enable anything: without this block, $$ stays on the page as literal text.

hugo.yml
markup:
  goldmark:
    parser:
      attribute:
        block: true # numbered equations need the attribute line
    extensions:
      passthrough:
        enable: true
        delimiters:
          block: [['\[', '\]'], ['$$', '$$']]
          inline: [['\(', '\)']]

A single $ is deliberately left out of the inline delimiters, so a price such as “$5” in prose is never read as the start of a formula. Configuration describes each key. The math and chem fences and the eq shortcode need none of this.

Shortest form

An inline formula sits in the sentence; keep the surrounding spaces and punctuation outside the delimiters.

Source
The shared buffer hit ratio is \(\mathrm{hit} = \frac{H}{H + R}\), where \(H\) is `blks_hit` and \(R\) is `blks_read`.

The shared buffer hit ratio is hit=HH+R\mathrm{hit} = \frac{H}{H + R}, where HH is blks_hit and RR is blks_read.

Display formulas

Put a formula in its own paragraph between $$ delimiters, or \[…\], to show it larger and centered.

Source
A B-tree with fan-out \(f\) over \(N\) keys has height:

$$
h = \left\lceil \log_{f} N \right\rceil
$$

A B-tree with fan-out ff over NN keys has height:

h=⌈log⁡fN⌉ h = \left\lceil \log_{f} N \right\rceil

A formula too long for the column scrolls sideways inside it instead of widening the page.

The math fence

A math fence is another way to write a display formula. It needs no passthrough configuration, and GitHub shows it as an ordinary code block.

Source
```math
N_{\text{conn}} = \lambda \cdot \bar{t}_{\text{resp}}
```
Nconn=λ⋅tˉrespN_{\text{conn}} = \lambda \cdot \bar{t}_{\text{resp}}

This is Little’s law applied to a connection pool: in steady state, the connections you need equal the arrival rate times the mean response time, which is why a pool can be much smaller than the number of clients.

Chemistry and units

A chem fence typesets a \ce{…} body with KaTeX’s mhchem extension, which also handles physical units.

Source
```chem
\ce{CO2 + H2O <=> H2CO3 <=> H+ + HCO3^-}
```
COX2+HX2O⇌HX2COX3⇌HX++HCOX3X−\ce{CO2 + H2O <=> H2CO3 <=> H+ + HCO3^-}

The mhchem manual documents the syntax.

Numbered equations

Put an attribute line under a display formula to number it. You write the number yourself in num, such as 3-1 or 5.3, because the theme does not count; the anchor defaults to #eq-<num>. The number appears at the right of the formula after a localized “Equation” label.

Source
$$
\text{WAL}_{\text{day}} \approx \text{TPS} \times \bar{s}_{\text{record}} \times 86400
$$
{#eq-wal num="3-1" caption="Estimating daily WAL volume"}

See [Equation 3-1](#eq-wal): multiply by the retention period for the floor on archive disk size.
WALday≈TPS×sˉrecord×86400 \text{WAL}_{\text{day}} \approx \text{TPS} \times \bar{s}_{\text{record}} \times 86400
Equation 3-1 Estimating daily WAL volume

See Equation 3-1: multiply by the retention period for the floor on archive disk size.

caption is optional plain text, and both #id and caption need num. An incomplete attribute line warns and drops the parts it cannot use; a duplicate target warns and keeps the first. On a narrow screen a long caption wraps inside the column.

Cross references

An ordinary link reaches a numbered equation on the same page, as above. To link across pages, or to have the “Equation N” label written for you, use xref:

Source
Capacity planning starts from {{< xref eq="3-1" anchor="eq-wal" />}}.

Capacity planning starts from Equation 3-1.

xref may come before the equation it points to. For a book-wide list of equations and the book-equations index, see publishing books.

The eq shortcode

eq renders with the same KaTeX engine for sites that cannot enable passthrough, such as a migrated manuscript or a site whose hugo.yml you do not control. Without parameters it is an unnumbered display formula; with num it matches the attribute-line form. Where you can enable passthrough, $$ is shorter.

Source
{{< eq >}}\sigma_{\text{idx}} = \frac{\text{rows}_{\text{matched}}}{\text{rows}_{\text{total}}}{{< /eq >}}

{{< eq num="3-2" caption="Where a sequential scan and an index scan cost the same" >}}
c_{\text{seq}} \cdot P = c_{\text{rand}} \cdot \sigma \cdot T
{{< /eq >}}
σidx=rowsmatchedrowstotal\sigma_{\text{idx}} = \frac{\text{rows}_{\text{matched}}}{\text{rows}_{\text{total}}}
cseq⋅P=crand⋅σ⋅Tc_{\text{seq}} \cdot P = c_{\text{rand}} \cdot \sigma \cdot T
Equation 3-2 Where a sequential scan and an index scan cost the same

Output

No form loads JavaScript. A page with formulas loads a local katex.min.css and the WOFF2 fonts it names; a page without formulas loads neither. Print shows formulas the same way, without scrolling. The Markdown output keeps the TeX source as written, except that an eq shortcode becomes **Equation 3-2.** caption followed by a $$ block. RSS carries rendered KaTeX HTML and MathML; a numbered $$ block gets a bold “Equation N.” line before it, and a numbered eq keeps its “Equation N” caption below the formula.

Parameter reference

The four ways to write a formula:

\(…\) , Placementinline
Needs the site’s passthrough configuration; takes no attributes
$$…$$ / \[…\] , Placementdisplay
Needs the site’s passthrough configuration; an attribute line below it numbers it
```math , Placementdisplay fence
Works without passthrough; takes no attributes
```chem , Placementdisplay fence
Works without passthrough; the body is \ce{…}

The attribute line {…} under a display formula:

num , string , default—
[0-9A-Za-z.-]+; registers a numbered equation and shows “Equation N” at the right
#id , identifier , defaulteq-<num>
[A-Za-z][A-Za-z0-9_.:-]*; the anchor and cross-reference target
caption , plain text , default—
Caption after the number; requires num

The eq shortcode:

num , string , default—
As above; without it the formula is an unnumbered display formula
id , identifier , defaulteq-<num>
Requires num
caption , plain text , default—
Requires num
class , class list , default—
Requires num; passed through for site CSS
Body , TeX , default—
Required, non-empty

Invalid TeX does not stop the build. It warns with KaTeX’s error message and the source position, and the page shows the expression as written, so search the build log for these warnings before you publish.

Limits

  • Only $$ blocks and eq can be numbered. The math fence takes no attribute line, so switch forms when you need a number.
  • The theme never counts or renumbers equations. When you reorder chapters, update num yourself.
  • Inline formulas take no attributes; the attribute line applies only to display formulas.
  • caption is plain text, so Markdown in it appears literally.