How to dispatch the correct function based on positional argument

I am trying to have two types of raw blocks, one with a gray background and one with a white background and no inset, spacing. This is what I have

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

// I want this to have white background, no inset or spacing
#show raw.where(block: true, lang: "mylang"): block.with(fill: rgb("#ffffff"), inset: 0em, radius: 0em, width: 100%)

But when I do

Test code 

it still seems to have the gray background. Interestingly enough, the inset actually is set to zero in this block so I am not sure what is happening.

Hopefully someone will be able to explain why the show rules don’t override.

In the meantime, here’s a possible solution:

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

#show raw.where(block: true, lang: "mylang"): set block(
  fill: rgb("#ffffff"),
  inset: 0em,
  radius: 0em,
)

#raw(block: true, "Without mylang")

#raw(block: true, lang: "mylang", "With mylang")
Output


Interestingly enough, the inset actually is set to zero in this block so I am not sure what is happening.

I couldn’t replicate this on my end, everything appeared the same without mylang.


A similar example can be found in the current docs, however that show rule is only being applied once. I presume you saw that example, but unfortunately encountered this peculiarity afterward.

1 Like

Hi @kfold3,

the way you wrote the show rules (without the set keyword), the raw element gets wrapped for each “show …” inside a new block element. Your code essentially produces:

#block(
  fill: rgb("#ffffff"), 
  inset: 0em, 
  radius: 0em,
  width: 100%,
  block(
    fill: luma(240), 
    inset: 1em, 
    radius: 0.5em, 
    width: 100%,
    [
      ```mylang
      Test code 
      ```
    ]
  )
)

@hpcfzl used show-set rules which modify the built-in block of the raw element and therefore overwrite the existing settings of it.

2 Likes

Would this work for you? Now we are not creating new blocks, and we are conditionally changing the style of container block based on lang.

#show raw.where(block: true): it => {
  set block(
    fill: luma(240),
    inset: 1em,
    radius: 0.5em,
    width: 100%,
  )
  set block(
    fill: rgb("#ffffff"),
    inset: 0em,
    radius: 0em,
  ) if it.lang == "mylang"
  it
}

```mylang
Test code
```

```rust
Test code
```