I am currently writing a document where I have some appendix which requires to be have line numbers. Unfortunately, I have 4 chapters which need line numbers individually (each chapter starting from 1 again). I know this might be possible with raw text, but I really would like to use normal text and just reset the counter if possible.
What I tried was using #set par.line(numbering-scope: "page"), but this resulted in the start of new numbers on every new page. This does not work for me, because my content is longer than one page. I also tried resetting the counter like so #counter(par.line).update(0), but this did not have any effect as well.
Did any of you faced this issue before or know a solution to my problem?
I think this is currently not possible. According to the comments for layout-line-number in crates/typst-layout/src/flow/compose.rs , the par.line counter is currently different from normal counters, so this counter is not exposed to users through counter(par.line) or any other means.
Hey @Timo, welcome to the forum! In addition to adding a tag to your post, I’ve changed your question post’s title to better fit our guidelines: How to post in the Questions category
For future posts, make sure your title is a question you’d ask to a friend about Typst.
As explained by @ParaN3xus above, this is not currently possible due to technical limitations in the compiler. However, you can do it manually, if you don’t mind adding some extra code before your chapters. Note that this extra code has to be updated for a chapter each time you add more text to the previous chapter, so there’s a bit of a maintenance burden, but it can at least provide the results you are expecting.
The core idea is that you will add the following code before each chapter:
// N is the last line number in the previous chapter
#show: reset-line-numbers-from(N)
You will have to update N manually as you add more lines. (You don’t have to do this all the time - you can choose to only do it when exporting, for example.)
Here’s the code, and some sample usage:
// /!\ Pick your numbering format here /!\
#let num-format = "1."
#set par.line(numbering: num-format)
#let reset-line-numbers-from(n, format: num-format) = doc => context {
let previous-n = counter("par-line-reset").get().first()
set par.line(numbering: line => numbering(format, line - n - previous-n))
counter("par-line-reset").update(count => count + n)
doc
}
// Sample usage:
= ABC
Hello world
// Replace '2' with the last line number before here
#show: reset-line-numbers-from(2)
= Other chapter
Hello
world \
a \
a \
a \
b
// Line number of 'b' was 7
#show: reset-line-numbers-from(7)
= My own chapter
This
is a
chapter
#show: reset-line-numbers-from(4)
= My own chapter