How to Add line numbers before every line in a block?

I have the following Typst code:

  #block(
  fill: aqua,
  inset: 8pt,
  radius: 4pt,
  width: 100%,
  [
    #lorem(150)
  ],
)

I would like to automatically add a line number before every line which is generated by #lorem(150) . I also need enough space (around one tab) between the line number and the line.
Thank you.

The only one I know of is par.line.

#block(fill: aqua, inset: 8pt, radius: 4pt, width: 100%)[
  #set par.line(numbering: "1.")
  #lorem(150)
]

a\u{9}a

Tab character doesn’t have a defined width. If anything, it’s a control character that is unprintable, same goes to all first 32 characters.

Using my hack I implemented last month for filling N lines of space regardless of text, here it is:

#let color-box(
  body,
  fill: aqua,
  inset: 8pt,
  radius: 4pt,
  width: 100%,
  body-indent: 1.5em,
  dx: 0pt,
  numbering: "1",
) = {
  set par.line(numbering: numbering)
  show: block.with(fill: fill, inset: inset, radius: radius, width: width)
  body = pad(left: body-indent, body)
  let num(n) = if numbering != none { std.numbering(numbering, n) }
  layout(size => {
    let leading = par.leading.to-absolute()
    let one = measure([a], ..size).height + leading
    let height = measure(body, ..size).height + leading
    let line-count = int(height / one + 0.1)
    let line-numbers = range(1, line-count + 1).map(num)
    place(dx: dx, grid(align: right, gutter: leading, ..line-numbers))
    body + parbreak()
  })
}

#set par(justify: true)

#color-box(numbering: "1.", body-indent: 1.5em, dx: -0.3em)[
  #lorem(150)
]

So the only tricky part is counting the number of lines, which is always very close to an integer, sometimes is exactly an integer, depending on float division.

1 Like

Thank you Andrew for your support.

1 Like