How to fill entire columns of a table?

#table(
  columns: (5%,30%,5%,30%,30%),
  table.header(
    [], [*Column A*], [],[*Column B*],[*Answer* \
    (Write the alphabet of Column B)]
  ),
  [1],[Hicks],[A],[Samuelson],[],
  [2],[Marshall],[B],[Edgeworth],[],
  [3],[Jorda],[C],[Friedman],[],
  [4],[Mankiw],[D],[Hicks],[],
  [5],[Samuelson],[E],[Marshall],[],
  [6],[Samuelson],[E],[Marshall],[],
  [7],[Samuelson],[E],[Marshall],[],
  [8],[Samuelson],[E],[Marshall],[],
  [9],[Samuelson],[E],[Marshall],[],
  [10],[Samuelson],[E],[Marshall],[],
)

I want to automatically fill the numbers and alphabet columns

Hi there,

In order to help us help you, could you please mark your code appropriately for ease of reading and copy/pasting.

Thanks for your cooperation :wink:

Reason: your code does not compile.

#table(
  columns: (5%,30%,5%,30%,30%),
  
  table.header(
    [], [*Column A*], [],[*Column B*],[*Answer* \
    (Write the alphabet of Column B)]
  ),
  [1],[Hicks],[A],[Samuelson],[#h(0.8em)1--],
  [2],[Marshall],[B],[Edgeworth],[#h(0.8em)2--],
  [3],[Jorda],[C],[Friedman],[#h(0.8em)3--],
  [4],[Mankiw],[D],[Hicks],[#h(0.8em)4--],
  [5],[Samuelson],[E],[Marshall],[#h(0.8em)5--],
  [6],[Samuelson],[E],[Marshall],[#h(0.8em)6--],
  [7],[Samuelson],[E],[Marshall],[#h(0.8em)7--],
  [8],[Samuelson],[E],[Marshall],[#h(0.8em)8--],
  [9],[Samuelson],[E],[Marshall],[#h(0.8em)9--],
  [10],[Samuelson],[E],[Marshall],[#h(0.8em)10--],
)

you can always write your own:

#let autotable(
  fills: ((0, "1"), (2, "A")),
  columns: (5%,30%,5%,30%,30%),
  header,
  ..cells
) = {
  let rows = cells.pos().chunks(columns.len() - fills.len())
  let filled-rows = rows.enumerate().map(((y, row)) => {
    for (fill-pos, fill-pattern) in fills {
      row.insert(fill-pos, numbering(fill-pattern, y + 1))
    }
    row
  })
  table(columns: columns, header, ..filled-rows.flatten())
}

then use it like:

#autotable(
  table.header(
    [], [*Column A*], [],[*Column B*],[*Answer* \
    (Write the alphabet of Column B)]
  ),
  [Hicks],[Samuelson],[#h(0.8em)1--],
  [Marshall],[Edgeworth],[#h(0.8em)2--],
  [Jorda],[Friedman],[#h(0.8em)3--],
  [Mankiw],[Hicks],[#h(0.8em)4--],
  [Samuelson],[Marshall],[#h(0.8em)5--],
  [Samuelson],[Marshall],[#h(0.8em)6--],
  [Samuelson],[Marshall],[#h(0.8em)7--],
  [Samuelson],[Marshall],[#h(0.8em)8--],
  [Samuelson],[Marshall],[#h(0.8em)9--],
  [Samuelson],[Marshall],[#h(0.8em)10--],
)

(it’s not very robust of course)

1 Like

What @cAtte basically suggests is what I described here for another use case, but with some general advice too:

In this case, tip 1. (“you need to store the data somewhere before creating the table”) is the important one; tip 2. not so much in this particular instance (“you need to make sure that you operate on numbers and not content”). The reason is that you don’t want to edit existing data, but insert automatically generated data alongside the existing one.

Thats great. One more help, how to increase row width?

Use the rows parameter of table (row-gutter can also be of some help). Table Function – Typst Documentation.

#table(
  columns: 2,
  rows: (auto,3em,2cm),
  ..range(6).map(str)
)