millefeuille — Interactive OCG Playground
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.
printocg, viewocg, exportocg, and parent: hierarchies.
gap, gapinline, correction-flow & correction-ocg(overlay: true)
#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._
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.
ocg-steps, ocg-stepper, ocg-step-zone & ocg-goto
#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])
ocg-steps / ocg-uncover / ocg-only)ocg-only("sol", "2-3"): Both roots have the same sign because their product is positive.
ocg-stepper, ocg-step-zone, ocg-goto)
0 / 3 active
ocg-zone-all & ocg-cycle / ocg-radio
#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$.])
Which of these numbers is prime?
ocg-when, ocg-all, ocg-any & ocg-not
#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]
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
ocg-hover, hover-word & hover-phrase
#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.]
mode: "show") — "This text is not visible."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.
ocg-canvas / ocg-group & parent: Hierarchy
#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]
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").
code-gap / code-gaps & ocg-scope
#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])
]
sol@ex1
sol@ex2
ocg-link / ocg-colorlinks & ocg-hide-aware (Touying)
// Screen-only link highlight
#show: ocg-colorlinks.with(
color: blue, stroke: 0.6pt + blue, radius: 2pt,
url-color: red, url-stroke: 0.6pt + red,
)
See #link("https://typst.app")[Typst docs].
// Touying slides: place before theme show rule
#show: ocg-hide-aware
#show: simple-theme.with(aspect-ratio: "16-9")
== Exercise
$12 + 7 =$ #gap[$19$] \
#pause
$5 times 6 =$ #gap[$30$]
Visit https://typst.app and jump to the sortable table.
ocg-hide-aware)
Sortable Table (▲ / ▼ per column & order)
#ocg-sort-table(
([Function], [Category], [Layers]),
(
("gap", "Inline", "1"),
("ocg-steps", "Sequence", "N"),
("ocg-canvas", "Vector", "K"),
),
toggle-order: true,
)
| Function | Category | Layers |
|---|
ocg-switch)
Bilingual Switch (EN / FR) & External Package (lilaq, asymptote x = 0)
// 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
ocg-switch)
Active: EN
lilaq plot, vertical asymptote x = 0)
State 1: Curve only
Click to cycle lilaq diagram: curve alone ↔ vertical asymptote at x = 0.
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.