Repository navigation
Description
Description
Currently, it is only possible to determine whether an element occurs after or before a specific element. However, it is not easily possible to determine whether an element is "inside" a specific element.
For example, take the following document:
= Chapter 1 <C1>
This <Label> is inside Chapter 1
== Section 1
This <OtherLabel> is inside Chapter 1 and Section 1, but not Section 2
== Section 2
And <YetAnotherLabel> is inside Chapter 1 and Section 2, but not Section 1
It becomes very difficult to determine whether an element is inside a chapter. This is especially true when combined with calls to state(), where one might be interested in seeing if a state update was made in a specific chapter or not.
Use Case
The primary use-case would be to determine whether or not an element is specifically inside another element. Take the following document as an example:
#let s = state("state", 0)
#let valueForChapter(chapter) = {
locate(loc => {
let tmp = query(selector(<StateUpdate>).after(chapter), loc)
if tmp == () {
// The chapter has no value associated. We'll return 0
0
}
else {
s.at(tmp.first().location())
}
})
}
Expected: 10 / Actual: #valueForChapter(<H1>)
Expected: 0 / Actual: #valueForChapter(<H2>)
Expected: 20 / Actual: #valueForChapter(<H3>)
= H1 <H1>
The value of H1 is 10
#s.update(10)
<StateUpdate>
= H2 <H2>
This chapter does not have a value associated.
= H3 <H3>
The value of H3 is 20
#s.update(20)
<StateUpdate>
With a .inside() method, it would become trivial to determine if H2 has a value associated to it or not. Without this method, one would need to use something like selector(<StateUpdate>).after(<H2>).before(<H3>), which in turn becomes problematic inside loops.
Activity
PgBiel commentedon Aug 14, 2023
In principle, an
.in()or.inside()method would theoretically only check whether or not something is displayed in place of the parent element. That is, I believe your use case doesn't really apply to selectors in general. What you're looking for is a selector that considers counter values, and I'm not sure if I'd call it "inside". More like counter equals, maybe? This'd need some thought, however.MechMK1 commentedon Aug 15, 2023
@PgBiel Counter values would work for sections, but I think it would also be a nice generalization for when one element would be inside another (e.g. an element being inside a table)
SolidTux commentedon Dec 15, 2023
I think
insidewould fit better to something that is actually inside the thing. E.g. when you have a heading with mathIt would be nice to have a selector that could match the equation in the heading, e.g. to do things like
As far as I know this currently requires the more elaborate setup
Edit: After some testing, it looks like this solution doesn't work for setting the math font in headings.
[-]Selectors should contain an `.inside()` function[/-][+]Ancestry/inside selector[/+]memeplex commentedon Aug 4, 2024
I've opened a different FR to match specific list levels. That can of course be implemented by adding a list depth attribute to lists or something like that. But it can also be seen as an instance of this issue, although list nested inside list nested inside list not nested inside list: i. doesn't look quite nice, ii. also requires a not nested selector.
memeplex commentedon Aug 5, 2024
Or perhaps this could be implemented like
{n,m}in regular expressions, "lists that are exactly 2 times inside other lists", or "lists that are more than 2 times inside other lists", etc.selector(list).inside(heading, 2)orselector(list).inside(heading, (3, -1)). May be generalized toinside/outsideas well asbefore/after, which can then be used to match things like "the third second level heading after the second first level heading" FWIW. Just thinking aloud, but it seems to me like a neat idea if it's not too crazy implementation-wise.[-]Ancestry/inside selector[/-][+]Ancestry/inside/within selector[/+]