How can I map an array of math mode characters to text?

What is the correct way to use map to render math mode? For example, I would like to print

1, 2, 3, a, b

to the document.

I have tried the following


#text[#(1, 2, 3, $alpha$, $beta$).map(
    x => x
)]

#(1, 2, 3, $alpha$, $beta$).map(
    x => text[#x]
)

#(1, 2, 3, $alpha$, $beta$).map(
    x => text[$#x$]
)

However, the math blocks are not rendered as text

$#(1, 2, 3, $alpha$, $beta$).intersperse(",").map(x => [#x]).join()$

image

One of the key problems you are having is that you get an array and not a content. Currently, content can be represented by a few (hidden) types: context, sequence, styled. context is something you can look up, but the other two normally only can be discovered when tinkering/hacking. sequence is a sequence of elements (something that can be implicitly converted to content), while styled is when you add some set/show rules to your document.

You can use repr() (and .func()/type()) to look deeper at how things work:

#let list = (1, 2, 3, $alpha$, $beta$)
#repr(list) // `array`

image


#let list = (1, 2, 3, $alpha$, $beta$)
// `array` (commas inserted between all elements)
#repr(list.intersperse(","))

image


#let list = (1, 2, 3, $alpha$, $beta$)
// `array` where all elements of type `content`
// You can't join `content`/`str` with `int`.
#repr(list.intersperse(",").map(x => [#x]))

image


#let list = (1, 2, 3, $alpha$, $beta$)
#repr(list.intersperse(",").map(x => [#x]).join()) // `sequence`

image

I want to make an explicit note that in the future the way content is internally defined can be changed and these sequence and styled can become inaccessible or disappear altogether. But for now a few packages and solutions do depend on them, but these are all considered hacks, since there were no intention that users will use the functions directly.

Edit:

As was correctly pointed out by @Eric the intersperse() here is exessive as array.join() has an optional positional separator argument (that can have any type). This brings the MRE down to:

$#(1, 2, 3, $alpha$, $beta$).map(x => [#x]).join[,]$

No need for the intersperse, as you can do that with join too:

#let list = (1, 2, 3, $alpha$, $beta$)
#list.map(x => [#x]).join[, ]
2 Likes

Completely forgot about that, thank you. I used too much of .join() recently, so for me the optional argument was non-existent.