Part independent section

Set rule for a document with part sections included in the heading’s level function hierarchy but the part of the label string corresponding to level: 1 (part) not shown for level > 1. In this case, I set the label "I" for parts:

// Create a chapter counter and initialize it to 1
#let chapter-count = state("chapter-count", 1)

#set heading(numbering: (..nums) => {
  if nums.pos().len() == 1 {
    // Level 1: Roman numerals for Parts
    numbering("I", nums.pos().at(0))
  } else if nums.pos().len() == 2 {
    // Level 2: Use the chapter state counter and increment it
    let current = chapter-count.get()
    chapter-count.update(n => n + 1)
    str(current)
  } else {
    // Level 3+: Use the chapter number followed by the position within that chapter
    let chapter-num = chapter-count.get() - 1  // -1 because it's already incremented for the next chapter
    let sub-nums = nums.pos().slice(2)
    str(chapter-num) + "." + sub-nums.map(str).join(".")
  }
})
= Part One
== Chapter One
=== Section
== Chapter Two
= Part Two
== Chapter Three
=== Section
=== Section
==== Subsection
==== Subsection
= Part Three
== Chaper Four

t.pdf (9.7 KB)

1 Like

Hi, do you have a question about this, if so what is the question?

Hello, no I don’t; clearly the category is “Showcase”. Thank you.

This person vmartel08 has changed it to “Questions”. I don’t know why. I’ve edited it again…

But your post doesn’t showcase anything. I also have a hard time understanding the first sentence. At least add a full example that people can compile and see how it looks. Better include the output yourself. Then I can see this as a Showcase post. Also, don’t forget to use an appropriate language identifier for code blocks.

Your last sentence makes your post look like it belongs to Questions category.

1 Like

Apologies for that, it did sound like a question before you modified your post. Just tried to help. Thanks for the showcase :nerd_face:

1 Like

You can simplify this by using numbering, counter, and spread operator:

#set heading(numbering: (..nums) => {
  let chapter-count = counter("chapter counter")
  if nums.pos().len() == 1 {
    // Level 1: Roman numerals for Parts.
    numbering("I", ..nums)
  } else if nums.pos().len() == 2 {
    // Level 2: Use the chapter state counter and increment it.
    chapter-count.step()
    str(chapter-count.get().first() + 1)
    // or
    // context numbering("1", ..chapter-count.get())
  } else {
    // Level 3+: Use the chapter number followed by the position within that chapter.
    numbering("1.1", ..chapter-count.get(), ..nums.pos().slice(2))
  }
})

= Part One
== Chapter One
=== Section
== Chapter Two
= Part Two
== Chapter Three
=== Section
=== Section
==== Subsection
==== Subsection
= Part Three
== Chapter Four

image

P.S. By “better include the output” I meant the screenshot of the relevant part of the document (like above). The PDF supposed to be uploaded in case a few shortcuts won’t cut it or a bug-related info is needed. Allow sharing PDF and ZIP files on the forum - #10 by laurmaedje