Hi there, I am trying to create documents whose paragraphs and sections are all numbered, and need to be referenced between one another. I also want the indentation of all text in the paragraph to increase with the numbering level, like the attached screenshot.
#set enum(full: true, indent: 1cm, spacing: 1cm)
+ This is the first. #lorem(34)
+ this is the second #lorem(77)
This is the next paragraph
+ This is second level. It is a paragraph! #lorem(567)
After having looked at typst for longer, I can see why you’re asking these questions, as you’re thinking very much in a “typst” mindset (for obvious reasons).
What I mean, though, is exactly what I’ve written in the non-typst sense.
I want every paragraph to have a number, like the numbered list that @William has shown, and I want to be able to reference those paragraphs from others.
I tried using William’s approach, but unfortunately enumerated list items can’t be referenced (only headings, figures, equations and footnotes). I think I am therefore stuck using headings and a custom numbering system.
I can get something that works like this, but I think it would be useful to be able to reference anything using its location provided a custom ref “show” block supports them.
Here every paragraph is indented, numbered and referenceable:
#show par: it => {
let par-counter = counter("par-counter")
let hack = 1e-5pt
let fli = it.first-line-indent.amount.to-absolute().pt()
if calc.rem(fli, hack.pt() * 10) < hack.pt() { return it }
let fields = it.fields()
let body = fields.remove("body")
let fli = fields.remove("first-line-indent")
fli += (amount: fli.amount + hack)
par(first-line-indent: fli, ..fields, {
par-counter.step()
context [#metadata(here())<par>] + par-counter.display("1. ")
body
})
}
#show ref: it => {
if it.element != none { return it }
let match = str(it.target).match(regex("^par(\d+)$"))
if match == none { return it }
let par-num = int(match.captures.first())
let body = [ Reference to paragraph #sym.numero #par-num]
let par = query(<par>).at(par-num - 1, default: none)
if par == none { return text(red)[Paragraph #par-num does not exist.] }
link(par.location(), body)
}
#set par(first-line-indent: (amount: 1em, all: true))
= Heading
#lorem(20)
#lorem(20)
@par1
= Heading
#lorem(20)
#lorem(20)
@par7
#lorem(20)