---
title: Math and diagrams
subtitle: Write math in LaTeX (rendered by KaTeX at build time), and draw flowcharts, sequence diagrams, and state diagrams with Mermaid.
description: Using inline and block LaTeX math (KaTeX) in ButterIX, plus Mermaid diagrams (flowchart, sequence, state, gantt, and more), including dark mode and on-demand loading.
---

## Math

Math is rendered to HTML by KaTeX **at build time**, so readers' browsers don't need to run any math typesetting scripts. Only pages that use math load the KaTeX stylesheet.

### Inline math

```mdx
Rate limiting uses a token bucket: $r$ tokens are refilled per minute and the bucket holds $b$, so bursts are capped at $b$ requests.
```

Rate limiting uses a token bucket: $r$ tokens are refilled per minute and the bucket holds $b$, so bursts are capped at $b$ requests.

### Block math

```mdx
$$
t_{\text{wait}} = \max\left(0,\ \frac{1 - n_{\text{tokens}}}{r}\right) \times 60\,\text{s}
$$
```

$$
t_{\text{wait}} = \max\left(0,\ \frac{1 - n_{\text{tokens}}}{r}\right) \times 60\,\text{s}
$$

Another example — the BM25 formula used to score document chunks during retrieval:

$$
\operatorname{score}(D, Q) = \sum_{i=1}^{n} \operatorname{IDF}(q_i) \cdot \frac{f(q_i, D)\,(k_1 + 1)}{f(q_i, D) + k_1 \left(1 - b + b \cdot \frac{|D|}{\text{avgdl}}\right)}
$$

<Note>
  Math syntax errors don't fail the build: KaTeX shows the faulty part as red source text so it's easy to find.
</Note>

## Mermaid diagrams

Code blocks with the language `mermaid` are rendered as diagrams. The Mermaid script loads on demand only on pages that contain diagrams, and diagrams re-render automatically to follow the site's light/dark mode. Click a diagram to enlarge it into a floating window, where you can zoom with the wheel, drag, and double-click to reset.

### Zoom and pan

Diagrams taller than 120px show a set of buttons on hover: zoom in, zoom out, pan in four directions, reset, and the floating window. On touch screens the buttons are always visible. Holding <kbd>Ctrl</kbd> / <kbd>⌘</kbd> while scrolling (or pinching on a trackpad) zooms as well, and once a diagram is zoomed or panned you can drag it.

Add parameters after the language to adjust a single diagram:

````mdx
```mermaid controls={true} placement="top-left"
flowchart LR
  A --> B
```
````

| Parameter | Values | Description |
|---|---|---|
| `controls` (or `actions`) | `{true}`, `{false}` | Always or never show the buttons; left out, the height of the diagram decides |
| `placement` | `top-left`, `top-right`, `bottom-left`, `bottom-right` | Where the buttons are, `bottom-right` by default |

### Flowcharts

````mdx
```mermaid
flowchart TD
  A[git push] --> B{Production branch?}
  B -- Yes --> C[Production build]
  B -- No, pull request --> D[Preview build]
  C --> E[Switch production pointer]
  D --> F["Preview URL site--shortid"]
```
````

```mermaid
flowchart TD
  A[git push] --> B{Production branch?}
  B -- Yes --> C[Production build]
  B -- No, pull request --> D[Preview build]
  C --> E[Switch production pointer]
  D --> F["Preview URL site--shortid"]
```

### Sequence diagrams

```mermaid
sequenceDiagram
  participant R as Reader
  participant S as Docs site runtime
  participant L as Model provider
  R->>S: POST /_biti/ai/chat
  S->>S: Retrieve doc chunks
  S-->>R: event: sources
  S->>L: System prompt + chunks + question
  L-->>S: Streamed text
  S-->>R: event: text (repeated)
  S-->>R: event: done
```

### State diagrams

```mermaid
stateDiagram-v2
  [*] --> queued
  queued --> building
  queued --> canceled
  building --> ready
  building --> failed
  ready --> [*]
  failed --> [*]
```

The diagram above is exactly the state flow of a [deployment](../deploy/previews-and-rollbacks.mdx) on the platform.

### Supported diagram types

Every Mermaid diagram type is available. Common ones include `flowchart`, `sequenceDiagram`, `classDiagram`, `stateDiagram-v2`, `erDiagram`, `gantt`, `pie`, `journey`, `gitGraph`, `mindmap`, and `timeline`.

<Info>
  Diagrams are rendered with `securityLevel: "strict"`: HTML in nodes and scripts in click callbacks are never executed. `%%{init}%%` can't change the security settings, the style text or the fonts either. When rendering fails, the source and error message are shown.
</Info>

### In the Markdown version

In a page's `.md` version and in `llms-full.txt`, Mermaid diagrams are kept as `mermaid` code blocks, so AI can read the diagram structure directly.
