How to create show rule in which the selector rule combines regex and style

If I wanted to apply a particular show rule to every “foo” and to every time there’s emph text then I’d just make two separate rules in which the selectors are different but the set function’s the same.

However, how do I do it if I want to apply a show rule only to instances of “foo” which are emph? In other words, if there’s a “foo” which is just roman or bold then the show rule doesn’t apply?

Say, for example, I want any foo to become foo:

foo → foo

foofoo

any emph text that isn’t fooany emph text that isn’t foo

1 Like

If the text is something specific, you can use where:

#show emph.where(body: [foo]): strong

_this contains foo_

this _foo_ is on its own

this foo is normal

Note that the reason this is so different and so much shorter than what is shown in the linked question is that you want to find emph and “foo”, not emph or “foo”.


Since you mention regex in your title: you can’t do that inside where, but you can just use an if in a simpler show rule:

#show emph: it => {
  // I prefer writing this as a "early return",
  // in case the actual show rule becomes longer
  // in this `if`, you can also have regex and other complex stuff
  if it.body != [foo] {
    return it
  }

  strong(it)
}

_this contains foo_

this _foo_ is on its own

this foo is normal

Just in case you or someone looking for this question wants

any emph text that isn’t fooany emph text that isn’t foo

That would look like this:

#show emph: it => {
  show "foo": strong
  it  
}

_this contains foo_

this _foo_ is on its own

this foo is normal  
1 Like

Your answer was extremely helpful. Thank you!

By the way, are you just a really helpful individual, or is writing the next version of the Typst documentation your job?

Just helpful :slight_smile: and I’m a teacher and currently there’s a school break, so that means I have a bit more time rn and also an “explain stuff to people” deficit :stuck_out_tongue:

Putting on my mod hat again: if your question is completely answered, don’t forget to add the :white_check_mark: checkmark!