How can I use a show function inside a loop?

I’m trying to make an environment where there are a number of customizable shorthands via an array or argument sink, the code explains it better:

#let characters = ("Sam", "Dave", "Adam")

#let log(body) = {
  for c in characters {
    show c + ":": it => [#it...]
  }
  block()[
    #body
  ]
}

#log()[
  Sam:
]

I can see why this wouldn’t work since the show rules are on the wrong level, but how could I bring them up one level?

Is there something more complex that you are trying to achieve? If the example given is the end goal then you could use a regular expression as your show rule. With longer lists it will become slower and slower.

#let characters = ("Sam", "Dave", "Adam")
#let rePattern = "("+characters.map(c => "\b"+c).join("|")+"):"//(\bSam|\bDave|\bAdam):

#show regex(rePattern): it => {
  show ":": [...]
  it
}

Alice:

Bob:

Clarice:

Dave:
Output

image

The regular expression pattern here ends up being (\bSam|\bDave|\bAdam):. The ‘\b’ makes sure that the character before the start of each name is not a character (typically a space). It’s optional but without it you could match too many things.

And if you wanted it as the log function:

Code
#let characters = ("Sam", "Dave", "Adam")
#let rePattern = "("+characters.map(c => "\b"+c).join("|")+"):"//(\bSam|\bDave|\bAdam):

#let log(body) = {
  show regex(rePattern): it => {
    show ":": [...]
    it
  }
  body
}

See here:

2 Likes

thanks that’s exactly it, sorry for not reading more closely!