I have written some lecture scripts, which had definitions, theorems, lemmata and so on. However, when preparing for an exam, it is nice, to have these extracted, for instance in the form on flashcards.
Here I show you, how to achieve it. I am using ctheorem, but this does not actually matter for the functionality.
main.typ
- the main script file
In order to extract the information, we can save the content of each environment, i.e. like this:
#import "@preview/ctheorems:1.1.3"
#let definition(..args, body) = {
context [#metadata((
title: args.at(0, default: none), body: body
)) #label("ctheorems-" + "definition")]
ctheorems.thmbox(
"definition", "Definition", supplement: "Def."
)(..args, body)
}
... your script
Instead of defining a definition environment directly, we make its arguments easily accessible elsewhere by passing title and body inside a labeled metadata element.
flashcards.typ
- the flashcard generator file
Here we have to include the main script (its content can be hidden). We then query over all definitions to create our new file(s):
include "main.typ"
#context [#metadata(counter(page).get()) <flashcard-section-pagenumber>]
On the following pages we have the definitions of:
#context query(<ctheorems-definition>).map(x => x.value.title).join(", ")
#context query(<ctheorems-definition>).map(x => {
let def = x.value
page(paper: "a7", flipped: true, {
strong(def.title)
def.body
})
}).join()
<flashcard-section-pagenumber>
is inserted, so that we can execute
typst query flashcards.typ <flashcard-section-pagenumber> --field value --one --root
which yields us the page from where the flashcards are generated. The resulting number should be noted and will in our example be 73.
Flashcard generation
Create a folder where the images can be saved an, here called flashcards
.
Finally we can generate the flashcards in the following way:
typst c flashcards.typ --pages 73- "flashcards/card_{n}.png"