#let lec(date, number) = [
// <here are some if-checks to make sure the file has been included into a parent-file
// o.w. it uses different styling>
= Lecture \##number
Date: #date
]
Is it possible to
make them referenceable, so the following code would work:
#lec("23.09.2024", 6) <lec6>
// ....
For that, see @lec6
Somehow include the date of the lecture into the output of #outline()?
There are two things to remember about labels (directly quoted from Typst docs):
Inserting a label into content attaches it to the closest preceding element that is not a space.
The preceding element must be in the same scope as the label, which means that Hello #[<label>], for instance, wouldn’t work.
In your case, you cannot reference a sequence. You can automatically label your heading (which is a element), directly from lec. To include the date in the output, you simply need to add it to the heading’s body. The character \ is a linebreak.
#let lec(date, number) = [
// <here are some if-checks to make sure the file has been included into a parent-file
// o.w. it uses different styling>
= Lecture \##number\ Date: #date
#label("lec" + str(number))
]
#set heading(numbering: "1.")
#lec("23.09.2024", 6)
// ....
For that, see @lec6.
Maybe something like: “How do I label content from a function? How do I include text in the outline, but not in the heading?”. Although, you can create multiple posts for multiple questions with no worries in the future!
Hi,
I also played a little around with it, just didn’t have time to post it. @quachpas solution provides all the building blocks, but it does not fully work for multiple lectures.
The metadata element can be queried without label. When building the outline the location of the current heading element can be used to query the associated (next) metadata element with this code:
Welcome to the forum @tea! I’ve updated your post title according to our guidelines: How to post in the Questions category. Make sure your title is a question you’d ask to a friend!
I attached a <lecture-date> label to the metadata elements (so that they don’t interfere with other metadata elements). I also put just the date into it’s own function so that headings keep their normal markup in the document. The outline entry looks for the <lecture-date> metadata element between the referenced heading and the next one, so that it does not accidentally show the wrong date if you forget to specify it.
Some observations (about typst)
I think in situations where you are likely referencing an element (like with the it in show outline.entry and show ref rules), it would be nice to have the referenced location available at it.ref() or similar, instead of it.element.location().
It would be nice to not have to special case so much. For the last heading in the document, there is no next-head. It would be great if there was some end-of-document element that one could use instead of none in the first query. With it, the if next-head != none test would become unnecessary.
There is a similar problem if there is no date. It would also be nice if to be able to program the entire happy path first, and only check for none at the end. (Similar to ? operators in many programming languages.) I run into things like this frequently.
I would like it if it were possible to write
metadata(dt)
label("lecture-date")
instead of
[#metadata(dt) <lecture-date>]
in the date function. But alas, label does not produce content and therefore cannot be joined with metadata. (And
Of note, this doesn’t have much to do with the label syntax you’re using, it’s just that you need to be in markup mode to attach a label; they’re special-cased. That is, #{ figure(...) <lbl> } won’t work but #[#figure(...) <lbl>] will.
Yeah, my point was more about having to switch to markup mode to attach labels. I shouldn’t have changed the label syntax between my examples. (I guess label("lecture-date") felt more code-ish to me.)