How to center tables in rectangles?

Hi!

I want to create some labels to laminate them after printing.

Atm I’m doing it like this:

#set page(
  paper: "a4",
  margin: (left: 15mm, right: 15mm, top: 15mm, bottom: 15mm),
)

#place(
  top + left,
  dx: 0mm,
  dy: 0mm,
  rect(width: 84mm, height: 42mm, stroke: 2mm)
)
#place(
  top + left,
  dx: 96mm,
  dy: 0mm,
  rect(width: 84mm, height: 42mm, stroke: 2mm)
)
#pad(top: 13mm, left: 20mm)[
  #stack(dir: ltr,
    table(
      align: center,
      stroke: none,
      [#text(size: 18pt, weight: "bold")[Samsung 960GB]],
      [#text(size: 14pt)[SM863a]]
    ),
    48mm,
    table(
      align: center,
      stroke: none,
      [#text(size: 18pt, weight: "bold")[Samsung 128GB]],
      [#text(size: 14pt)[MZ-7WD128...]]
    )
  )
]

I know this is bad and everything else than perfectly centered inside the rectangles. This shows just the first pair of labels. I would fill the rest of the page with more of these.

Is there a way to dangle everything inside a structure that doesn’t require manual positioning of the tables inside the rectangles like it is now?

Thanks in advance!

You can put the text in the body of your rect, with center+horizont alignment (to center horizontally and vertically):

#rect(width: 84mm, height: 42mm, stroke: 2mm, {
  set align(horizon+center)
  text(18pt)[*Samsung 960GB*]
  linebreak()
  text(14pt)[SM863a]
})

You can replace the linebreak() with a v(...) of the desired spacing, or put the whole content in a grid (better than a table when you just want a layout without grid lines):

#rect(width: 84mm, height: 42mm, stroke: 2mm, {
  set align(horizon+center)
  grid(
    gutter: 1em,
    text(18pt)[*Samsung 960GB*],
    text(14pt)[SM863a]
  )
})

or using a stack:

#rect(width: 84mm, height: 42mm, stroke: 2mm, {
  set align(horizon+center)
  stack(
    spacing: 1em,
    text(18pt)[*Samsung 960GB*],
    text(14pt)[SM863a]
  )
})

Just highlighting the existence of https://typst.app/universe/package/etykett/ which can give some ideas.

Thank you guys!

Based on @sijo suggestions, I’m now using

#set page(
  paper: "a4", // 210 × 297 mm
  margin: (left: 15mm, right: 15mm, top: 15mm, bottom: 15mm),
)

#pad(top: 0mm, left: 0mm)[
  #stack(dir: ltr,
    rect(width: 84mm, height: 42mm, stroke: 2mm, {
      set align(horizon+center)
      grid(
        text(28pt)[*Backup 01*],
        v(10mm),
        text(18pt)[Samsung 960GB]
      )
    }),
    12mm,
    rect(width: 84mm, height: 42mm, stroke: 2mm, {
      set align(horizon+center)
      grid(
        text(28pt)[*Backup 02*],
        v(10mm),
        text(18pt)[Seagate Spinpoint 2TB]
      )
    })
  )
]

And I only need to add another pair with a vertical padding of 10mm with the same #pad block, rinse and repeat. For the small stuff I’m doing with it, it’s the perfect solution.

@vmartel08
Thanks for the hint as well! The package itself is a bit overkill for what I’m doing here but I’ll take a look at it anyway ofc^^

Thanks again!

1 Like