Why the definition of "show regex" doesn't work in a function?

I want to define several rules with show regex in a template. If you define show regex within the insole, before the bodysuit works.

#let my-template(
  body
) = {
  [My template]

  show regex("=+/?(.+)"): it => {
     it.fields()
  }

  body
}

But if I define a function in another file and call it from the template, it doesn’t work for me. Why does this happen?

#import "./sugar.typ": *

#let my-template(
  body
) = {
  [My template]

  __sugar()

  body
}

Let me leave an example

https://typst.app/project/rDUPwG7CTptF_cKa8D2_Pv

This looks like a duplicate of this post:

Edit: not exactly, but I think the answer there will help you a lot.

1 Like

This is because show only works on content within its scope. In your example, this scope is the interior of the __sugar() function definition.

The principle of show is that it will take everything that matches its selector below and within its scope, and process it using the function you specify. So when you call __sugar(), show did nothing because there is nothing else in it’s scope.

To fix this issue, you need to write another show in the scope you want. For example:

#let __sugar(content) = {
  show regex("=+/?(.+)"): it => {
    it.fields()
  }
  content
}

#let my-template(
  body
) = {
  [My template]

  show: __sugar

  body
}
1 Like

Thanks for the reply.

1 Like