Is there a more elegant way for an Enclosure chapter with main heading visible in outline, subchapters invisible, with custom titles shown and shown when referenced?

I wan’t to create a section named Enclosures without number. It shall have subsections called Enclosure 1, Enclosure 2, and so on. I guess this could be done without using headings, but I want the main chapter name to show up in the outline and the heading levels ot inherit the heading font, sizes and behaviour.

After many, many tries I got this script to perform as expected. But the implementation feels kind of hackish. Part of the code is needed to avoid duplicate labels in the outline. Since I did not find a simpler way to get this to work I used trial and error until it eventually worked.

Is there a more elegant way to do this, or using more standard commands?

To be clear, the enclosure section is in addition to other sections in the document. The “#show: enclosures” can be used anywhere in the document to insert this. Like the appendix. I use it after all chapters and appendices.


#let enclosure_numbering(..nums) = {
  let pos = nums.pos()
  if pos.len() == 1 { "Enclosures" }
  else { "Enclosure " + str(pos.last())}
}

#let enclosures(body) = {
  show heading: set text(font: ("Libertinus Sans", "New Computer Modern"))
  // this sets the reference text for the heading counter to "Enclosure 1", "Enclosure 2", etc.
  set heading(numbering: enclosure_numbering, supplement: none)

  // Level 1 shows in outline, level 2 does not.
  show heading.where(level: 1): set heading(outlined: true)
  show heading.where(level: 2): set heading(outlined: false)

  // Render only the numbering text on the page; body is kept empty.
  show heading.where(level: 1): it => {
    let num = context counter(heading).display(enclosure_numbering)
    block[
      // #set text(size: 18pt)
      #num
    ]
  }
  show heading.where(level: 2): it => {
    let num = context counter(heading).display(enclosure_numbering)
    block[
      // #set text(size: 12pt)
      #num
    ]
  }

  body
}

#pagebreak()

#show: enclosures

 = <enclosures>
  Main heading to be seen in the outline and useable as a reference. The body of this heading is empty, so only the reference text is rendered on the page.

== <enc-1> 
Subheading hidden in the outline, but, useable as a reference. 

 #lorem(50)

  #figure(
      placement: none,
      image("../assets/Figures/67595261.png", width: 50%),
      caption: [Example using a known logo.],
    ) <fig_logo_example>

== <enc-2>
Test of reference @enc-1 and @enc-2. And @enclosures.

OUTPUT:

For easy numbering, the numbly library is really nice, I seem to achieve the same result as yours with the following:

#import "@preview/numbly:0.1.0": numbly

#let enclosures(body) = {
  show heading: set text(font: ("Libertinus Sans", "New Computer Modern"))
  set heading(numbering: numbly("Enclosures", "Enclosure {2}"), supplement: none)
  show heading.where(level: 2): set heading(outlined: false)

  body
}

So approximately the same but I’m not sure I understand the need for the last show rules : I get the same result in the outline and references you seem to want?