How to place verse numbers in a sidebar?

The old answer

Here’s a half solution for option A.

#set page(height: auto, width: 300pt, margin: 15pt)

#let make-bible(usfm) = {
  show regex(`\\v \d+\s*`.text): it => context box(place(
    bottom + start,
    dx: 10pt - here().position().x,
    dy: -0.3em,
    align(right, box(width: 0pt, {
      set text(0.6em)
      it.text.replace(`\v `.text, "").trim()
    })),
  ))


  usfm.replace(regex(`\r?\n`.text), " ")
}

#make-bible(
  ```
  \v 1 I put the text in a raw block to avoid unexpected escaping sequences.
  \v 2 Ideally, you should read it from a file.
  \v 3 Refer to the documentation for details.
  \v 4 https://typst.app/docs/reference/data-loading/read/
  \v 18 This was how the birth of Jesus Christ took place. His mother Mary was engaged
  to Joseph, but before they were married, she found out that she was going to have a
  baby by the Holy Spirit.
  \v 19 Joseph was a man who always did what was right, but he did not want to disgrace
  Mary publicly; so he made plans to break the engagement privately.
  ```.text,
)

#set page(height: auto, width: 350pt, margin: (left: 40pt, right: 15pt))


#let make-bible(usfm) = {
  show regex(`\\v \d+\s*`.text): it => [#metadata(
    it.text.replace(`\v `.text, "").trim(),
  )<verse>]

  set par.line(numbering: _ => {
    let this-line = here().position().y
    let last-line = this-line - par.leading.to-absolute() - text.size / 2

    let numbers = query(<verse>)
      .filter(verse => {
        let y = verse.location().position().y
        last-line < y and y <= this-line
      })
      .map(verse => verse.value)

    // The height should be a fixed value, or the box will affect `here().position()`.
    // This circular dependency will cause the layout to fail to converge.
    box(height: 0pt, align(bottom, {
      super(numbers.join(", "))
    }))
  })

  usfm.replace(regex(`\r?\n`.text), " ")
}

#make-bible(
  ```
  \v 1 I put the text in a raw block to avoid unexpected escaping sequences.
  \v 2 Ideally, you should read it from a file.
  \v 3 Refer to the documentation for details.
  \v 4 https://typst.app/docs/reference/data-loading/read/
  \v 18 This was how the birth of Jesus Christ took place. His mother Mary was engaged
  to Joseph, but before they were married, she found out that she was going to have a
  baby by the Holy Spirit.
  \v 19 Joseph was a man who always did what was right, but he did not want to disgrace
  Mary publicly; so he made plans to break the engagement privately.
  ```.text,
)
3 Likes