Grid with constant content in columns

I’m looking for a way to simplify this:

#grid(
  columns: 3,
  gutter: 5pt,
  [A], $->$, [B],
  [C], $->$, [D],
)

(i.e., the middle column is → for all rows.)
In LaTeX, you can specify arbitrary content between columns when defining array or tabular. What’s the Typst equivalent?

#let const = $->$
#grid(
  columns: 3,
  gutter: 5pt,
  [A], const, [B],
  [C], const, [D],
)

#grid(
  columns: 3,
  gutter: 5pt,
  ..(
    [A], [B],
    [C], [D],
    [E], [F],
  ).chunks(2).map(row => row.intersperse($->$)).flatten()
)

#let with-const(..cells) = {
  cells.pos().chunks(2).map(row => row.intersperse($->$)).flatten()
}
#grid(
  columns: 3,
  gutter: 5pt,
  ..with-const(
    [A], [B],
    [C], [D],
    [E], [F],
  )
)

I don’t know what you’re talking about.


1 Like
#show grid.cell.where(x: 1): [$->$]
#grid(
  columns: 3,
  gutter: 5pt,

  [A], [], [B], 
  [C], [], [D],
)

This is how I would do it.

1 Like

In LaTeX, it would be this:

\begin{tabular}{l@{$\to$}l}
A & B\\
C & D
\end{tabular}

(left-align column, followed by $\to$, followed by left-aligned column)

Maybe something like this would be closer to what I’m looking for:

#show grid.cell.where(x: 1): it => [$->$ #it]
#grid(
  columns: 2,
  gutter: 5pt,

  [A], [B], 
  [C], [D],
)

I still don’t know if it treats the middle part as a separate cell or not. Not clear what spacing would be used. It’s all depends on how hacky you want it to be/what kind of API, and how the spacing and other styling should be applied. I showed the non-hacky ways. You can go as far as recreate the grid with 3 columns from a grid with 2 columns with a show rule. Or imitate a 3rd column like in Grid with constant content in columns - #5 by CharpoV. Though then you must set the column-gutter equal to the width of a whitespace to make it symmetrical.

Here’s another way to do it, to add to the collection:

#grid(
  columns: 3,
  gutter: 5pt,
  ..2 * (grid.cell(x: 1)[$->$], ), 
  [A], [B], 
  [C], [D],
)

How it works: We can insert grid cells at fixed coordinates. We insert 2 grid cells at x: 1 (second column). We need to specify the number of rows (2).

4 Likes

Wow, this is a very clever way.
I did not know that you can specify the cell position like that. Thanks for sharing!

1 Like