You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
query a specific content instead of the whole doc #7307
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:
#letcnt= [
=== 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_:#letwidths= cnt.query(heading.where(level: 3)).map(it=>measure(it.body).width)
#letmax-width= calc.max(..widths)
// All headings will be rendered inside a rect of the exact same size each time:#showheading.where(level: 3): rect(width: max-width, align(center, it))
#cnt
]
#letscope-counter= state("scope-counter", 0)
#letscoped(func) = context [
#scope-counter.update(x=>x+1)
// Ensure we get unique label names for each call to `scoped`#letbeg= label("scope-begin:"+str(scope-counter.get()))
#letend= 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
])
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)
@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.
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 [
#letheadings= 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.
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
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.
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.
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.
Activity
YPares commentedon Nov 4, 2025
Here is a way to emulate this currently:
You can then use this function to obtain a "locally scoped"
queryvariant:EDIT: In case people are interested, I extended a bit this idea and turn that into a small library: https://github.com/YPares/typstware
cAttte commentedon Nov 4, 2025
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)laurmaedje commentedon Nov 5, 2025
Could you elaborate a bit on this? Which cases would this help with that #1926 does not cover?
YPares commentedon Nov 5, 2025
@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.laurmaedje commentedon Nov 5, 2025
The way you wrote it with
cnt.querywouldn't really work I think (becausecntis 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
withinselector. For example, you could say:Basically
here()identifies the context block andwithin(here())would capture anything in that context block.laurmaedje commentedon Nov 10, 2025
I'll close this in favor of #1926.
haennes commentedon Jan 7, 2026
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:
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:
for anyone interested the typ2anki library can be found here
laurmaedje commentedon Jan 7, 2026
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.
haennes commentedon Jan 10, 2026
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.
laurmaedje commentedon Jan 12, 2026
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
queryfunction 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.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.