How to right annotate a span of list items with a brace and text?

Very hacky and limited, but it works:

#let get-margin(side) = {
  if page.margin == auto or page.margin.at(repr(side)) == auto {
    calc.min(page.height, page.width) * 2.5 / 21
  } else {
    page.margin.at(repr(side))
  }
}

#let from() = [#metadata(none)<from>] + hide[.]
#let to() = [#metadata(none)<to>] + hide[.]
#let brace(dx: 1em, text) = context {
  let from = query(<from>).last()
  let to = query(<to>).last()
  let (_, x1, y1) = from.location().position().values()
  let (_, _, y2) = to.location().position().values()
  let left-margin = get-margin(left)
  let top-margin = get-margin(top)
  let magic-y-offset = 0.7em
  place(
    top + left,
    dx: x1 - left-margin,
    dy: y1 - top-margin - magic-y-offset,
    $ stretch(\}, size: #(magic-y-offset + 100% * (y2 - y1))) $,
  )
  place(
    top + left,
    dx: x1 - left-margin + dx,
    dy: (y2 + y1) / 2 - top-margin - magic-y-offset,
    text,
  )
}

- Item 1
- Item 2
- Item 3 #from()
- Item 4
- Item 5 #to()
- Item 6

#brace[two times]

Works for item gap 0+.

Margin function adapted from How can I robustly place a box exactly on the left edge of each page? - #4 by vmartel08.

The main limitation for cleanest approach is this: "Real" absolute option for `place` (or alternative function) · Issue #5233 · typst/typst · GitHub.

1 Like