I am trying to create a grid of math symbols (to put labels over the top of a sparse matrix), but I am getting some strange alignment issues with the math symbols. Consider the following 3 different code blocks:
// Nicely aligned, but too close together
#grid(
columns: 3,
$delta$,$Delta$,$w_0$
)
//More space between them, but misaligned math characters
#grid(
columns: (.3cm,.3cm,.3cm),
$delta$,$Delta$,$w_0$
)
//misaligned math characters, but in a different way
#grid(
columns: (.4cm,.4cm,.4cm),
$delta$,$Delta$,$w_0$
)
//aligned, but with too much spacing between them
#grid(
columns: (.5cm,.5cm,.5cm),
$delta$,$Delta$,$w_0$
)
I’d really like to do something where I can set the spacing of each column, but then I get the weird vertical alignment of the different math symbols. Any idea how I can set the spacing of the columns without having alignment issues?
Welcome to the forum. 
The answer is already in your question.
You should set the space between the columns instead of setting the width of the columns.
This could be a possible solution
#grid(
columns: 3,
column-gutter: 0.5em,
$delta$,$Delta$,$w_0$
)
3 Likes
Since you only want to align horizontally, you could use something other than grid
. Here are two alternative solutions:
-
Using h
for horizontal alignment:
$delta #h(0.3em) Delta #h(0.3em) w_0$
or even declaring a reusable horizontal space:
#let space = h(0.3em)
$delta space Delta space w_0$
-
Using stack
:
#stack(
dir: ltr,
spacing: 0.3em,
$delta$,
$Delta$,
$w_0$,
)
1 Like
The problem with this solution (which is elegant, BTW), is that if the symbols themselves are different widths, then you end up with non-aligned values. I really need the symbols to be regularly spaced, which is why I am using the grid. For the example I gave, that is a good step.
I’ve gotten a couple of good replies (thank you @Mathemensch and @sermuns), but I realize I may not have specified my problem well enough. What I am trying to do is create the labels for a matrix. Maybe there is a better way to do this in general, but I have a sparse matrix where I am trying to show where the non-zero entries are. The grid problem I described here is how to create the labels for that matrix. (Picture below). Because I am putting labels on something which is regular, I have to have labels that are regular independent of the size of the labels themselves. Both solutions proposed before puts space between labels, but does not make the placement of the labels themselves regular.
In LaTeX, I use the blkarray
package, which, BTW, also helps me put brackets around the matrix itself. Maybe there is a similar package in typst, but I have not found it.
I’m pretty sure that the problem in your original examples is that the column widths chosen are too narrow for the symbols, causing the line to wrap:
#grid(
columns: (.3cm,.3cm,.3cm),
stroke: 0.5pt + red
$delta$,$Delta$,$w_0$
)
#context [
$Delta$ is #measure($Delta$).width.cm() cm wide\
$w_0$ is #measure($w_0$).width.cm() cm wide
]
So you would need to either choose wider columns, or make the equations overflow their boxes, e.g.:
#grid(
columns: (.3cm,.3cm,.3cm),
$delta$, box(width: 4cm)[$Delta$],box(width: 4cm)[$w_0$]
)

2 Likes
(your problem seems related to How can I label the columns and rows of a matrix? or How to input an excess matrix, where elements "overflow" the parentheses?, maybe one of those questions has a helpful answer for you)
(You may also be interested in Build Table, Measure, then Build it Again, which shows how to create the entire matrix (including contents, labels, and brackets with tables)
2 Likes