|
| 1 | +# pi-games |
| 2 | + |
| 3 | +Typesetting tools for **game theory** in Typst: draw **normal-form payoff |
| 4 | +matrices** and **extensive-form game trees** with consistent per-player |
| 5 | +colours across both representations. |
| 6 | + |
| 7 | +`pi-games` is an umbrella module that bundles three libraries — a shared colour |
| 8 | +palette, normal-form tables, and extensive-form trees (built on |
| 9 | +[CeTZ](https://github.com/cetz-package/cetz)) — behind a single import. |
| 10 | + |
| 11 | +**Normal form — Prisoner's Dilemma** |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +**Extensive form — Beer–Quiche signalling game** |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +```typst |
| 22 | +#import "@preview/pi-games:0.1.0": * |
| 23 | +``` |
| 24 | + |
| 25 | +Extensive-form trees are drawn inside a `cetz.canvas` block, so import CeTZ as |
| 26 | +well when you need them: |
| 27 | + |
| 28 | +```typst |
| 29 | +#import "@preview/cetz:0.5.2" as cetz |
| 30 | +#import "@preview/pi-games:0.1.0": * |
| 31 | +``` |
| 32 | + |
| 33 | +### Normal form |
| 34 | + |
| 35 | +`game-normal-form` covers the same kind of normal-form games as the |
| 36 | +[`game-theoryst`](https://typst.app/universe/package/game-theoryst/) Typst |
| 37 | +package by Connor T. Wiegand — players, strategy lists, a payoff matrix and |
| 38 | +per-player best-response underlines — but with its own CeTZ-based API: payoffs |
| 39 | +are a single nested array and best responses are passed as `(row, col)` |
| 40 | +coordinate lists, rather than `game-theoryst`'s table cells with inline |
| 41 | +`hul()`/`vul()` markers. |
| 42 | + |
| 43 | +`game-normal-form` returns ready-to-place content (no canvas needed). The |
| 44 | +Prisoner's Dilemma above is produced by: |
| 45 | + |
| 46 | +```typst |
| 47 | +#game-normal-form( |
| 48 | + [Prisoner 1], [Prisoner 2], |
| 49 | + ([C], [D]), |
| 50 | + ([C], [D]), |
| 51 | + ( |
| 52 | + (([$3$], [$3$]), ([$0$], [$5$])), |
| 53 | + (([$5$], [$0$]), ([$1$], [$1$])), |
| 54 | + ), |
| 55 | + p1-best: ((1, 0), (1, 1)), // underline Player 1 best responses |
| 56 | + p2-best: ((0, 1), (1, 1)), // underline Player 2 best responses |
| 57 | + nash: ((1, 1),), // outline the Nash equilibrium cell |
| 58 | +) |
| 59 | +``` |
| 60 | + |
| 61 | +Use `game-three-player-normal-form` for three-player games, rendered as one |
| 62 | +sub-matrix per Player 3 strategy. |
| 63 | + |
| 64 | +### Extensive form |
| 65 | + |
| 66 | +The tree style replicates the look of the |
| 67 | +[`xgames`](https://carlabernard.ch/beni/downloads/xgames.pdf) LaTeX package by |
| 68 | +Benjamin Bernard — per-player coloured node labels, action labels and payoff |
| 69 | +vectors — reimplemented natively in Typst/CeTZ. |
| 70 | + |
| 71 | +Build trees from nodes, branches, terminals and information sets inside a |
| 72 | +`cetz.canvas`. The Beer–Quiche signalling game above uses `game-nature`, |
| 73 | +`game-node`, `game-branch`, `game-prob`, `game-terminal` and bracket-style |
| 74 | +`game-infoset` calls. A minimal tree: |
| 75 | + |
| 76 | +```typst |
| 77 | +#cetz.canvas({ |
| 78 | + import cetz.draw: * |
| 79 | + game-branch((0, 0), (-1.5, -2), action: [Out], player: 1, side: "w") |
| 80 | + game-terminal((-1.5, -2), payoffs: ([0], [2])) |
| 81 | + game-branch((0, 0), (1.5, -2), action: [In], player: 1, side: "e") |
| 82 | + game-terminal((1.5, -2), payoffs: ([1], [1])) |
| 83 | + game-node((0, 0), player: 1, label: [Player 1], la: "n") |
| 84 | +}) |
| 85 | +``` |
| 86 | + |
| 87 | +## Public API |
| 88 | + |
| 89 | +**Normal form** |
| 90 | + |
| 91 | +| Function | Description | |
| 92 | +|---|---| |
| 93 | +| `game-normal-form(p1, p2, s1, s2, payoffs, ...)` | N×M payoff matrix with coloured payoffs, best-response underlines and Nash highlights | |
| 94 | +| `game-three-player-normal-form(p1, p2, p3, s1, s2, s3, payoffs, ...)` | Three-player game as a row of N×M sub-matrices | |
| 95 | + |
| 96 | +**Extensive form** |
| 97 | + |
| 98 | +| Function | Description | |
| 99 | +|---|---| |
| 100 | +| `game-node(pos, player, label, ...)` | Decision node (filled, open, or dot style) | |
| 101 | +| `game-nature(pos, label, ...)` | Nature / chance node (grey) | |
| 102 | +| `game-terminal(pos, payoffs, ...)` | Terminal node with a coloured payoff vector | |
| 103 | +| `game-branch(from, to, action, ...)` | Branch with an optional action label | |
| 104 | +| `game-prob(from, to, action, ...)` | Nature-branch probability label (wrapper around `game-branch`) | |
| 105 | +| `game-infoset(...pts, player, style, ...)` | Information set, `"dashed"` or `"bracket"` style | |
| 106 | +| `game-subgame(apex, depth, width, ...)` | Proper-subgame triangle marker | |
| 107 | +| `game-highlight(from, to, color, ...)` | Bold overlay marking an equilibrium path | |
| 108 | +| `game-payoffs(payoffs, parens)` | Inline coloured payoff vector for body text | |
| 109 | +| `game-player(player, label)` / `game-player-default(player)` | Player name in the player's colour | |
| 110 | + |
| 111 | +## Styling |
| 112 | + |
| 113 | +All colours and geometry are plain `let` bindings exposed by the package and can |
| 114 | +be overridden after import — e.g. the player palette `game-pal`, the Nash |
| 115 | +outline `game-nash-color`, the highlight colour `game-highlight-color`, and tree |
| 116 | +geometry such as `game-node-radius`, `game-terminal-radius`, `game-gap`, … |
| 117 | + |
| 118 | +```typst |
| 119 | +#import "@preview/pi-games:0.1.0": * |
| 120 | +#let game-pal = ( // custom player colours |
| 121 | + rgb("#005f73"), rgb("#94d2bd"), rgb("#e9d8a6"), |
| 122 | + rgb("#ee9b00"), rgb("#ae2012"), |
| 123 | +) |
| 124 | +``` |
| 125 | + |
| 126 | +## Dependencies |
| 127 | + |
| 128 | +- [`@preview/cetz`](https://typst.app/universe/package/cetz) `0.5.2` (requires Typst ≥ 0.14.0) |
| 129 | + |
| 130 | +## Acknowledgements |
| 131 | + |
| 132 | +- The normal-form games cover the same ground as the |
| 133 | + [`game-theoryst`](https://typst.app/universe/package/game-theoryst/) Typst |
| 134 | + package by Connor T. Wiegand, reimplemented here with a different, |
| 135 | + CeTZ-based API. |
| 136 | +- The extensive-form trees replicate the visual style of the |
| 137 | + [`xgames`](https://carlabernard.ch/beni/downloads/xgames.pdf) LaTeX package by |
| 138 | + Benjamin Bernard. The original colour-coded design is his; `pi-games` only |
| 139 | + reimplements that look in Typst/CeTZ. Full credit for the design goes to him. |
| 140 | + |
| 141 | +## License |
| 142 | + |
| 143 | +MIT © Piotr Kuszewski — see [`LICENSE`](LICENSE). |
0 commit comments