#Counting subheadings continuously

17 messages · Page 1 of 1 (latest)

chilly atlas
#

Hi everyone,
So I've been trying to render headings in a way that would look like this:

= Section 1
== Subsection 1.1 // or just 1
== Subsection 1.2 // or just 2
= Section 2
== Subsection 2.3 // or just 3

(This is actually because these subsections are questions in an exam)

A hacky solution I put up is to use a seperate counter for subsections (or just directly counter(heading.where(level: 2))) and use it in the numbering function directly. This doesn't play well when referencing subheadings.

A better solution I believe would be to modify the counter(heading) counter in a show heading: rule, but then things don't work out very nicely (see below) because headings seem to compute their numbering before the composition with the show rule

Have you had any experience with trying to modify this counting flow? thanks

#

?r

#set heading(numbering: "1.")
#show heading: it => {
  counter(heading).update(5)
  context it
}

#context repr(counter(heading).get())
= Test

#context repr(counter(heading).get())
= Test 2
jolly terrace
#

?r

#let c = counter("subheading")
#c.step()
#show heading.where(level: 2): h => {
c.step()
block[#h.body #c.display() #label(str(c.get().first()))]
}

= one
== sub one 
== sub two
= two
== sub three
== sub four 


Reference to #ref(<1>)
spark cedarBOT
chilly atlas
#

yes I have done something like this but it doesn't allow references

jolly terrace
chilly atlas
#

Yes
Maybe the solution with an extra counter could be adapted to allow that but it is hacky, which is why I am also looking for more "idiomatic" solutions

solemn tiger
#

?r ```ansi
#set heading(numbering: "1.1")

#show heading.where(level: 1): it => {
// Get number of last level-2 heading of previous chapter.
let num2 = query(heading.where(level: 2).before(here()))
.map(h => counter(heading).at(h.location()).at(1, default: 0))
.at(-1, default: 0)

// Update heading counter to continue after the number
// of the last level-2 heading of the previous chapter.
counter(heading).update(num => (num, num2))

it
}

= Section 1

== Subsection 1.1

=== Subsubsection 1.1.1

== Subsection 1.2

= Section 2

== Subsection 2.3 <sec-2-3>

== Subsection 2.4

=== Subsubsection 2.4.1

= Section 3

== Subsection 3.5

@sec-2-3

jolly terrace
#

I've tried above to add a label with the counter but it doesn't work. But, I'm not sure if there is a more idiomatic way tbh. But someone else may have some better idea.

chilly atlas
#

still i don't fully understand why counter updates in the same show rule don't work but this is I guess just the same question as the one i asked in #quick-questions

#

Thanks anyway @jolly terrace!

solemn tiger
rose nest
#

yeah, so the counter value is determined at the heading's location and the heading's location is immediately before the show rule, so in particular before the counter update

chilly atlas
#

ok i see! And so is it a special case for headings or is it something generic between objects like here() and show rules?