enable customizing exercise numbering

This commit is contained in:
Gaspard Jankowiak 2025-11-17 20:56:12 +01:00
commit c81d8f65b2
2 changed files with 40 additions and 4 deletions

View file

@ -18,7 +18,7 @@ A summary is then printed on the front page.
## Usage
Writing an exam is now easy:
Writing an exam is now easy (see [API](#api) for all options):
#import "@local/exam:0.1.0": exam, exercise, exercise-items, mtext
@ -99,3 +99,34 @@ These are used in the example above.
- `mtext(str)` to typeset text within math mode using the default text font.
- `frame(stroke_width)` provides `stroke` for use in a `table`, horizontal lines only,
top and bottom lines are bold.
### API
```typ
exam(
title: "%KLAUSUR or EXAM%",
course-title: [%COURSE_TITLE_FIRST_LINE% \ %COURSE_TITLE_SECOND_LINE%],
course-short-title: "%course-short-title%",
course-code: "%course-code%",
date: "%DATE%",
institution: smallcaps("NAWI Graz"),
duration-minutes: "%DURATION%",
ask-student-number: false,
ask-trainer-name: false,
ask-group: true,
instructions: none,
language: "de", // or "en"
font-size: 12pt,
paper-size: "a4",
exercise-numbering: "1",
)
```

11
lib.typ
View file

@ -103,25 +103,26 @@
// Used to compute the overview table (see below)
// title: string, title of the exercise
// points: int, the number of points the exercise is worth
#let exercise(title, points, numbering: "1") = {
#let exercise(title, points) = {
// state("exercise_points").update(points)
let exercise_counter = counter("exercise")
exercise_counter.step()
context {
let current_exercise = exercise_counter.get().at(0)
let exercise-numbering = state("exercise-numbering").get()
let exercise_registry = state("exercise-registry")
let final_points = 0
if (exercise_registry.final().len() >= current_exercise) {
final_points = exercise_registry.final().at(exercise_counter.get().at(0)-1).at(2)
}
let exercise_label = exercise_counter.display(numbering)
let exercise_label = exercise_counter.display(exercise-numbering)
exercise_registry.update(entries => entries + ((exercise_label, title, points),))
block(radius: 1cm, stroke: none, fill: lightgray, inset: 1pt,
table(
columns: (auto, 1fr, auto),
align: (left, center, right),
stroke: none,
[#rect(radius:1cm, outset: 4pt, inset: (x:2pt, y:0pt), fill: white)[*#get_L("exercise") #context{exercise_counter.display(numbering)}*]], [#title], [#rect(radius:1cm, outset: 4pt, inset: (x:2pt, y:0pt), fill: white)[*#format_points(final_points)*]],
[#rect(radius:1cm, outset: 4pt, inset: (x:2pt, y:0pt), fill: white)[*#get_L("exercise") #context{exercise_counter.display(exercise-numbering)}*]], [#title], [#rect(radius:1cm, outset: 4pt, inset: (x:2pt, y:0pt), fill: white)[*#format_points(final_points)*]],
)
)}}
@ -148,6 +149,8 @@
font-size: 12pt,
paper-size: "a4",
exercise-numbering: "1",
body
) = {
@ -159,6 +162,8 @@
set page(paper: paper-size)
set text(size: font-size, lang: language, hyphenate: true)
state("exercise-numbering").update(exercise-numbering)
/*
* Localization
*/