I’m trying to create some numerated lists, and use each of them in separated levels. For instance, I have a function similar to the following:
// allows a counter to span over the whole document
#let c = counter("level1")
#let list_level1(body) = {
set enum(numbering: it => {
c.step()
context{
let num = c.get().first()
let lvl1_prefix = text("Lvl1. ", weight: "bold")
let lvl1_counter = numbering("1", num)
if num < 10{
text(lvl1_prefix+lvl1_counter+"º")
} else{
text(lvl1_prefix+lvl1_counter)
}
}
})
body
}
In my main file I use:
#import "file.typ": list_level1
#list_level1[
+ Item 1
+ Item 2
...
+ Item 10
]
This give me the following output:
Lvl1. 1º Item 1
Lvl1. 2º Item 2
…
Lvl1. 10 Item 10
Now I want to create another enum function to be called in the second level. I already could create it, however I’m struggling to find how many items exist in this level. To be clearer, I want to achieve the following:
Lvl1. 1º Item 1
Single. Subitem 1
It shows the Single. if I have only one subitem, otherwise it should show:
Lvl1. 1º Item 1
Lvl2. Subitem 1
Lvl2. Subitem 2
…
A few moments ago I was trying the following:
#let list_level2(body) = {
let itens = body.children
set enum(numbering: it => {
let par_prefix = text("Lvl2. ", weight: "bold")
let par_counter = numbering("1", it)
if itens.len() == 1{
text("Single. ")
} else{
if it < 10{
text(par_prefix + par_counter+"º")
} else{
text(par_prefix+par_counter)
}
}
}, indent: -2em)
body
}
Then, in the main file:
#import "file.typ": list_level1, list_level2
#list_level1[
+ Item 1
#list_level2[
+ Subitem 1.1
+ Subitem 2.1
]
+ Item 2
#list_level2[
+ Subitem 2.1
]
...
+ Item 10
]
However I could never get the Single. prefix, even with only one subitem for Item 2. In my research through the forum, docs and AI, I could find the query
and selector
, but I was unable to use them with enum
.
Furthermore I found the items.len()
returns the value 3 despite declaring only subitem 2.1
. When I hover the mouse over item
it shows:
Sampled values
([ ], item(body: [Subitem 2.1]), [ ])
This array explains the value 3, but I could not find a way to change this. In the main file I tried:
#list_level1[Item 1][Item 2] ...
and
#list_level1(
[Item 1]
[Item 2]
...
)
and
#list_level1 {
[Item 1]
[Item 2]
...
}
None worked. Could anyone help me on this?