Skip to content

Ancestry/inside/within selector #1926

Closed

Description

@MechMK1

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

  1. added
    feature request
    on Aug 14, 2023
  2. PgBiel commented on Aug 14, 2023

    @PgBiel
    Contributor

    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.

  3. MechMK1 commented on Aug 15, 2023

    @MechMK1
    Author

    @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)

  4. added
    styling
    and removed on Nov 24, 2023
  5. SolidTux commented on Dec 15, 2023

    @SolidTux

    I think inside would fit better to something that is actually inside the thing. E.g. when you have a heading with math

    = Test $alpha$
    
    $beta$

    It would be nice to have a selector that could match the equation in the heading, e.g. to do things like

    #show selector(math.equation).inside(heading): set text(font: "Something")

    As far as I know this currently requires the more elaborate setup

    #show heading: it => {
        show math.equation: set text(font: "Something")
        // set heading etc ...
    }

    Edit: After some testing, it looks like this solution doesn't work for setting the math font in headings.

  6. changed the title [-]Selectors should contain an `.inside()` function[/-] [+]Ancestry/inside selector[/+] on Jul 16, 2024
  7. memeplex commented on Aug 4, 2024

    @memeplex

    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.

  8. memeplex commented on Aug 5, 2024

    @memeplex

    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) or selector(list).inside(heading, (3, -1)). May be generalized to inside/outside as well as before/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.

  9. changed the title [-]Ancestry/inside selector[/-] [+]Ancestry/inside/within selector[/+] on Nov 5, 2025
  10. added
    introspection
    on Nov 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestintrospectionstyling

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions