Skip to content

Resetting show and set to its default / Revoke rules #420

Open

Description

@FedericoStra

I think we need a way to reset show rules to their default.

// introduce a custom style for all text objects
#show text: box.with(stroke: .4pt + red, outset: (bottom: .3em, rest: .1em), radius: .1em)

Some simple text.
Some other text wrapped in a tight red box.

// how do we turn it off?!
#show text: it => it

Ugh!

Similarly, after a set rule there is no simple way of "unsetting" it (reverting to the default), apart from explicitly writing the default or wrapping everything in a scope.

Activity

  1. changed the title [-]Resetting `show` to its default[/-] [+]Resetting `show` and `set` to its default[/+] on Mar 29, 2023
  2. user202729 commented on Mar 29, 2023

    @user202729
    Contributor

    Related: #229, #351.

  3. user202729 commented on Mar 29, 2023

    @user202729
    Contributor

    In this particular case one working way is

    // introduce a custom style for all text objects
    #[
    #show text: box.with(stroke: .4pt + red, outset: (bottom: .3em, rest: .1em), radius: .1em)
    
    Some simple text.
    Some other text wrapped in a tight red box.
    ]
    
    Good
    

    I think it's cleaner than TeX's "save stack" way to handle these things, but it doesn't solve everything.

  4. FedericoStra commented on Mar 29, 2023

    @FedericoStra
    Author

    Yes, I'm aware that one can localize the effect with a block

    apart from explicitly writing the default or wrapping everything in a scope.

    I was thinking more about the case where one wants to revert some change introduced by an imported template.

  5. added
    feature request
    styling
    on Mar 31, 2023
  6. rauberdaniel commented on Jul 5, 2023

    @rauberdaniel

    Another currently (I think) unsolvable example:
    I have defined a custom show for headings so they are wrapped within a block with a stroke. However, in some special cases (not dependent on level or similar) this should not be the case.

  7. laurmaedje commented on Jul 5, 2023

    @laurmaedje
    Member

    Another currently (I think) unsolvable example.

    You might be aware, but you can reconstruct the heading from its body:

    #show heading: block.with(stroke: 1pt, inset: 4pt)
    = Hello
    
    #show heading: it => it.body
    = Hello again
    

    In theory, "Hello again" should now be completely plain and unstyled, but through another bug it keeps the compiler-default show-set rule that increases weight and size. But in any case, you can get rid of the original show rule here by reconstructing the heading from scratch. But you also need to take care of adding the numbering etc. again so it's not really a satisfactory solution.

  8. istudyatuni commented on Aug 21, 2023

    @istudyatuni
    Contributor

    Maybe something like

    #show heading: block.with(stroke: 1pt, inset: 4pt)
    = Hello
    
    #show heading: it => it.default()
    = Hello again
  9. PgBiel commented on Oct 21, 2023

    @PgBiel
    Contributor

    So, I had this crazy idea:

    #show heading: set text(red) <my-show-rule>
    = Red heading
    
    // syntax prone to bikeshedding
    #unapply @my-show-rule  // or <my-show-rule> or something
    = Not red heading
  10. Zheoni commented on Nov 8, 2023

    @Zheoni
    Contributor

    This could also fix #229 by manually stopping recursion. Something like

    #show "fancy": it => {
      unnaply @my-show-rule
      raw("fancy")
    } <my-show-rule>

    or

    #show "fancy": it => {
      unnaply self
      raw("fancy")
    }
  11. laurmaedje commented on Nov 8, 2023

    @laurmaedje
    Member

    The problem is that you do want to retain the show rule for nested content not generated by the rule (consider list show rules on a list in a list). You only do not want it on the element itself and content generated by the rule. Otherwise, fixing the recursion bug would be easy.

  12. Zheoni commented on Nov 8, 2023

    @Zheoni
    Contributor

    I wasn't trying to say it was a simple thing, I'm sorry if you understood that. I was just throwing out an idea on the previous suggestion without thinking about the implementation. 😔

  13. laurmaedje commented on Nov 8, 2023

    @laurmaedje
    Member

    No no, don't worry. It's good to see ideas thrown around, I was just pointing out a possible problem with the approach to add to the discussion.

  14. yochem commented on Dec 6, 2023

    @yochem

    Might be related: there is currently no way to see the default show rule set by typst. That would be very useful to tweak minor parts of an element.

  15. 15 remaining items

  16. laurmaedje commented on Jan 16, 2024

    @laurmaedje
    Member

    Note that the idea was only to replace show: x with apply x not show x: y.

  17. polazarus commented on May 2, 2024

    @polazarus

    Another way to get around it would be to provide access to raw and immutable commands in some kind of "builtin" namespace.

  18. zhc7 commented on May 12, 2024

    @zhc7

    Can we know when will the feature be implemented? Or are there some sort of thread that tracks this?

  19. laurmaedje commented on May 13, 2024

    @laurmaedje
    Member

    The design on this feature is not yet finalized because there wasn't consensus on what it should and shouldn't do, so it will likely still take a while. The latest discussion about it is in the styling forge thread.

  20. CheeseTurtle commented on Jul 16, 2024

    @CheeseTurtle

    I think it should be #unshow, personally. Also, perhaps #show could return a unique identifier that could then be used to revoke that particular layer of redefinition?

    More support for introspection on show and set would be excellent as well.

    At the very least, there should be a way to specify within the #show declaration itself that the definition should NOT be applied recursively. In fact, I think this behavior (non-recursion) should be the default, and an extra keyword or keyword suffix (#show*, #show!, etc) should be required to activate recursive application of a #show rule. (For consistency's sake, #set could also have this behavior.)

  21. Andrew15-5 commented on Sep 26, 2024

    @Andrew15-5
    Contributor
  22. Andrew15-5 commented on Oct 15, 2024

    @Andrew15-5
    Contributor

    Based on #5199, I want to add a similar issue that I faced with 0.12.0-rc1.

    Previously, when I was using string/text substitution in my documents, I would escape/cancel the show rule, which worked as expected:

    #show "sometext": "something else"
    sometext \sometext

    image

    This is very useful when you have some text you want to substitute (or transform) throughout the document, but in rare cases you need to insert it verbatim/leave it as-is. But now this doesn't work:

    image

    Which is apparently a fixed bug and not a broken feature. Currently, there is no way to work around this, other than using variable, with the substituted content and/or using locally scoped show rule. This has several downsides:

    1. The text looks a little off, since now you have # in certain places, or the whole variable can be unintentionally highlighted by the IDE (for Typst and the default color scheme it's colored in purple), making unnecessary accent on such substitutions.
    2. The spell checker that I use won't recognize the variable as a word, which can lead to undesired warnings and possibly uncaught mistakes.
    3. Using locally scoped show rules is much more verbose than just one global rule (depends on the number of usages in a particular document).
    4. Using variable requires manual importing it in every file (if document has many files), which is also more verbose and messy than a single root-scoped show rule.
    5. With the revoke syntax it can be more manageable, but it is still more verbose and won't work in certain situations.

    The particular situation I have in mind is when you have a paragraph in your (big) document where want to use the substitution in one instance, and in the other you want the text verbatim:

    #show "Writer": "LibreOffice Writer"
    
    Writer --- #lorem(7). It #lorem(10). \Writer #lorem(20)

    Here, with the backslash "bug", you can easily cancel one rare occasion where you don't want to substitute text. Using any other method would result in a (messy and) more verbose solution. I like the backslash as it only 1-char-fix and since it's rarely used in the document, it's not a big deal at all.

    I would like to potentially bring this bug back and rebrand it as a feature instead. One place of conflict is the \u{} syntax for inserting a Unicode code point like this:

    A can be inserted as \u{41}.

    Currently, this also matches the show rule the same way, the verbatim Unicode code point is inserted. I.e., show "A" will apply to A and to \u{41}. And here comes the decision: will the \\u{41} produce A that won't match the show "A", or will it produce \u{41}? In any case, I think this is a super niche thing, to escape a dedicated/special syntax for inserting Unicode code points that is rarely used in of itself (but some do use it more actively than others). I don't think it makes much sense to escape the show rule by prepending a backslash, but what is more practical is printing \u{41} verbatim (when showing examples etc.)

    Other than this tiny detail, I think it should be possible. Of course, someone might want to match "\text" in a show rule, but now we are literally talking LaTeX macros (ain't no one wants those in Typst, amiright). But this also can be the second point of discussion.

    Related: #5209.

  23. bluss commented on May 19, 2025

    @bluss
    Contributor

    Does this issue include revoking default rules? Use case: I want a raw block that inherits the document's text.font, text.size.

    Edit: Now issue #6302. Feel free to edit it.

  24. laurmaedje commented on May 19, 2025

    @laurmaedje
    Member

    @bluss I think that is worthy of its own issue.

  25. TehBrian commented on May 4, 2026

    @TehBrian

    Just wanted to toss my two cents into the discussion. Apologies if these points have already been brought up.

    I find that show/set rules work excellently for adjusting the appearance of text marked up with Typst's custom syntax. For example, = Heading being styled with show heading: it => [ ❗ #it ❗].

    If one element should look different than the rest, then a custom function could be made. For example, #let h1-special(body) = { heading(level: 1, [✨ #body ✨]) }.

    However, since show rules apply globally, even to elements inside custom functions, if one custom function is made, then ALL show rules must be converted to custom functions as to not affect other custom functions.

    This essentially eliminates the usefulness of the custom syntax in the markup language.

    In my last project, I found that once I had a case of = One Special Heading that had to be converted to h1-special[One Special Heading], I had to convert all usages of = Heading to h1[Heading].

    This isn't unusable, but it'd be nice to leverage Typst's markup syntax, such as _emphasis_, = Heading, *strong*, and so on.

    Unapplying show rules, I believe, would fix this, but I'd definitely be interested in alternative ways to solve this problem.

    Thanks for reading :-)

  26. laurmaedje commented on May 5, 2026

    @laurmaedje
    Member

    The more idiomatic way would be to keep the styling rule-based, but local, like this:

    #let h1-special(body) = {
      show heading: it => ..
      heading(level: 1, body)
    }
    
    #h1-special[One special heading]

    or even

    #let special(body) = {
      show heading: it => ..
      body
    }
    
    #special[= One special heading]

    Then, depending on whether you use it or it.body in the show rule, you get the effect of the more global rule or you don't.

    Writing the show rule that uses it.body can be a bit annoying and that's something we're aware of. A mechanism to revoke the rule would help with this, but only to return to the default style, not to tweak it. To do this nicely, we need something more general. I haven't yet put my thoughts on the matter into text form, but I gave a talk on it at the last Typst meetup.

  27. TimeTravelPenguin commented on Aug 20, 2026

    @TimeTravelPenguin

    I just wanted to leave a comment for anyone else looking for a workaround from within odd circumstances.

    You can use labels to filter your styling. In my case, I want all links, citations, and references to have the same blue, underlined look. However, I just added a "badge", which links to external GitHub URLs. As a result, I have used my "badge" as the body of link. To avoid the styling, I have added a label and modified my show rule:

    #let demo_badge(url) = [
        #link(
          "base_url" + url,
          block(
            fill: green,
            text(green)("Some badge message"),
          ),
        ) <badge>
    ]
    
    #show link: it => context {
      if it in query(<badge>) {
        return it
      }
    
      set text(blue)
      underline(it)
    }

    This can be implemented much cleaner, but it shows the general idea. Hopefully this is useful to someone else who needs a quick-and-dirty escape hatch.

  28. MMachado05 commented on Sep 21, 2026

    @MMachado05

    Adding another use case: a template package that I installed automatically adds the underline show rule to links, and I naturally don't want to change the original package. I do, however, want to "unapply" this show rule within a particular function I've written to use on top of this template, however, I'm unsure of how to do this. Naturally, the existing "wrap it in its own block" solution doesn't apply to me, since the block that sets that styling exists in Typst code that came with the package. Tough!

  29. laurmaedje commented on Sep 21, 2026

    @laurmaedje
    Member

    @MMachado05 For that particular problem, you can hack around it like this:

    #show link: it => {
      show underline: u => u.body
      it
    }

    But yeah, it would be a good use case for revoke.

  30. MMachado05 commented on Sep 21, 2026

    @MMachado05

    @laurmaedje I found a similar solution elsewhere, and that's exactly what I did! Didn't realize that underline was an actual element, rather than a property. Definitely still a novice here, lol.

    I did, however, see that rule revocation is on the roadmap, and naturally being 0.x software I'm content in recognizing that there's still things being actively developed. Excited to keep using it!

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 requeststyling

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions