How to I create a table with vertical non-breaking header text?

Hi there,

I am trying to create a table where each of the header cells has its text rotated -90 degress without line breaks. I have gotten this far by hardcoding the cell height:

#let header = ("Name", "Maths", "Physics", "French", "Computer Science")
#let body = (
  "Tomás", "A+", "A", "B-", "A++",
  "Alice", "B", "B+", "A", "A",
  "Bob", "C+", "B-", "C", "A-",
)

#table(
  columns: header.len(),
  rows: (5cm, auto),
  align: center,

  table.header(
    ..header.map(h =>
      place(bottom + left, rotate(-90deg, strong(h)))
    )
  ),

  ..body.flatten(),
)

Note that my goal is to be able to read a CSV which has particularly long headers, and the cell contents are all single or double digit numbers, hence the reason for the automatic single-liner vertical header text.
The output of my snippet of code breaks “computer science” into two lines, and also has poor alignment on the first column:

I tried wrapping the text in a box() because I thought it would prevent the lines from breaking but it didn’t change the result.

I also found this very interesting post about headings at 45deg angles, but that technique didn’t prevent the lines from breaking.

Is there a way to accomplish the desired effect?

Welcome @Tomas_Badenes,

I used #show rotate: set block(fill: red) for a quick visualization of your issue:

As you can see from the places marked in red, the bounding boxes are wrong. The bounding boxes are from the content before the rotation. You need to add reflow: true to your rotation call to get the correct layout.

//debug
#show rotate: set block(fill: red)

#let header = ("Name", "Maths", "Physics", "French", "Computer Science")
#let body = (
  "Tomás", "A+", "A", "B-", "A++",
  "Alice", "B", "B+", "A", "A",
  "Bob", "C+", "B-", "C", "A-",
)

#table(
  columns: header.len(),
  rows: 2,
  align: center+bottom,

  table.header(
    ..header.map(h =>
      rotate(-90deg, reflow: true, strong(h))
    )
  ),

  ..body.flatten(),
)

With this you now also don’t have to hardcode the cell height or use place.

1 Like

That did the trick!
Thanks flokl