Skip to content

Math & diagrams

astro-slides renders math and diagrams from plain fenced/inline markup. Math and PlantUML render at build time (they reduce to HTML or an <img> — zero runtime bytes); Mermaid is the one lazily-hydrated diagram, loaded only when a slide actually contains one.

Math is rendered with KaTeX at build time. Use $…$ for inline math and $$…$$ for a centered display block.

Euler's identity is $e^{i\pi} + 1 = 0$, inline in a sentence.
$$
\int_0^\infty e^{-x^2}\,dx = \frac{\sqrt{\pi}}{2}
$$

If a single expression is malformed, KaTeX renders that one node in red (throwOnError: false) rather than failing the whole build.

A display block can reveal one equation row per click. Put a click-step spec in {…} immediately after the opening $$, and split rows with \\. Each \\-separated row maps to a step (all reveals the remaining rows), driven by the deck click model and numbered after prose and code clicks.

$$ {1|2|all}
a^2 + b^2 = c^2 \\
E = mc^2 \\
\nabla \cdot \mathbf{E} = \frac{\rho}{\varepsilon_0}
$$

Here row 1 shows on step 1, row 2 on step 2, and the last row on the final step.

KaTeX’s stylesheet is only linked into a deck when that deck actually uses math (the katex feature flag, detected by the parser). Math-free decks ship no KaTeX CSS.

Write a Mermaid diagram in a mermaid fenced block. It renders on the client, into an SVG mounted in a Shadow DOM (so Mermaid’s injected CSS can’t leak into your deck, or vice versa). The Mermaid library is heavy, so it’s imported lazily and only when a slide contains a diagram — Vite code-splits it into its own chunk fetched on demand.

```mermaid
flowchart LR
A[Author] --> B{Build}
B -->|dev| C[Preview]
B -->|export| D[PDF / PPTX]
```

By default the diagram theme follows the active color scheme (light/dark) and re-renders when the scheme changes. You can override per-diagram via fence options after the language tag (comma-separated key: value pairs):

```mermaid theme: neutral, scale: 0.8
sequenceDiagram
Author->>Deck: dispatch(next)
Deck-->>Author: slide 2
```
  • theme — a Mermaid theme name (default, dark, neutral, forest, …). Overrides the automatic scheme-following.
  • scale — a CSS transform scale factor applied to the rendered SVG.

A plantuml (or puml) fenced block is encoded at build time into a URL on a PlantUML server and rendered as a plain <img> — no client JavaScript. Bare source is wrapped in @startuml/@enduml automatically.

```plantuml
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
```

The server defaults to the public plantuml.com renderer; point it at your own instance with the plantumlServer integration option.

  • packages/core/src/math/katex.tsrenderMath via katex.renderToString.
  • packages/core/src/math/remark-katex.tsinlineMath / math<KaTeX>, stepped rows.
  • packages/core/src/diagrams/remark-diagrams.ts — mermaid/plantuml fences → components.
  • packages/core/src/diagrams/plantuml.ts — build-time encoding + server URL.
  • packages/client/src/diagrams/mermaid.ts — lazy load, Shadow DOM, scheme sync, option parsing.
  • packages/client/components/KaTeX.astro — inline vs block KaTeX wrapper.
  • packages/client/components/Mermaid.astro — the lazily-hydrated diagram island.
  • packages/client/components/PlantUml.astro — the build-time-encoded <img>.
  • packages/core/src/routes/slide.astro — conditional KaTeX <link> via the katex feature.
  • docs/built/09-math-and-diagrams.md — phase write-up.