refactor, add template, add license

This commit is contained in:
Gaspard Jankowiak 2025-12-15 09:10:29 +01:00
commit 482bc0a088
5 changed files with 116 additions and 4 deletions

21
LICENSE Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2025 Gaspard Jankowiak
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

340
src/lib.typ Normal file
View file

@ -0,0 +1,340 @@
/*
* Customisation
*/
#let lightgray = rgb("#eeeeee")
// Default font (embedded in Typst)
#let font-text = "libertinus serif"
#let mtext = text.with(font: font-text)
/*
* Macros
*/
// stroke helper function for table, no vertical lines, top and bottom are bold
// s: stroke, to specify e.g. the width and color of strokes
#let horiz-only(s) = (x, y) => (
left: none,
right: none,
top: if y > 0 { s } else { 2*s },
bottom: 2*s,
)
#let capitalize(string) = {
return string.replace(
regex("[A-Za-z]+('[A-Za-z]+)?"),
word => upper(word.text.first()) + lower(word.text.slice(1)),
)
}
#let get_L(k, cap:false) = context {
let translations = state("translations").get()
let term = translations.at(k, default:k)
if cap {
return capitalize(term)
} else {
return term
}
}
// Formats number of points, "1 Punkt", "2 Punkte", etc.
// p: int
#let format_points = (p) => (
context {
if p <= 1 {
[#p #get_L("point")]
} else {
[#p #get_L("points")]
}
}
)
// Displays a list of sub-questions and the associated points.
// Updates the exercise's points automatically.
// items: array of (int, expression), where
// - int is the number of points this item is worth,
// - expression is the item to typeset.
// example:
// #exercise-items((
// (1, [Untersuchen Sie, ob $f$ injektiv ist.]),
// (1, [Untersuchen Sie, ob $f$ surjektiv ist.]),
// (1, [Geben Sie im Falle ihrer Existenz die Umkehrfunktion $f^(−1)$ an.])
//))
#let exercise-items(override-points: true, numbering: "a)", items) = block[
#let c = counter("exercise-items")
#let exercise_points = 0
#c.update(0)
#table(
columns: (1fr, auto),
stroke: none,
..for (points, statement) in items {
exercise_points += points
let pt
if points <= 1 {
pt = get_L("point_short")
} else {
pt = get_L("points_short")
}
(
[#c.step()#v(4pt) *#context c.display(numbering)* #statement],
rect(radius:1cm, inset: (x:5pt, y:3pt), stroke: gray+0.5pt)[#points #pt],
)
}
)
#if override-points {
context {
state("exercise-registry").update( arr => {
if (arr.len() == 0) {
arr
} else {
let new_arr = arr.slice(0, arr.len()-1)
new_arr.push((arr.last().at(0), arr.last().at(1), exercise_points))
new_arr
}
})
}
}
]
// Displays a header for the exercise, ex. number is incremented automatically
// 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) = {
// 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(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(exercise-numbering)}*]], [#title], [#rect(radius:1cm, outset: 4pt, inset: (x:2pt, y:0pt), fill: white)[*#format_points(final_points)*]],
)
)}}
#let 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",
body
) = {
/*
* Setup
*/
// Paper and text size
set page(paper: paper-size)
set text(size: font-size, lang: language, hyphenate: true)
state("exercise-numbering").update(exercise-numbering)
/*
* Localization
*/
let translations = none
if language == "en" {
translations = (
course-code: "Course code",
exam_duration: "Exam duration",
student_name: "Name",
student_number: "Student number",
trainer_name: "Trainer",
group: "Group",
exercise: "Exercise",
exercise_short: "Ex.",
point: "point",
points: "points",
point_short: "pt",
points_short: "pts",
points_obtained: "Points",
total_points: "Total",
minutes: "minutes",
)
} else if language == "de" {
translations = (
course-code: "LV-Nummer",
exam_duration: "Arbeitszeit",
student_name: "Name",
student_number: "Matrikelnr.",
trainer_name: "Übungsleiter",
group: "Übungsgruppe",
exercise: "Beispiel",
exercise_short: "Bsp.",
point: "Punkt",
points: "Punkte",
point_short: "P.",
points_short: "P.",
points_obtained: "Erreichte Punkte",
total_points: "Gesamt",
minutes: "Minuten",
)
}
state("translations").update(translations)
// State and counter for exercises
state("exercise-registry").update(())
// Displays an overview of the exam as a table, with
// a row per exercise, the corresponding points and space to write the mark.
let exercise_overview_table() = context {
let exercise_registry = state("exercise-registry")
let entries = exercise_registry.final()
let total_points = entries.fold(0, (sum, entry) => sum + entry.at(2))
let columns = entries.map((i) => 1fr)
columns.push(1fr)
[== #get_L("points_obtained")]
set text(size: 14pt)
block(radius: 0.3cm, stroke: none, fill: lightgray, inset: (x:0pt, y:4pt),
table(
columns: columns,
stroke: none,
fill: none,
gutter: 0pt,
inset: (x, y) => {
let top = 8pt
if y == 0 { top = 2pt }
if x == 0 {
(left:4pt, right:2pt, top: top, bottom: 0pt)
} else if x == entries.len() {
(left:2pt, right:4pt, top: top, bottom: 0pt)
} else {
(left:2pt, right:2pt, top: top, bottom: 0pt)
}
},
align: (x, y) => { if (y == 1) { right+horizon } else { center+horizon }},
..for entry in entries {
(
[#get_L("exercise_short", cap: true) #entry.at(0)],
)
},
[*#get_L("total_points")*],
..for entry in entries {
(
rect(radius:0.2cm, fill: white, width: 100%, inset: 8pt, outset: 0pt, stroke:none)[/#entry.at(2)],
)
},
rect(radius:0.2cm, fill: white, width: 100%, inset: 8pt, outset: 0pt, stroke:black)[#strong([/#total_points])],
)
)
}
// Header and footer
set page(
header-ascent: 20%,
header: [
#set text(size: 8pt)
#table(
stroke: none,
gutter: 4pt,
inset: 0pt,
align: (x, y) => { if x < 2 { left+bottom } else { right }},
columns: (auto, 1fr, auto),
[#institution], [], [#course-short-title \ _#(date)_],
)
],
footer: [#align(right)[#context(counter(page).display("1 / 1", both:true))]],
)
// Title page
v(1fr)
align(center)[= #title]
v(0.5cm)
align(center)[== #course-title]
v(1fr)
[*#get_L("course-code")*: #course-code \ *#get_L("exam_duration"): #duration-minutes #get_L("minutes")*]
v(0.5cm)
block(radius: 0.2cm, fill: lightgray, inset: 3pt, clip: true, table(
fill: lightgray,
stroke: none,
columns: (auto, 1fr),
gutter: 3pt,
inset: (left:7pt, right:0pt, y:0pt),
align: horizon,
[#get_L("student_name")], rect(fill: white, width: 100%, radius: 0.2cm),
.. if ask-student-number {
([#get_L("student_number")], rect(fill: white, width: 100%, radius: 0.2cm))
},
..if ask-trainer-name {
([#get_L("trainer_name")], rect(fill: white, width: 100%, radius: 0.2cm))
},
..if ask-group {
([#get_L("group")], rect(fill: white, width: 100%, radius: 0.2cm))
}
))
v(0.5cm)
if (instructions != none) {
[ #instructions ]
} else {
if language == "de" [
- _Lesen Sie die ganze Prüfungsaufgabe durch_.
- Es sind keine Unterlagen und auch keine Taschenrechner erlaubt.
- Alle Rechenschritte (inklusive Zwischenresultate und Lösungswege) sind anzugeben und alle Antworten genau zu begründen bzw. zu beweisen!
- Schreiben Sie auf jedes _lose_ Blatt Ihren Namen und Ihre Matrikelnummer!
- Nicht mit roter Farbe schreiben.
- Viel Erfolg!
]
if language == "en" [
- _Before starting, read the whole exam._
- Course material and calculators are not allowed.
- Write down all the steps, including intermediate results. All answers must be substantiated!
- Do not write in red.
- Good luck!
]
}
v(1fr)
exercise_overview_table()
v(1fr)
pagebreak()
body
}

85
template/exam.typ Normal file
View file

@ -0,0 +1,85 @@
#import "@local/exam:0.2.0": exam, exercise, exercise-items, mtext, horiz-only
// General configuration
#show: exam.with(
title: "Exam",
course-title: [Abstract Binary Computation & Elegant Finite Graphs],
// Top left header
institution: [Super University],
// Top right header
course-short-title: "ABC & EFG",
date: "1. January 1970",
// Exam details, appears below the title
course-code: "π",
duration-minutes: "90",
// Configure personal information fields
ask-trainer-name: false,
ask-group: true,
ask-student-number: false,
// Configure language, either "de" or "en"
language: "en",
// custom instructions
// instructions: none,
// change font-size
// font-size: 12pt,
// custom paper size
// paper-size: "a4",
// custom numbering of exercises
// exercise-numbering: "1",
)
// First exercise
#exercise("Relations and their properties", 2)
Consider the relation $R subset NN^2$, defined as follows:
$ (x, y) in R #h(0.5cm) <==> #h(0.5cm) x + y #mtext[is odd]. $
Is $R$ reflexive? transitive? symmetric? antisymmetric?
// Second exercise
#exercise("Properties of functions", 3)
Let $f : (0, +infinity) → (0, +infinity)$ with $f (x) = e^(-x)$.
// Several sub-questions
#exercise-items((
(1, [Is $f$ injective?]), // The first argument is the number
// of points the item is worth
(1, [Is $f$ surjective?]),
(2, [Is $f$ bijective?]),
))
// Third exercise
#exercise("Logical operators", 3.14)
Consider the following truthtable:
#align(center, table(
stroke: horiz-only(0.5pt),
columns: (auto, auto, auto),
align: center,
[$A$], [$B$], [$A or B$],
[1], [1], [1],
[1], [0], [1],
[0], [1], [1],
[0], [0], [1],
))
// More sub-questions
// override-points to false, so that the exercise total points are not recomputed.
#exercise-items(override-points: false, numbering: "i)", (
(1, [Is the truthtable correct?]),
(1, [If not, fix it.]),
))

BIN
thumbnail.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 119 KiB

View file

@ -1,8 +1,14 @@
[package]
name = "exam"
version = "0.1.0"
entrypoint = "lib.typ"
version = "0.2.0"
entrypoint = "src/lib.typ"
authors = ["Gaspard Jankowiak"]
license = "MIT"
description = "Exams for NAWI Graz."
compiler = "0.14.0"
categories = ["layout", "office"]
[template]
path = "template"
entrypoint = "exam.typ"
thumbnail = "thumbnail.png"