1

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?

2
  • Welcome to TeX.SE! One row multirow cell has not sense. In such cell is suffucinet (and more correct) to write \SetCell{m} or even better, this column specify (in your case) as X[1,l,m].
    – Zarko
    Jun 13, 2022 at 18:19
  • Thanks for welcoming @Zarko, and thanks for your comment! I did try your suggestions (in overleaf compiler pdfLaTeX, version 2021) but it didn't work. frabjous answer did the trick, though!
    – exepe
    Jun 13, 2022 at 18:57

2 Answers 2

3

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.

5
  • Thank you very much, this solved my problem. In time, do you have any pointers for the definition of baseline? Combining sentences 1)_What "m" does is align the middle of the cell contents with the baseline of that row_ and 2) so you're aligning the middle of "YY" with the top of the paragraph in the cell on the right it sounds like, somehow the top of paragraph in the cell on the right is the baseline, which sounds a bit arbitrary. Again, thank you.
    – exepe
    Jun 13, 2022 at 19:06
  • By the way, just for completeness, \SetCell{h} (to place in the upper part) and \SetCell{f} (to place in the lower part) seem to be independent of this baseline you said. It might be useful if there is more than one column, and you do not want all of them to be set as \SetCell{m} just to force the baseline to be centered.
    – exepe
    Jun 13, 2022 at 19:11
  • Internally, multiline cells are treated as \parboxes. Think of \parboxes outside of a table. If they have multiple lines they can line up with the text around them in different ways. Typeset this without any tabular/tblr: Regular text. \parbox[t]{1em}{t\\t\\t} \parbox[m]{1em}{m\\m\\m} \parbox[b]{1em}{b\\b\\b} Back to baseline. See the three ways they line up with the text surrounding them. You can think of each table row as having its own baseline where c, l and r cells line up, and the multiline cells are parboxes "jutting" away from it in different ways. Not sure if that helps.
    – frabjous
    Jun 13, 2022 at 19:29
  • I'm not that familiar with tabularray's h and f but yes it appears they don't line up any line with the baseline, but rather align the top of the contents to the topmost part of anything else in the row, or the bottom with the bottommost.
    – frabjous
    Jun 13, 2022 at 19:32
  • Awesome example! That was very helpful!
    – exepe
    Jun 13, 2022 at 20:16
0

Let me expanded my comment to an answer: beside set that content of the first column to be vertical centered, you also need to set vertical positions of other columns too. At this is not need to be vertical centered, they also can be moved to the top (option h). That is an advantage of tabularray package over others:

\documentclass{article}
\usepackage{tabularray}
\usepackage{lipsum}

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .