How to properly change background color of a block 'raw' element?

#show raw.where(block: true): block.with(fill: luma(240), inset: 1em, radius: 0.5em, width: 100%)
#show raw.where(block: false): box.with(fill: luma(240), inset: (x: 3pt), outset: (y: 3pt), radius: 2pt)

```rust
fn main() {
    println!("Hello, world!");
}
```

#[
  #show raw.where(block: true): set block(fill: rgb("#ffff00"), inset: 1em, radius: 0.5em, width: 100%)
  ```rust
  fn main() {
      println!("Hello, world!");
  }
  ```
]

Lorem ipsum ```rust fn main()``` dolor sit amet.

I want the block raw elements to have gray background, and this works fine. But then I try to make one of them to have yellow background instead of gray, and this seems to add another block instead of changing the color of the existing one. How to fix it?

Current output:

What I’m trying to achieve:

Hello, your first show rule uses a function block.with, and your second show rule is a show-set. They add up, hence the double background.
If you modify the first show rule to be set block(...), then its fine.

1 Like

Thanks, this solved the issue.

//before:
//#show raw.where(block: true): block.with(fill: luma(240), inset: 1em, radius: 0.5em, width: 100%)

#show raw.where(block: true): set block(fill: luma(240), inset: 1em, radius: 0.5em, width: 100%)


```rust
fn main() {
    println!("Hello, world!");
}
```

#[
  #show raw.where(block: true): set block(fill: rgb("#ffff00"), inset: 1em, radius: 0.5em, width: 100%)
  ```rust
  fn main() {
      println!("Hello, world!");
  }
  ```
]

But the block.with part is from the example in the docs, https://typst.app/docs/reference/text/raw/:

#show raw.where(block: true): block.with(
  fill: luma(240),
  inset: 10pt,
  radius: 4pt,
)

And hence now there are two other questions arised:

  • How exactly .with works? (Sorry, I did’t find the respective page in docs.)
  • Why the example in the docs (which I’ve shown above) uses block.with instead of set block?

Glad it’s working!

You can find the docs for function.with at Function Type – Typst Documentation. If you type “with” in the search box it is the first result.

The reason why the second works is because raw is already a block element. I am unsure why the docs write it as block.with, but it has the same output for sure.

1 Like

f.with(a: 1) takes a function f and returns a new function with argument a already set to 1.

There are several types of rules that do different things, see here.

In particular:

  • #show A: set B(...) is a “show-set” rule, it changes the default B settings for A elements. For example #show raw: set block(fill: luma(240)) sets the default fill for blocks in a raw element.

  • #show A: f is a “show” rule: it transforms elements A by passing them to the given function. For example #show raw: block.with(fill: luma(240)) is the same as #show raw: it => block(fill: luma(240), it). It wraps the raw element in a new block.

Of course sometimes both methods can be used to get the same visual result.

1 Like

@sijo Thanks a lot! Now I understand.

Hi @jsx97, don’t forget to tick one of the responses if you got a satisfying answer. The answer you choose should usually be the response that you found most correct/helpful/comprehensive for the question you asked. Thanks!

1 Like