How can I write a function to format variables in math?

I am trying to generalize the following process using global functions.

I try to match the math output in typst to that in the problem sets I am proving. Since all Matrices are uppercase and bold, all vectors are bold and all vector spaces are script I find myself writing a list of repeating code for every proof. I also do not want to add this to a global show rule, since a symbol in one proof might be a vector another and in the next just a scalar.

#proof()[
  #show math.equation: eq => {
    show "A": math.upright(math.bold($A$))
    show "x": math.bold($x$)
    show "b": math.bold($b$)
    show "v": math.bold($v$)
    show "L": $scr(L)$
    eq
  }

Can I write global function which works on an array of strings and automatically sets a specific show rule? I would call the function within a proof.

e.g. #matrix((“A”, “X”, “Y”)) or #vector((“x”, “y”, “z”))

Hi @Micah !

Yes you could write a function

#let proof-preset(body) = {
  show math.equation: eq => {
    show "A": math.upright(math.bold($A$))
    show "x": math.bold($x$)
    show "b": math.bold($b$)
    show "v": math.bold($v$)
    show "L": $scr(L)$
    eq
  }
  body
}

and call it within the proof like so:

#proof[
  #show: proof-preset
  ...

]

Or are you looking for generalization for the conversion of the math characters?

Like so?


#let all-scr(strings: (), body) = {
  show math.equation: body => strings.fold(body, (it, string) => {
    show string: math.scr
    it
  })
  body
}

#show: all-scr.with(strings: ("a", "X")) // only a and X, not b

$"a" "b" "X"$

Note that you do not need the quotes, you can just write $a$ and $X$.

Yes, that is what I was looking for. Although I do not understand how that code gets restricted to only changing the letters wrapped in $$.

Oh, right I forgot to restrict the show rule to equations. I edited the solution to include this!

Edit: actually it also works without restricting the show rule to equations. It seems that rules like

#show "a": math.scr

have no effect for non-math content.

Does it matter if it’s not restricted? math variant fonts don’t have any effect in markup mode (this could be a bug but I didn’t find an open issue)

edit: see edit above :smiley:

1 Like

Exactly, it worked, which is why I was confused. Good to know though, that that is the case. Thanks for the help!

1 Like