How to create a document with indented sections?

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.

How is this achieved in typst?

Something like this may be a start:

#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)

What do you mean by numbered and referenced paragraphs?

And what is “indented sections”? Headings or list items?

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.

Hey - there is an open issue about that feature request.

If you want to automatically label all your elements you’re able to add the functionality f.e. by using one of the snippets in the issue.

There is How to get the number of the first and last paragraph on a page and show the result in the header? - #8 by sijo for numbering paragraphs. Like in How to number paragraphs similarly to Bible verses?, you have par.line for numbering lines, which is common in some cases. Numbering and referencing each paragraph seems more niche.

How exactly do you want to do that?

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)

= Heading
#lorem(20)

#lorem(20)

@par1

= Heading
#lorem(20)

#lorem(20)
@par7

#lorem(20)

2 Likes

There is Support paragraph numbering (legal writing) · Issue #5001 · typst/typst · GitHub.