How to "spread" a 2d array in table?

  • What does flatten do?
    flatten() converts a multi-dimensional array into a one-dimensional array. For example, in your case, txn is a 2D array (which means elements in array are 1D array) like:

    (
        (
            "Date",
            "Tran Type",
            "Tran ID",
            "Withdrawals",
            "Deposits"
        ),
        (
            "01/05/2024",
            "TFR",
            "S81420904",
            "700.00",
            ""
        ),
        (
            "01/05/2024",
            "TFR",
            "S84554541",
            "",
            "1.01"
        )
    )
    

    and txn.flatten() returns a 1D array like:

    (
        "Date",
        "Tran Type",
        "Tran ID",
        "Withdrawals",
        "Deposits",
        "01/05/2024",
        "TFR",
        "S81420904",
        "700.00",
        "",
        "01/05/2024",
        "TFR",
        "S84554541",
        "",
        "1.01",
    )
    
  • What should we provide to table ?
    The table does not treat cells from different columns differently, so you don’t need to use other functions (like your to_content) to construct a complete row. Instead, we should provide the table with individual cell content.

    Simply using ..array.flatten() works because the data you provide comes in groups of five, and the table is set to have five columns, so each group of data perfectly fills one row.

  • About .. operator
    Basically, you can think of the .. operator as removing the brackets from around the list. For example:

    #let txn = (
      (
        "Date",
        "Tran Type",
      ),
      (
        "01/05/2024",
        "TFR",
      ),
    )
    #table(columns: 2, ..txn)
    

    is equivalent to (simply replacing the variable):

    #table(columns: 2, ..(
      (
        "Date",
        "Tran Type",
      ),
      (
        "01/05/2024",
        "TFR",
      ),
    ))
    

    is equivalent to (spread array with ..)

    #table(
      columns: 2,
      (
        "Date",
        "Tran Type",
      ),
      (
        "01/05/2024",
        "TFR",
      ),
    )
    

    This won’t work because table does not accept array as row, (it receives str or content as individual cell, as we mentioned above), but with ..txn.flatten(), we will get:

    #table(
      columns: 2,
      "Date", "Tran Type",
      "01/05/2024", "TFR",
    )
    

    That’s exactly what we want.

2 Likes