# Guide

`sortes` makes exams from a question bank.
You write the bank and the exams in Typst and preview them in your editor.
`sortes build` picks each exam's questions, shuffles them into versions whose answer letters are spread evenly, and renders the print and solution PDFs.
It remembers what it drew, so rebuilding an exam gives the same exam, and later exams avoid questions you already used.

## Install

Install [Typst](https://github.com/typst/typst#installation) and [`uv`](https://docs.astral.sh/uv/#installation).
Then:

```bash
uv tool install sortes
```

`cargo install sortes` works too.
sortes runs on macOS (Apple silicon) and Linux (amd64, arm64).
On Windows, use WSL.

## Quickstart

```bash
sortes init exams
cd exams
sortes term F2026
sortes build F2026/midterm.typ
```

`init` creates the course: `bank.typ` with a few example questions, and `typst.toml`.
`term` creates a folder for a term with an example exam, `midterm.typ`.
The build prints:

```text
  Midterm · 9 questions · 2 versions · 20 points
  ✔ Drew <regression>, <classification> (seed 485337375)
    F2026/sortes.lock
    F2026/PDFs/midterm-A.pdf, midterm-B.pdf
    F2026/PDFs/midterm-A-sol.pdf, midterm-B-sol.pdf
```

The course now looks like this:

```text
exams/
├── bank.typ
├── typst.toml
└── F2026/
    ├── midterm.typ
    ├── sortes.lock
    └── PDFs/
```

## Preview

`init` also installs the sortes Typst package as `@local/sortes`, so your editor can compile the bank and the exams.
Use the course folder (the one with `typst.toml`) as the project root, because exams include `../bank.typ`.
With [tinymist](https://github.com/Myriad-Dreamin/tinymist), open the course folder.
With the Typst CLI, pass `--root`:

```bash
typst watch --root . F2026/midterm.typ
```

The preview of the bank shows every question with its ID and the right answers marked.
The preview of an exam shows version A with solutions.
Draws that haven't been built yet show as a placeholder.

## Write the bank

The bank is one Typst file, `bank.typ`:

```typst
#import "@local/sortes:0.1.0": *

= Supervised Learning and Regression <regression>

#q(<supervised-def>)[
  What is supervised learning?
  - Minimizing a function given data
  - #correct[Approximating the function $bold(x) |-> y$ from labelled data]
  - Learning without labels
]

#q(<outlier-fix>)[
  A handful of extreme outliers dominate a least-squares fit. A principled fix is to:
  - Remove the bias term from the model
  - Increase the learning rate
  - #correct[Replace the Gaussian likelihood with a Laplace likelihood]
  #why[Absolute errors grow linearly, so outliers pull less on the fit.]
]

#q(<normal-eq-shape>)[
  $bold(X)$ has $N$ rows and $D$ columns. What is the shape of $bold(X)^top bold(X)$?
  #blank(expect: [a *shape*])[$D times D$]
]

#q(<normal-eq-derive>, points: 4)[
  Derive the normal equation for linear regression.
  #answer(height: 5cm)[Set the gradient of the squared error to zero and solve for $bold(theta)$.]
]
```

A question is `#q(<id>)[...]`: the stem, then how students answer it, in one of three forms.

- **Choices:** a `-` list of 2 to 5 choices, with the right one wrapped in `#correct[...]`. Several `#correct` choices make a multi-select question.
- **One line:** `#blank[...]` holds the model answer. Students write on a single line, labelled with what to write when you give `expect:`.
- **A box:** `#answer[...]` holds the model answer. Students write in a box `height:` tall, 4cm unless you say otherwise.

`#why[...]` is an explanation that only the solutions show, e.g. the answers you also accept.

A heading with a label, like `= Regression <regression>`, starts a section.
Exams draw questions from sections.

The ID is how exams and past terms know a question, so it must never change.
Pick names that stay true, like `<kl-nonneg>` or `<q-0412>`.
Don't number them by lecture, like `<lec05-kl>`: you'll want to move questions between lectures.
Fixing a question's text keeps its ID.
A new ID is a new question.
If a question that an exam used comes back under a new ID with exactly the same text, the build stops:

```text
  ✘ <robust-loss> is <outlier-fix> under a new ID. IDs are permanent, because exams and past terms know questions by them.
    Change it back to <outlier-fix>.
```

If your course has a file of math macros, import it at the top of `bank.typ` as usual:

```typst
#import "@local/sortes:0.1.0": *
#import "math_commands.typ": *
```

The questions keep the bank's macros in every exam.
Import the file in an exam, as `"../math_commands.typ"`, only if the exam's own text uses them.

The [reference](/reference/#q) lists the options of `q`: `kind`, `points`, `cols` and `shuffle`.

## Make an exam

An exam is a Typst file in a term folder.
It uses your own template.
sortes only prints the questions; the page, the header and the cover are yours.

```typst
#import "@local/sortes:0.1.0": *

#show: sortes.with(
  bank: include "../bank.typ",
  lock: json("sortes.lock"),
  title: "Midterm",
  date: "2026-11-20",
  versions: 2,
  points: 20,
)

#align(center)[
  #text(size: 1.6em, weight: "bold")[Midterm] \
  Version #version() #if solution() [· Solutions]
]

= Supervised Learning and Regression
#pick(<supervised-def>)
#draw(<regression>, 3)

= Maximum Likelihood and Classification
#draw(<classification>, 4, min: (compute: 1))

= Written
#pick(<normal-eq-derive>)
```

`#draw(<regression>, 3)` takes 3 questions from the section `<regression>`.
`min: (compute: 1)` asks for at least one of them to have `kind: "compute"`.
`#pick` puts specific questions in the exam.

`points: 20` is the exam's total.
Questions with their own `points:` keep them, and the others share the rest.
`date:` is the day of the exam.
It matters after the exam, see [below](#after-the-exam).

`version()`, `solution()` and `summary()` let your template print the version, mark the solution copy, and show the number of questions and points on a cover.
See the [reference](/reference/#version).

To use a template you already have, apply it first, then sortes:

```typst
#import "@local/sortes:0.1.0": *
#import "../template.typ": exam

#show: doc => exam(doc, title: [Midterm], course: [ML 101])
#show: sortes.with(
  bank: include "../bank.typ",
  lock: json("sortes.lock"),
  title: "Midterm",
  versions: 2,
  points: 20,
)
```

The template should print the version and mark the solution copy with `version()` and `solution()`, instead of its own inputs:

```typst
#import "@local/sortes:0.1.0": *

#let exam(doc, title: none, course: none) = {
  set page(header: [#course #h(1fr) #title, Version #version() #if solution() [· Solutions]])
  doc
}
```

A cover that counts the questions or points must use `summary()`, inside `context`.
sortes renders the questions, so the template's own counters see none.

The quickstart built the example `midterm.typ`, so its draw is in `sortes.lock`.
When you replace it with your own exam, build it once with `--redraw`:

```bash
sortes build F2026/midterm.typ --redraw
```

Otherwise the build treats your exam as an edit of the example.
It reuses the example's seed and reports changes to an answer key and points that nobody has seen.

## Build

```bash
sortes build F2026/midterm.typ
```

The first build draws the questions and writes them to `F2026/sortes.lock`.
For every version, it shuffles the order of the drawn questions and of their choices.
Across a version, the right answers are spread evenly over the letters, with no letter more than 3 times in a row.

Building again keeps everything:

```text
  Midterm · 9 questions · 2 versions · 20 points
  ✔ Kept the draw in sortes.lock
    F2026/sortes.lock
    F2026/PDFs/midterm-A.pdf, midterm-B.pdf
    F2026/PDFs/midterm-A-sol.pdf, midterm-B-sol.pdf
```

So you can fix the bank and the template and rebuild as often as you like.
A fixed question stays where it was, with its choices in the printed order.
When a build changes the answer key or the points, it says which question numbers changed in which version:

```text
  ! Edited since the last build, kept the printed choice order: <normal-eq-purpose>
  ! Answer key changed in version A: 2 A→C
  ! Answer key changed in version B: 3 A→D
```

If you delete a drawn question from the bank, the next build draws a replacement from the same section and puts it in the same place.
If you change `#draw(<regression>, 3)` to 4, it keeps the 3 and draws one more.
`sortes build --redraw` throws the draw away and draws again.

The [reference](/reference/#what-the-next-build-does) lists what each kind of edit does.

For Gradescope, enter the answer key from the `-sol` PDFs.
The filled bubbles are the key, one PDF per version.

Commit `sortes.lock` with the exams.
It is the record of what each exam contains.

## Terms

Each term gets its own folder in the course:

```bash
sortes term W2027
```

The first term gets an example exam.
Later terms start with an empty `sortes.lock`; copy an exam from an earlier term and change its `date:`.

Within a term, exams never draw the same question.
If the bank runs out, the build says so:

```text
  ✘ <regression> has 1 unused question (5 are in midterm); #draw(<regression>, 3) asks for 3.
    Write 2 more, or draw fewer.
```

Questions from past terms are avoided as far as `repeats:` allows.
By default, about 10% of an exam may repeat from past terms.
Repeats that can't be avoided come from the oldest terms first.
When the bank is too small for that, the build tells you:

```text
  ! About 1.0 of 9 questions are expected to repeat from past terms, more than the 10% you allow; the bank is small for this exam. (avoided where possible: F2026/midterm)
```

Once the next term starts, the earlier terms are frozen.
Nothing in them changes, not even the answer key of a given exam.
So finish regrades, see [below](#after-the-exam), before you start the next term.
Building an exam of a past term again only prints:

```text
  ✔ quiz is from a past term (W2027 is newer), so it stays as recorded. Its PDFs are in F2026/PDFs.
```

A past term can't get new exams either:

```text
  ✘ F2026 is a past term (W2027 is newer), so it can't get new exams.
    Make new exams in W2027.
```

## After the exam

After the exam's `date:`, the exam is given.
Students have the printed copies, so these never change again.
You can still fix the bank: a wrong key, a typo, an ambiguous choice.
The build then updates the key for the paper the students hold and re-renders only the solution PDFs:

```text
  ✔ midterm was given on 2026-11-20; its questions stay as printed
  ! Answer key changed in version A: 5 C→B
  ! Answer key changed in version B: 8 B→A
    F2026/PDFs/midterm-A-sol.pdf, midterm-B-sol.pdf
```

Use the new solutions to regrade.
Do this before the next term starts, because after that the term is frozen.

If the exam is postponed, change its `date:` to the new day.
The next build keeps the draw and treats the exam as upcoming again:

```text
  ! midterm's date moved from 2026-11-20 to 2026-11-27; it's not given yet
```

A given exam can't be drawn again.
To reuse it, copy its file to a new name.
