# 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
```

sortes runs on macOS (Apple silicon) and Linux (x86, ARM).
On Windows, use WSL.

## Quickstart

```bash
# Create a course
sortes init exams
cd exams

# Create a term
sortes term <term>

# Write an exam, e.g. <term>/<exam>.typ, see below
sortes build <term>/<exam>.typ
```

The command `init` creates the course: `bank.typ` with a few example questions, and `typst.toml`.

## Typst package

The `init` command also installs the sortes Typst package as `@local/sortes`.
Import it in your Typst files.
See `bank.typ` for an example.

## 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 labeled 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>)[...]` and has one of three answer types, as on Gradescope:

- **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:** the `#blank[...]` command holds the model answer. Students write on a single line, labeled with what to write in `expect`.
- **A box:** `#answer[...]` holds the model answer. The answer box's height can be specified.

The command `#why[...]` is an explanation that only shows in the solution PDFs.

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

:::caution
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.
:::

If a question that an exam used comes back under a new ID with exactly the same text, the build stops.

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.

See the [reference](/reference/#q) for options.

## Make an exam

An exam is a Typst file in a term directory.
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>)
```

Explanation:

- `#draw(<regression>, 3)`: randomly draw 3 questions from the section `<regression>`.
- `min: (compute: 1)`: randomly draw, with at least one of them of `kind: "compute"`.
- `#pick`: puts specific questions in the exam, i.e., no randomness. Note that for MCQs, their choices are still shuffled.
- `points: 20`: exam's total points.
  - Questions' `points` are preserved if specified in the bank. The rest evenly share the points left.
- `date:` is the day of the exam.
  - It is important for after the exam, see [below](#after-the-exam).
- `version()`, `solution()` and `summary()` are useful functions to use in your own template.
  - 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

// Your own template
#show: doc => exam(doc, title: [Midterm], course: [ML 101])

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

## Build

Suppose you have term F2026 and inside you have created a midterm.
Then you build it via:

```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 won't change the drawn question IDs, but you can change/adjust the questions drawn in the bank.
So you can fix the bank and the template and rebuild as often as you like.

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

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

## Terms

Each term gets its own directory in the course:

```bash
sortes term W2027
```

Within a term, exams never draw the same question.

Questions from past terms can appear in the current term.
The option `repeats:` caps the expected number of repeats, 10% by default.
Only the last 2 terms count, so questions from older terms are fresh again; `memory:` changes how many.
The build avoids the newest past exam's questions first, then the next newest, only as far as it needs to stay under the cap.
So the questions you used most recently are the last to come back, and once the bank is used up, the oldest come back first.
The reference explains [how repeats are counted](/reference/#repeats-from-past-terms).

Once the next term starts, the earlier terms are frozen.
Nothing in them changes, not even the answer key of a given exam.

## After the exam

After the exam's `date`, the exam is given.
A given exam can't be drawn again.
