Skip to content

query a specific content instead of the whole doc #7307

Closed as not planned
Closed as not planned

Description

@YPares

Description

Currently, query always searches through the whole document. Being able to call query on a specific content would make some introspection cases much more flexible. Is it a bit like #1926 but I believe it would cover a wider range of use cases.

Use Case

This could be useful for various cases which necessitate having information about all occurences of some type of content within a given scope, for instance conveniently align headings of some level withing some scope, eg. by wrapping them in a rect of the same width (that of the heading with max width).

For instance:

#let cnt = [
  === Subtitle xxxx
  #lorem(10)

  === Subtitle yyyyyyy
  #lorem(15)

  === Subtitle zzz zzz zzzzz
  #lorem(11)
]

#context [
  // We only query the lvl-3 headings _present in cnt_:
  #let widths = cnt.query(heading.where(level: 3)).map(it => measure(it.body).width)
  #let max-width = calc.max(..widths)
  // All headings will be rendered inside a rect of the exact same size each time:
  #show heading.where(level: 3): rect(width: max-width, align(center, it))
  #cnt
]

Activity

  1. added
    feature request
    on Nov 4, 2025
  2. YPares commented on Nov 4, 2025

    @YPares
    Author

    Here is a way to emulate this currently:

    #let scope-counter = state("scope-counter", 0)
    #let scoped(func) = context [
        #scope-counter.update(x => x+1)
        // Ensure we get unique label names for each call to `scoped`
        #let beg = label("scope-begin:" + str(scope-counter.get()))
        #let end = label("scope-end:" + str(scope-counter.get()))
        #hide[] #beg
        #func(selector => query(selector.after(beg, inclusive: false).before(end, inclusive: false))) 
        #hide[] #end
    ]

    You can then use this function to obtain a "locally scoped" query variant:

    scoped(query => [
       === Title 1
       Something
    
       === Title 2
       Something else
    
       #query(heading.where(level: 3))  // Will match ONLY the lvl-3 headings defined in this "scoped" block 
    ])

    EDIT: In case people are interested, I extended a bit this idea and turn that into a small library: https://github.com/YPares/typstware

  3. cAttte commented on Nov 4, 2025

    @cAttte
    Contributor

    here is a similar issue: #5218 (which you could solve by includeing the external doc then querying on that piece of content, assuming that the solution to this issue doesn't require embedding the content, as does the (very nice) workaround)

  4. laurmaedje commented on Nov 5, 2025

    @laurmaedje
    Member

    Is it a bit like #1926 but I believe it would cover a wider range of use cases.

    Could you elaborate a bit on this? Which cases would this help with that #1926 does not cover?

  5. added
    introspection
    on Nov 5, 2025
  6. YPares commented on Nov 5, 2025

    @YPares
    Author

    @laurmaedje Any case where you do not have a section title that exactly delimits the section you want to scope. #1926 uses typographic containers to serve as scopes, here with just a #let x = [ ... ] block you can define the exact scope you want to cover, regardless of how it will be rendered.

  7. laurmaedje commented on Nov 5, 2025

    @laurmaedje
    Member

    The way you wrote it with cnt.query wouldn't really work I think (because cnt is not yet in the document and could be added multiple times to the document and then the interior might change depending on the surrounding styling). So some adaption would be needed there.

    But I think what you want could be done with how I'm imagine a within selector. For example, you could say:

    = A
    #context [
      #let headings = query(selector(heading).within(here()))
      Count within: #headings.len() // => 2
      = B
      = C
    ]
    = D

    Basically here() identifies the context block and within(here()) would capture anything in that context block.

  8. laurmaedje commented on Nov 10, 2025

    @laurmaedje
    Member

    I'll close this in favor of #1926.

  9. haennes commented on Jan 7, 2026

    @haennes

    Problem Statement

    Me and a friend of mine are developing a software to convert typst documents to anki cards.
    To make it easier to generate cards whilst also taking notes we added a question and anwer feature:

    //q declares a question
    //a declares an answer
    //ids identify the respective card the question and answer should be attached to. i.e. 187 / 186
    = #q(187)[#q(186)[VPN]]
    #a(186)[
      - tunneled connection into a network
      - virtually extending a private network
    ]
    - #q(187)[Advantages]
      #a(187)[
        - securing unencrypted connections / dns
        - use firewall fo your router
        - untrusted environments
        ...by encrypting until your endpoint
      ]
    

    While rendering works "completely" fine we see extensive ram usage when querying for the created cards.
    This is due to us having to basically emulate a function like query(label(...), scope: doc) through this library which i have quickly written myself.
    The reason for this is that in order for the metadata annotations that are generated by q / a - calls to be able to be queried "normally" the entire content of the document would need to be included. However when generating a anki card we only want that specific content.

    Tried Workarounds

    Creating a box with an size of zero was briefly explored which upon using a label inside a q/a-call would result in a hard error by typst as there are multiple locations for the same label in the document.

    The only remaining option was to use the above mentioned implementation to emulate such a query function.
    Due to memoization (see #6508) ram usage grows rapidly exceeding 60GiB whilst querying.

    Call for Reopening

    #1926 does not solve this issue either as querying over data that might not be included in the final document would be greatly appreciated. This is what the above mentioned library does in a very inefficient way.

    #5218 suggests a similar concept that when allowing to pass custom content to query could be solved by a simple:

    query(..., include "abc.typ")

    for anyone interested the typ2anki library can be found here

  10. laurmaedje commented on Jan 7, 2026

    @laurmaedje
    Member

    Supporting this opens up a whole new can of worms that I'd rather not open. It's not as simple as just drilling down into the content as the content would need to be properly realized for this to behave sensibly. It would also open up the question of other introspection functionality supporting something similar. This introduces a lot of complexity for something that feels fairly niche to me.

    I don't have the time to investigate your concrete problem, but did you try asking in the Forum or on Discord about it? Maybe someone has an idea for an alternative approach that does not need this.

  11. haennes commented on Jan 10, 2026

    @haennes

    I do agree that this is quite niche and it is absolutely not the most pressing issue.
    Imho this absolutely is a nice to have that should be implemented when the majority of all work is done on typst (speaking: almost no open issues on typst). I am rather more confused why #5218 is still open instead of this one :)

    Not making any promises here, but would you / typst accept a PR that adds this functionality or would it be rejected due to the large amount of possible bugs this could generate?

    I will try asking on the forum, thanks for the suggestion.

  12. laurmaedje commented on Jan 12, 2026

    @laurmaedje
    Member

    Not making any promises here, but would you / typst accept a PR that adds this functionality or would it be rejected due to the large amount of possible bugs this could generate?

    At the current stage, I would not accept a PR. Not just because it's implementation-wise complex and could indeed easily be buggy or error-prone, but also because I'm not fully convinced by the design as presented. Besides pinning down the behavior of the query function as presented, there is for example also still the concern of "It would also open up the question of other introspection functionality supporting something similar." Now, it's not impossible that I could be convinced by a future design, but given the nicheness I don't really have capacity to engage to much in design discussions about it currently.

    I am rather more confused why #5218 is still open instead of this one :)

    There's probably various issues that could be closed as not planned, we're not always on top of that. This one here was in particular closed because #1926 presents an alternative that I prefer.

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 requestintrospection

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions