Repository navigation
Fix show-set semantics - #3311
Merged
Merged
Conversation
1 task done
1 task done
Dherse
pushed a commit
to Dherse/typst
that referenced
this pull request
Feb 4, 2024
1 task done
1 task done
1 task done
Dikluwe
pushed a commit
to Dikluwe/typst-crystalline
that referenced
this pull request
Sep 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request fixes three long-standing issues with show-set rules. This comes with a change in semantics.
Show-set order
To first recap the current (old) semantics: A
show x: set y(..)rule is pretty much equivalent toshow x: it => { set y(..); it }. While consistent, this leads to a couple of problems. Those all fundamentally boil down to the fact that transformational show rules need to be applied one by one: The most local show rule for an element is executed first. If it doesn't useit, no further show rules are executed. If it does useit, thisitrepresents the output of the second-most-local show rule (which is executed lazily onceitis discovered in the content produced by the first show rule). Executing show rules in this inward-out fashion makes sense because it allows user to extend or replace the behaviour of outer/built-in show rules.Applied to show-set rules, the semantics are problematic though, as demonstrated below:
Desugaring this yields
After the inner show rule is executed, we have this:
And after the outer show rule is executed, we have this:
Now we can see the problem: The outermost show-set rule ends up producing the innermost content and thus, following Typst's usual rules, it takes precedence. As a result, show-set rules apply in inverse order.
While consistent and simple, the current semantics fundamentally force us to consider show-set rules in inwards-out order. To fix the problem outlined above, this PR implements new semantics for show-set which depart from the desugaring shown above. Instead of considering show-set rules one-by-one like normal show rules, all show-set rules are taken into account immediately as soon as Typst visits an element for the first time. As a result, we can properly respect their order and the snippet shown above just works, fixing #351.
Fields available in show rules and queries
This also has another benefit: It means that a styled element only has one well-defined, canonical state of its fields that can be exposed to its consumers in show rules and queries. Where in the old system, the element would "gain" new fields rule-by-rule, going through various intermediate states, there was no proper time to materialize fields configured via set rules into the element itself.1 As a result, only a few "blessed" fields were available in show rules and queries: Synthesized fields, which were originally designed to be used for synthesizing new fields from others (e.g.
raw.lines), but were then also abused to make fields available in queries.In the new system, we can simply materialize the element into its canonical state immediately after applying show-set rules. 2 This makes all fields available in show rules and queries and fixes #2193. The concept of synthesized fields remains, but is now only used for what it was originally intended to do.
Show-set on the same element
Another effect of the change discussed in the previous section is that show-set rules on the same element now just work. Rules such as
would previously not work for synthesized fields, because at the time where the show-set rule was applied, the heading's
numberingwas already materialized into the element from the existing styles. In the new system, the show-set rules are taking into account before materializing the element into its canonical state, fixing #2459.Limitations
Note that, as a result of these new semantics, the following code will not have the desired effect:
For the reasons laid out above (in particular those about an element's canonical state), I consider this acceptable. We're trading off some potential power for having a system that behaves in well-defined manner.
The only typical use case for rules like this I can think of would be if the show rule contained a conditional. However, those use cases should in my opinion rather be covered by show-set rules in combination with more powerful selectors. Similarly, previous discussions about potentially exposing mutable fields on elements in show rules also runs into the issue of what an element's canonical representation is, so I'd like to avoid those in favor of more powerful selectors, too.
I should also note that internal dependencies between show-set rules such as
are not supported in this PR. This could be implemented in a future PR via fixpoint iteration (to be independent of the order) if that's considered desirable.
Result
Fixes #351
Fixes #2193
Fixes #2459
Footnotes
I did experiment with a lazy approach for a while where fields wouldn't ever need to be materialized in-place, but that complicates things a lot and again raises the issue of what the canonical state of an element provided by
query()should be. ↩To be able to match show-set rules before materializing fields, selector matching needed to be made style-aware. This was implemented in https://github.com/typst/typst/pull/3290. For
figure.where(kind: table)to work, I unfortunately also had to do a throw-away pre-synthesis before matching becausekindwould beautoby default. That's somewhat unfortunate. A somewhat cleaner, but way more complex alternative would be to introduce "computed" fields that are only determined on-demand, but I felt that this wouldn't be worth it, at least not now. ↩