What is the `it` in show rules exactly?

I cannot quite figure out what it is in show rules. I have this example that I use to prevent line breaks on inline equations:

#show math.equation.where(block: false): it => {
  box(it)
}

What is it exactly in this case? I am a bit puzzled that this just works, e.g. the correct math styling is still applied still applied to the inline equations.

3 Likes

The right part of this show rule is an unnamed function. The show rule docs have other examples; using a function and show-set are the two fundamental forms of show rules.

In the anonymous function, it is the parameter and is filled by the show rule with its target: the element that matched the show rule. In this case, the function would be called once for every non-block equation.

2 Likes

So is the show rule above equivalent to replacing every

#math.equation(block: false, ...)
or
$...$

with

#box(math.equation(block:false, ...))

?

1 Like

A useful way to explore what things are is to use the repr() function. For instance:

#show math.equation.where(block: false): it => repr(it)

$1+2=3$

Results in:

equation(
  block: false,
  numbering: none,
  number-align: end + horizon,
  supplement: [Equation],
  body: sequence([1], [+], [2], [=], [3]),
)

So in this case you can see that it is an equation, and you could access some of its properties within your function, or use those properties as part of a filter (the .where in the first part of the show rule).

I’d like to note also that the name it is not required, it’s just a common choice (though I’m not sure why). The following all produce identical results:

#show math.equation.where(block: false): a-name-other-than-it => repr(a-name-other-than-it)
#show math.equation.where(block: false): it => repr(it)
#show math.equation.where(block: false): repr

In the third case no name is given, but that’s ok because when the show rule is applied to the equation, it is looking for a function to pass what it finds to. In this case the show rule finds/matches an equation, then that equation is passed directly to repr which accepts a single piece of content as a valid way to call the function.

Edit:
The tooltips that the web app (and Tinymist) provide are also very helpful. Hovering my mouse over the it shows the same info that repr does:

3 Likes

Thanks a lot for the repr tipp. This allows me to explore things in typst myself instead of asking here :slight_smile:

2 Likes

Exactly. As an aside, since your show rule’s function (it => box(it)) only calls box and nothing else, you can just use that function directly:

#show math.equation.where(block: false): box

or, if you pass some additional parameters to box:

#show math.equation.where(block: false): box.with(fill: red)

This would be equivalent to using it => box(fill: red, it)

Yes, thanks for pointing that out. I forgot to mention that and it’s commonly misunderstood.

As far as I know, “it” is just the English word, i.e. it roughly means “the thing”. Another meaning that I’ve seen is “item” but I think that has a bit less attestation. I also like to use “body”, especially for templates intended to be called as “show-everything” rules:

// main.typ
#import "template.typ": my-template
#show: my-template(config: true)

This is the body

// template.typ
#let my-template(
  config: false,
  // ... and other options
) = body => {
  // ... apply some styling
  body
}

(the example is a bit more complex than it needs to be, as my-template is a function returning a function, but I wanted to write the example like I would actually write my templates)

3 posts were split to a new topic: What are the values that are shown when hovering over a variable?

Why do you write your templates as functions? I do it like this:

#let template(text-color: black, title: "", body) = [
  #set text(fill: text-color)
  #text(weight: "bold", size: 20pt)[title]
  #body
]

#show: template.with(
  text-color: blue.darken(50%),
  title: "hello my friends"
)

Hi this is my template

Edit:
Replaced the code with a working version.

It comes down to the same thing, I do this too, because I prefer it that way, saves the unnecessary .with and looks a little cleaner IMO.

1 Like

exactly to avoid the with :slight_smile: it’s a bit more effort (time and mental load) for the template author, but saves a bit for the template consumer. And hopefully the template will be used a bunch, so prioritizing that hopefully pays off :slight_smile:

1 Like

I prefer the plain version that requires a with: I’m afraid that hiding it will obscure things for new users and push them in a “I don’t know what this is but it magically works” state of mind instead of learning the basics of the scripting language.

But there’s probably no right answer, it’s all a bit subjective…

I had the "I don’t know how it works, guess I’ll just write the with " moment :laughing: .
To be honest, the documentation and the tutorials are not quite there yet where I am able to just search for things and have them easily explained. Especially on google the info is kind of hard to find.

3 Likes

Just mentioning this as I didn’t see it mentioned anywhere below. it is just a variable name. You can use any name that you want. It doesn’t have to be it.

3 Likes