How can I create graphical elements in a loop?

I try to create n little colored squares aligned in a horizontal line. I seem to fail in every way that I try. Adding squares to a list seems impossible. My latest attempt was to map from a range to squares:

#let n = 5
#range(n).map(ang => [#square(fill: color.hsl(ang * 1deg, 100%, 50%))])

I get the following output (sorry, I cannot copy the code).
Screenshot 2025-05-07 171947
the squares are not rendered somehow. What am I doing wrong here?

I want to align the squares inside a math function (in color though)

$ f( #sym.qed #sym.qed ... ) $

The function .map() always returns an array, see Array Type – Typst Documentation. If you want to turn the array into content, you could for example use the function .join() or pass the array elements to the function stack(). There might be even more options to actually show the squares, but this should be enough to get you started.

#let n = 5
#let squares = range(n).map(ang => [#square(fill: color.hsl(ang * 1deg, 100%, 50%))])
#squares.join()
#stack(dir: ltr, spacing: 1em, ..squares)

Note that you could also change the color of the symbol sym.qed if you don’t want to draw arbitrary shapes. If all symbols should have the same color, you can use a show-set rule

#show sym.qed: set text(red)
$ f( #sym.qed #sym.qed ... ) $

If you want to color the qed symbols individually, you can define a function that returns the symbol in a specific color

#let c-qed(color) = text(color, sym.qed)
$ f( #c-qed(blue) #c-qed(red) ... ) $
2 Likes

thank you very much!
That is really helpful!