millefeuille v0.1.0

ocg.typ marks Typst content with pdf.artifact and special links, which ocg_postprocess.py turns into native PDF Optional Content Groups (OCGs). Every widget below simulates the post-processed PDF interactively and stays synced with the PDF Layers Panel.

Named OCGs Blocks with the same layer name share one OCG across the PDF.
Reserved Geometry Hidden layers reserve space so surrounding text never shifts unexpectedly.
Usage States printocg, viewocg, exportocg, and parent: hierarchies.
01 · Worksheets & Overlays

gap, gapinline, correction-flow & correction-ocg(overlay: true)

Typst Source
#import "@preview/millefeuille:0.1.0": ocgen
#import ocgen: *
#let btn(x) = box(inset: 4pt, stroke: 0.5pt, x)
#let feedback(name, body) = box(width: 12cm, height: 0.6cm, ocg-layer(name, body))
#set page(foreground: render-correction-overlays())

+ $12 + 7 =$ #gap(printocg: "never")[$19$]
+ Derivative of $x^2$: #gapinline(c: green, color: blue)[$2x$].
+ Triangle angles sum to #gapinline(
    c: blue,
    stroke: (paint: red, thickness: 1pt, dash: "dashed")
  )[$180°$, provided we work in a Euclidean plane].
+ Solve $3x - 5 = 2x + 4$.
  #correction-flow[
    *Solution.* $3x - 2x = 4 + 5$, so $x = 9$.
  ]
+ Overlay correction (covers following text):
  #correction-ocg(overlay: true)[
    *Overlay Solution.* Covers the text below without shifting layout.
  ]
  _Subsequent paragraph in normal flow._
Interactive Preview (Click any cue or zone)
1. 12 + 7 = printocg: "never"
2. Derivative of x²: .
3. Triangle angles sum to .
4. Solve 3x − 5 = 2x + 4.
Click to toggle breakable correction block...
Solution. 3x − 2x = 4 + 5, so x = 9.
5. Overlay mode (correction-ocg(overlay: true)):

Following exercise text in document flow:

Compute ∫₀¹ x² dx. When overlay: true is active, the correction layer sits on top without moving this text.

Overlay Correction (foreground):
∫₀¹ x² dx = [x³/3]₀¹ = 1/3.
02 · Step-by-Step & Sequential Navigation

ocg-steps, ocg-stepper, ocg-step-zone & ocg-goto

Step 1 / 4
Typst Source
#let btn(x) = box(inset: 4pt, stroke: 0.5pt, x)

#ocg-steps("sol", 4, start: 1)
#ocg-uncover("sol", 1)[*Problem.* Factor $x^2 - 5x + 6$.]
#ocg-uncover("sol", 2)[Sum $-5$, product $6$.]
#ocg-only("sol", "2-3")[_Tip on steps 2–3!_]
#ocg-uncover("sol", 3)[They are $-2$ and $-3$.]
#ocg-uncover("sol", 4)[$(x - 2)(x - 3)$.]
#ocg-steps-nav("sol", prev: true, loop: true)

#let layers = ("s-a", "s-b", "s-c")
#ocg-layer("s-a")[① Draw the triangle.]
#ocg-layer("s-b")[② Draw the altitude.]
#ocg-layer("s-c")[③ Mark the right angle.]
#ocg-stepper(layers, prev: true)
#ocg-step-zone(layers, btn[+1 Step])
#ocg-goto(layers, 2, btn[First 2])
Numbered Steps (ocg-steps / ocg-uncover / ocg-only)
Problem. Factor x² − 5x + 6.
We look for two numbers with sum −5 and product 6.
ocg-only("sol", "2-3"): Both roots have the same sign because their product is positive.
They are −2 and −3.
Hence x² − 5x + 6 = (x − 2)(x − 3).
goto:
Named Stepper (ocg-stepper, ocg-step-zone, ocg-goto) 0 / 3 active
① Draw the triangle.
② Draw the altitude.
③ Mark the right angle.
03 · Global Controls & Radio Groups

ocg-zone-all & ocg-cycle / ocg-radio

Typst Source
#let btn(x) = box(inset: 4pt, stroke: 0.5pt, radius: 2pt, x)
#let feedback(name, body) = box(width: 12cm, height: 0.6cm, ocg-layer(name, body))

#ocg-zone-all(mode: "off", btn[Hide all])
#ocg-zone-all(mode: "on", btn[Show all])
#ocg-zone-all(mode: "toggle", btn[Invert])

*Which of these numbers is prime?*

#ocg-cycle("q1", "q1-a", btn[A. 21])
#ocg-cycle("q1", "q1-b", btn[B. 27])
#ocg-cycle("q1", "q1-c", btn[C. 29])
#ocg-cycle("q1", "q1-d", btn[D. 33])

#feedback("q1-a", text(red)[No: $21 = 3 times 7$.])
#feedback("q1-b", text(red)[No: $27 = 3^3$.])
#feedback("q1-c", text(green)[Yes! 29 is prime.])
#feedback("q1-d", text(red)[No: $33 = 3 times 11$.])
Interactive Preview

Which of these numbers is prime?

Click A, B, C, or D — selecting one activates its layer and hides the other options in group q1.
No: 21 = 3 × 7.
No: 27 = 3³.
Yes! 29 is prime.
No: 33 = 3 × 11.
04 · Conditional Content (/VE)

ocg-when, ocg-all, ocg-any & ocg-not

Typst Source
#let btn(x) = box(inset: 4pt, stroke: 0.5pt, radius: 2pt, x)

#ocg-layer("A", text(red)[A is shown])
#ocg-layer("B", text(blue)[B is shown])
#ocg-zone("A", btn[A]) #ocg-zone("B", btn[B])

#ocg-when(ocg-all("A", ocg-not("B")))[visible if A and not B]
#ocg-when(ocg-any("A", "B"))[visible if A or B]
#ocg-when(ocg-not(ocg-any("A", "B")))[visible if neither]
Interactive Preview
A: OFF B: OFF
ocg-layer("A") A is shown
ocg-layer("B") B is shown
ocg-all("A", ocg-not("B")) visible if A and not B
ocg-any("A", "B") visible if A or B
ocg-not(ocg-any("A", "B")) visible if neither
05 · Hover Enter / Exit Actions

ocg-hover, hover-word & hover-phrase

Typst Source
#let btn(x) = box(inset: 4pt, stroke: 0.5pt, radius: 2pt, x)

// hover-word is built into ocg.typ
#hover-word[This] #hover-word[text] #hover-word[is] #hover-word[not] #hover-word[visible.]

// hover-phrase with mode: "hide" hides each word while hovered
#hover-phrase(mode: "hide")[Hover over each word to hide it.]
Interactive Preview (Move mouse over words; click also supported for touch/browsers)
A. Word-by-word reveal (mode: "show") — "This text is not visible."
B. Word-by-word hide on hover (hover-phrase(mode: "hide"))

Note: PDF mouse-enter/exit actions work in Acrobat Reader and Foxit; in browsers/touch we also support toggle on click.

06 · Vector Graphics & Layer Hierarchy

ocg-canvas / ocg-group & parent: Hierarchy

Typst Source
#let btn(x) = box(inset: 4pt, stroke: 0.5pt, radius: 2pt, x)

#ocg-canvas(cetz, length: 0.8cm, on: ("altitude",), {
  import cetz.draw: *
  line((0, 0), (5, 0), (1.5, 3), close: true, stroke: 1.2pt)
  ocg-group("altitude", line((1.5, 0), (1.5, 3), stroke: blue))
  ocg-group("median", line((2.5, 0), (1.5, 3), stroke: red))
  ocg-group("bisector", line((2.5, -0.3), (2.5, 3), stroke: green))
})
#ocg-cycle("lines", "altitude", btn[Altitude])
#ocg-cycle("lines", "median", btn[Median])
#ocg-cycle("lines", "bisector", btn[Bisector])

// Child layers via parent:
#ocg-layer("frame", on: true, rect(width: 7cm, height: 2cm))
#ocg-layer("details", parent: "frame", on: true, circle(radius: 0.5cm))
#ocg-layer("caption", parent: "details", on: true)[A detail]
Interactive Preview
det
A detail (visible only when frame, details & caption are ON)

Note on ocg-fletcher and ocg-passes: They use the exact same multi-pass hiding principle as ocg-canvas — hiding non-target elements while preserving layout bounds (fletcher.hide(..., bounds: true) or custom render(target) in ocg-passes) and supporting parents: (child: "parent").

07 · Code Blocks & Namespaces

code-gap / code-gaps & ocg-scope

Typst Source
#let btn(x) = box(inset: 4pt, stroke: 0.5pt, radius: 2pt, x)

#code-gap(```python
def square(x):
    return @@x * x@@

for n in range(@@1, 6@@):
    print(n, square(n))
```)

#ocg-scope("ex1")[
  Ex 1: #ocg-layer("sol")[Solution 1]
  #ocg-zone("sol", btn[Toggle sol@ex1])
]
#ocg-scope("ex2")[
  Ex 2: #ocg-layer("sol")[Solution 2]
  #ocg-zone("sol", btn[Toggle sol@ex2])
]
Interactive Preview
def square(x):
return
for n in range():
print(n, square(n))
Exercise 1 sol@ex1
Solution 1 (scoped to ex1)
Exercise 2 sol@ex2
Solution 2 (scoped to ex2)
09 · Interactive OCG Table

Sortable Table (▲ / ▼ per column & order)

Sorted by Col 0 (ASC)
Typst Source
#ocg-sort-table(
  ([Function], [Category], [Layers]),
  (
    ("gap", "Inline", "1"),
    ("ocg-steps", "Sequence", "N"),
    ("ocg-canvas", "Vector", "K"),
  ),
  toggle-order: true,
)
Function Category Layers
10 · Cycling Switch (ocg-switch)

Bilingual Switch (EN / FR) & External Package (lilaq, asymptote x = 0)

Typst Source
// 1. Bilingual EN / FR switch
#ocg-switch(
  [English: Click to switch to French.],
  [Français : Cliquez pour passer à l'anglais.],
)

// 2. Unwrapped external package (lilaq plot)
#import "@preview/lilaq:0.2.0" as lq
#ocg-switch(
  lq.diagram(lq.plot(xs, ys-curve)),
  lq.diagram(lq.plot(xs, ys-curve), lq.vlines(0)),
)
// Vertical asymptote at x = 0
Interactive Preview (Click either switch box to cycle)
1. Bilingual EN / FR (ocg-switch) Active: EN
English: Click to switch to French.
Français : Cliquez pour passer à l'anglais.
2. External package (lilaq plot, vertical asymptote x = 0) State 1: Curve only
x y x = 0

Click to cycle lilaq diagram: curve alone ↔ vertical asymptote at x = 0.

11 · Compatibility & Distribution

PDF Reader Support & Post-Processing Modes

Feature Acrobat Reader Foxit Firefox Chrome, Edge, Sumatra
Layers, initial states yes yes yes yes
Buttons (click, steps, radio) yes yes yes no
Conditional content (ocg-when) yes yes yes initial states only
Hover (ocg-hover) yes yes no no
Layers panel yes with --layers-panel yes no (pdf24, Evince: yes)

Distributing static / print-safe PDFs: Chrome and Edge ignore OCG button actions, so for an audience using browser viewers, generate a static version via --strip all (student copy with answers removed from the PDF) or --reveal all (answer-key copy).

Printing caveat: When printing from Chrome or Edge, hidden layers are printed too unless they carry printocg: "never" (or the file was post-processed with --print-off). Use --strip when answers must never be present in the file.