How to correctly use state for an ordered outline?

Hello everyone,

For a personal project where i’m trying to create a sorted outline by alphabetical order i found myself blocked due to my understanding of state that i consider has a global variable.

The current implementation of this ordered outline is :

#let alphabet-displayed = state("alphabet-displayed", ())
#let ordered-outline() = context {
  let current-page = here().page()
  let headings = query(selector(heading.where(level : 2)))
  let sorted-headings = headings.sorted(
    key: heading => {if heading.body.has("text") {heading.body.text} else {""}}
  )
  sorted-headings.map(entry => {
      let first-letter = entry.body.text.first() 
      // reimplement default outline.entry
      if entry.numbering != none {
        numbering(entry.numbering, ..counter(heading).at(entry.location()))
      }
      [ ]
      /*    Focus Here    */
      if (first-letter not in alphabet-displayed.get()){
        text(first-letter) + linebreak()
        alphabet-displayed.update(current => current + (first-letter,))
      }
      h(4pt) + entry.body
      box(width: 1fr, repeat[.])
      [#entry.location().page()]
    }).join([ \ ])
}


== Aaaa
some song
== Abaaa
some song
== B
some song
== C
some song
== D
some song

#pagebreak()
= Index
#ordered-outline()

With result :

Result Image

I want the letter of each “sub section” of the outline to be shown only one. How can i achieve this result ? In the example give, “A” should only be noted once even thought multiple song begin with A

Thanks in advance for your help.

Edits

EDIT 1 : Reformat code & image for following @Andrew comment.

Hi there,

Are you creating an index?

This may be of some help in-dexter – Typst Universe or at least some code they are using. Or perhaps glossarium – Typst Universe.

1 Like

Hello,

I’m actually trying to make an index from scratch.

While those libraries are great, they don’t quite fit what I’m trying to achieve, especially after looking through the source code.

I’d like to stick with the state approach, as I think it’ll help me deepen my understanding of how it all works.

Hello. I can’t compile a document that uses your function. See https://sscce.org.

1 Like