Custom "macros" with math mode parameters in math mode

I am having hard time finding how to do In Typst what in LaTeX can be done with \newcommand with parameters. Are there examples online of custom shorthand notations with parameters that work in math and text mode? Did I miss a documentation section about this?

Say, I want to use a “macro” with a parameter to simplify

$class("opening", "]") 0, infinity class("closing", "[")$

So, I try this definition:

#let intervaloo(it) = $class("opening", "]")
it
class("closing", "[")$

Then I write:

$intervaloo(0, infinity)$

Of course this does not work, because Typst takes 0, infinity as two arguments.

What is the most straightforward way to define custom shorthand notation with substitution of parameters?

There are two ways I can think of. One is to escape the commas,

$intervaloo(0\, infinity)$

but personally I find this a bit annoying to write. Another way is to use an argument sink and add the commas back:

#let intervaloo(..it) = $class("opening", "]")
#it.pos().join($,$.body)
class("closing", "[")$


$intervaloo(0, infinity)$

(If you’re wondering why I used $,$.body instead of "," it is to get the commas to use the math font & spacing)

Is there no way to group arguments (as `{…}` in LaTeX) or use some kind of a block to make clear I am passing a single argument?

If you want to only pass in a single argument, I suppose you can pass in an equation:

$intervaloo( #$0, infinity$ )$

But to be clear, arguments are separated by commas in typst (unlike wrapping each of them in {...} as in LaTeX). If you want to avoid unnecessary overhead when calling your function, I’d suggest using the argument sink as shown above.

Thanks, I did not know about #$ syntax. Where in the documentation can I read about it?

I do not want to use the argument sink because this is logically wrong, because I only want to pass a single argument.

I don’t know if the #$...$ is explicitly mentioned in the documentation, but the hash prefix can be used in non-code mode to add expressions, which in addition to functions can also be content blocks such as #[Hello world!] or equations #$x^2$ (see also the “blocks” section in the link above).

Well, sure. If you really feel this way, semantically the correct choice would be to escape the commas (or define a variable to use instead of commas #let sep = $,$.body), since to me it makes even less sense to have equations inside of equations. But, if you’re happy with this then okay.

You can simply use the built-in symbol comma:

$intervaloo(0 comma infinity)$