I’m trying to set up a template in typst for creating lecture exercises.
At the end of the day, I’m aiming for following user experience:
#import "../template.typ": *
#exercise(
[Exercise Title]
)[
Here are the tasks:
+ Number of possible values that one bit can hold. #points(1)
#solution[ 2 ]
+ Number of possible values that two bits can hold. #points(1)
#solution[ ... ]
]
I wrote following template file to calculate the number of points for each task:
Summary
#let show-solutions = state("show-solutions", false)
#let exercise-counter = counter("exercise")
#let total-points = state("total-points", 0)
#let current-exercise-points = state("current-exercise-points", 0)
#let exercise(title, body) = context {
let strings = strings-de
set enum(numbering: "a)")
// Increment exercise counter
exercise-counter.step()
let ex-num = exercise-counter.get().first()
// Reset and calculate points by processing body
current-exercise-points.update(0)
// Process body to count points
let _ = body
// Get the points that were accumulated
let ex-points = current-exercise-points.get()
// Add to total points
total-points.update(t => t + ex-points)
v(0.7cm)
let ex-display-num = ex-num + 1
// Exercise header
[
#text(size: 14pt, weight: "bold")[
#ex-display-num. #strings.task: #title (#ex-points #if ex-points == 1 { strings.point } else { strings.points })
]
]
v(0.3cm)
body
}
#let points(n) = {
let strings = strings-de
// Add to current exercise points
current-exercise-points.update(p => p + n)
[(#n #if n == 1 { strings.point } else { strings.points })#h(0.2em)]
}
#let solution(body) = context {
let strings = strings-de
let show-sol = show-solutions.get()
if show-sol {
v(0.3cm)
[*#strings.solution:*]
linebreak()
body
}
}
#let point-summary() = context {
let strings = strings-de
let total = total-points.final()
v(0.5cm)
text(weight: "bold")[
Total *#total #if total == 1 { strings.point } else { strings.points }*.
]
v(0.5cm)
}
However, I do not really see any way how I could “refer forward” to calculate the number of points in the task and write it into the headline of the task. Now the output is off-setted by one task.
Any help or suggestion would be highly appreciated! Thank you!



