Skip to content

References

sortes init <course>

Creates the course directory <course> with two files:

  • bank.typ: a bank with a few example questions.
  • typst.toml: marks the course directory as the Typst project root.

Files that already exist are left alone.

It also installs the sortes Typst package as @local/sortes:0.1.0 into Typst’s package directory, or into $TYPST_PACKAGE_PATH if you set it. Every sortes command checks the installed package and updates it to match the binary.

sortes term <term>

Creates the term directory <term> with an empty sortes.lock. Nothing else: the exams are yours to write.

The term must sit next to the course’s bank.typ. So run it inside the course directory, or pass <course>/<term>.

sortes build <exam.typ> [--redraw] [--seed N]

The first build draws the exam’s questions and writes them to the term’s sortes.lock. Every build then renders the PDFs into <term>/PDFs/:

Versions Print copies Solutions
1 midterm.pdf midterm-sol.pdf
2 or more midterm-A.pdf, midterm-B.pdf, … midterm-A-sol.pdf, midterm-B-sol.pdf, …
Flag Description
--redraw Throw away the exam’s draw and draw again. Not allowed once the exam is given, or once its term is frozen.
--seed N The seed for a new draw. Without it, sortes uses the exam’s seed: or a random one. Once the exam is drawn, it needs --redraw.

The seed is saved in the lock, so you can always see which one made a draw.

Typst runs with the course directory as --root, i.e., the nearest directory above the exam with a typst.toml (or a .git). sortes needs typst on your PATH. Set SORTES_TYPST to use another binary. Output is colored in a terminal, unless you set NO_COLOR.

sortes completions <shell>

Prints the completion script for bash, zsh or fish. Load it the way your shell expects, e.g. for bash, add source <(sortes completions bash) to ~/.bashrc. It isn’t listed in sortes --help.

// Choices
#q(<id>, kind: none, points: none, cols: 1, shuffle: true)[
Stem.
- Choice
- #correct[Right choice]
#why[Explanation.]
]
// One line
#q(<id>)[
Stem.
#blank(expect: [a *number*])[Model answer]
]
// A box
#q(<id>)[
Stem.
#answer(height: 4cm)[Model answer]
]

The first argument is the question’s ID, written as a label. IDs are unique in the bank.

A question has one of three answer types:

  • Choices: the - list that ends the question, 2 to 5 choices.
    • One #correct choice makes a single-answer question, with round bubbles.
    • Several #correct choices make a multi-select question, with square boxes.
    • A list followed by more text is part of the stem, not the choices.
  • One line: a #blank.
  • A box: an #answer. A question with no choices and no #blank gets a box too.

A question with two types, e.g., choices and a #blank, stops the build.

Option Default Example Description
kind none kind: "compute" Any name you like. #draw(..., min: (compute: 1)) asks for at least that many of a kind.
points none points: 4 The question’s points. Without it, the question shares what’s left of the exam’s points:.
cols 1 cols: 2 Columns for the choices.
shuffle true shuffle: false Shuffle the choices in each version. Use false for choices like “All of the above”.

The answer letters of each version are balanced over single-answer questions whose choices are shuffled. Other questions don’t count.

#correct[Right choice]
#why[Explanation]
#blank(expect: none)[Model answer]
#answer(height: 4cm)[Model answer]
  • #correct[...]: wraps a right choice, inside a list item.
  • #why[...]: an explanation, shown under the question only in the solution PDFs.
  • #blank[...]: makes a one-line question. The print copy has a line labeled “Answer (on ONE line):”. The solutions show the model answer on it.
  • #answer[...]: makes a question answered in a box. The solutions show the model answer in the box, which grows to fit it.

why, blank and answer go on their own line in the question, outside the list.

Command Option Default Example Description
blank expect none expect: [a *number*] What to write, shown in the label: “Answer (a number on ONE line):”.
answer height 4cm height: 6cm Height of the box in the print copy.

A heading with a label starts a section, e.g., = Regression <regression>. A question belongs to the last heading above it. Questions under a heading without a label can only be picked, not drawn.

#show: sortes.with(
bank: include "../bank.typ",
lock: json("sortes.lock"),
title: "Midterm",
date: "2027-02-26",
versions: 4,
points: 100,
)
Parameter Default Example Description
bank required include "../bank.typ" The bank, passed with include.
lock none json("sortes.lock") The term’s lock, passed with json. Without it, drawn questions show as placeholders.
title none "Midterm" The exam’s name. Not printed; summary() returns it. Give exams with the same draws different titles, so previews can tell them apart.
date none "2027-02-26" The day of the exam, as "YYYY-MM-DD" or a datetime. After this day, the exam is given.
versions 1 4 Number of versions, 1 to 26, named A, B, C, …
points none 100 The exam’s total points. Questions without their own points: share what’s left, in half-point steps.
repeats 10% 20% How much of the exam may be expected to repeat from past terms. See repeats from past terms.
memory 2 none How many of the most recent past terms count for repeats. none counts every past term.
choices "lettered" "bubbled" "lettered" prints A to E in the bubbles, for bubble sheets. "bubbled" prints empty bubbles.
seed none 42 The seed for a new draw, if --seed isn’t given.
max-run 3 2 The most times the same answer letter may appear in a row in a version.

When the points don’t split evenly, the build says how they were split.

#draw(<regression>, 5)
#draw(<regression>, 5, min: (compute: 2, proof: 1))
  • Randomly draws 5 questions from the section <regression>, none of which another exam of this term has.
  • min: at least that many questions of each kind.
  • Each version shuffles the order of the drawn questions.

If the section doesn’t have enough unused questions, the build stops and says how many are missing.

#pick(<supervised-def>, <normal-eq-derive>)

Puts these specific questions in the exam, i.e., no randomness. They keep the order you wrote in every version, and their choices are still shuffled. If another exam of this term has a picked question too, the build tells you.

version() is the version being rendered: "A", "B", …. In a preview, it’s "A".

solution() is true in the solution PDFs. In a preview, it’s true too. It’s false with typst compile --input mode=print.

summary() describes the exam, for covers and section headers. Call it inside context:

#context {
let s = summary()
[#s.questions questions, #s.points points]
}

It returns:

(
title: "Midterm",
date: "2027-02-26",
versions: 4,
questions: 40,
points: 100,
sections: (
(heading: [Regression], label: <regression>, questions: 8, points: 20),
..
),
)
  • sections: one entry per heading of the exam file that has questions under it.
  • label: the heading’s label, or none.
  • points: none until the first build, and when no question has points.

Your editor can preview the bank and the exams, with the course directory as the project root.

  • The bank shows every question with its ID and the right answers marked.
  • An exam shows version A with solutions.
  • A draw that hasn’t been built yet shows a placeholder.
exams/ the course
├── bank.typ the question bank
├── typst.toml marks the Typst project root
├── F2026/ a term
│ ├── midterm.typ
│ ├── final.typ
│ ├── sortes.lock
│ └── PDFs/
└── W2027/ a later term
├── midterm.typ
├── sortes.lock
└── PDFs/

A term is any directory next to bank.typ with a sortes.lock. A term starts on the earliest date of its exams. For an exam without date:, the day it was first drawn counts. Terms are ordered by when they start, not by their names. The terms that start before a term are its past terms.

The lock is JSON, with one entry per exam, named after the exam’s file. An entry holds:

  • the exam’s date, title and seed,
  • its #draw and #pick calls and the questions each one got,
  • every question’s points,
  • each version’s question order and choice orders,
  • the answer key,
  • a fingerprint of each drawn question’s text, to follow your edits.

Typst reads the lock to render the exam. sortes build reads the locks of all terms to know which questions are used.

Within a term, exams never share a question. Questions from past terms can come back, but repeats: caps how many. The expected number of repeats must stay under repeats times the exam’s number of questions. For 40 questions and the default 10%, that’s 4.

The number is expected because the draw is random. If 5 of the 50 questions a #draw can take were used in past terms, a draw of 10 is expected to repeat 10 × 5 / 50 = 1 of them. Only drawn questions count, picked ones don’t.

Only the last memory: terms count, 2 by default. Questions from older terms are fresh again, so they’re neither avoided nor counted as repeats. With memory: 2, a bank about three times the size of what one term draws never has to repeat. With memory: none, every past term counts, so once a fixed bank is used up, every exam repeats and the build warns every term.

How the build draws:

  1. Draw from every question not used this term.
  2. If the expected repeats are over the cap, avoid the newest past exam’s questions and draw again.
  3. Then avoid the next newest too, and so on, until the expected repeats fit.
  4. If avoiding every remembered past exam still leaves too few questions, take the rest from the avoided ones, oldest term first, and warn.

Suppose a section has 100 questions, last term’s exam used 5 of them, and this exam draws 10. The expected repeats are 10 × 5 / 100 = 0.5, under the cap of 1, so nothing is avoided. Each of last term’s 5 questions may come up, but rarely:

✔ 0.5 expected repeats from the last 2 terms

Now suppose a section has 30 questions, and each term’s exam draws 10:

Term Draws Why
F2025 10 new There’s no past term.
F2026 10 new F2025’s questions are avoided.
W2027 The last 10 new F2026’s and F2025’s questions are avoided.
F2027 F2025’s 10 again F2025 is 3 terms back, so its questions are fresh again.

With memory: none, F2027 gets the same questions, but with a warning, and so does every term after it.

Before the exam’s date, you can keep editing the bank and the exam:

Edit Next build
Fix a stem Renders the fix.
Fix a choice’s text Renders the fix and keeps the printed choice order. A choice rewritten in entirely new words counts as removed and added.
Move #correct, add or remove a choice Keeps the printed order as far as it can. A removed choice closes its gap, and a new one goes last. Reports the new key.
Change a question’s points: Splits the exam’s points again and reports the changed points.
Delete a drawn question Draws a replacement from the same section, in the same place, with the same answer letter where it fits. The build says which question replaced which.
Delete a picked question Stops: there’s no such question in the bank.
Change a question’s ID A new ID is a new question. If the old one was used and the text is the same, the build stops.
Add questions to the bank Nothing: existing draws stay.
Change n or min of a #draw Keeps the questions that still fit and draws the difference.
Rename a section’s label Keeps the draw, if all its questions are in the renamed section.
Add a #draw Draws it, and the other draws stay.
Change versions Keeps the existing versions and adds or drops versions at the end.
Rename the exam file A new exam with a new draw. The old entry stays in the lock, and its questions stay used.
--redraw Draws everything again.

Reports go by question number, per version. When a new question moves the others down, the later numbers show as changed too.

After the exam’s date, the exam is given, and it’s frozen:

Edit Next build
Anything in the bank or exam file Nothing changes and nothing is rendered. The build tells you where the PDFs are.
Change date: to today or later The exam is upcoming again. It keeps its draw, and the build renders the PDFs.
--redraw Stops: a given exam can’t be drawn again. Copy the file to a new name instead.

The exam file still has to compile, since sortes reads its date: from it.

A term is frozen once the date of an exam in a later term has passed. Until then, the term stays open, even if you have already built next term’s exams. In a frozen term:

  • Building an exam renders nothing and changes nothing in the lock, even after edits to the bank or to the exam file, including its date:.
  • --redraw stops.
  • Building a new exam stops.