Tables Vertical Alignment – Fixing Vertical Alignment in Tabularray when r=1 Not Working

tablestabularrayvertical alignment

I'm using tabularray to create a table cause it was supposed to be simple to configure the alignment of the cells. Especially, I want to use the vertical alignment to center the text in specific cells. It works for multirow cells (e.g., \SetCell[r=2]{m}), but not when r=1. Example:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularray}
\usepackage{lipsum}  

\begin{document}
\begin{table}[]
\footnotesize
\centering
\caption{Test table}
\begin{tblr}{X[1,l]X[2,l]}
\hline
& \SetCell[]{c}A\\
\hline
\SetCell[r=2]{m} XX & \lipsum[1-1] \\
&  \lipsum[2-2] \\
\hline
\SetCell[r=1]{m} YY & \lipsum[3-3]\\
\hline
\end{tblr}
\label{tab:test}
\end{table}
\end{document}

Generates

enter image description here

So clearly "XX" is being centralized (across two rows), but "YY" is not (in a single row). Omitting r=1 changes nothing (it is the default value).
How do I centralize the single-row, single-column "YY" text vertically using tabularray?

Best Answer

Vertical alignment in LaTeX tabular/tblr cells does not work exactly how people think it does. What "m" does is align the middle of the cell contents with the baseline of that row. "t" (or "p" without tabularray) which is default aligns the top line of the content with the baseline, and "b" aligns the bottom line with the baseline of that row.

What's happening here is that, in the bottom row, the first cell is "m", but the second cell is "t", so you're aligning the middle of "YY" with the top of the paragraph in the cell on the right. Hopefully that explains why you're getting the result you don't want.

In this case, the solution is to add the "m" designation to the cell on the right, so that its middle is at the baseline and "YY" is lined up with its middle. (The setting for the cell on the left doesn't really matter if it's only one line.)

\begin{document}
\begin{table}[]
\footnotesize
\centering
\caption{Test table}
\begin{tblr}{X[1,l]X[2,l]}
\hline
& \SetCell[]{c}A\\
\hline
\SetCell[r=2]{m} XX & \lipsum[1-1] \\
&  \lipsum[2-2] \\
\hline
\SetCell[r=1]{m} YY & \SetCell[r=1]{m}\lipsum[3-3]\\
\hline
\end{tblr}
\label{tab:test}
\end{table}
\end{document}

Or it might be easier to do it in the colspec:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularray}
\usepackage{lipsum}  

\begin{document}
\begin{table}[]
\footnotesize
\centering
\caption{Test table}
\begin{tblr}{X[1,l,m]X[2,l,m]}
\hline
& \SetCell[]{c}A\\
\hline
\SetCell[r=2]{m} XX & \lipsum[1-1] \\
&  \lipsum[2-2] \\
\hline
YY & \lipsum[3-3]\\
\hline
\end{tblr}
\label{tab:test}
\end{table}
\end{document}

tblr example

Because of this oddity of how vertical alignment works, sometimes more drastic measures are necessary: see here. But in this particular case, I think this is enough.