Tables – Creating Advanced Tables with Tabularray

tablestabularray

I've heard that instead of tabular tabularray should be used. And I think it's right because classic table is glitching out when combined with row colors. But I absolutely cannot figure out how to use tabularray.

In the code below I have provided main table created with tabular, and second table created with tabularray, which should be exact copy of the first one. Problem is that the secondary table doesn't look correctly, and doesn't even compile, because for reasons unknown tabularray does not support captions. At least not directly, I'm sure caption can be added, but this is also the thing that I can't do.

\documentclass{article}
\usepackage{graphicx}
\usepackage{xcolor,colortbl}
\usepackage{multirow}
\usepackage{hhline}
\definecolor{rowcol}{rgb}{0.7, 0.7, 0.7}

\usepackage{tabularray}

\title{TEST2}
\author{Anon}
\date{May 2023}

\begin{document}

\maketitle

\section{Introduction}

%main table
\begin{table}[!htbp]
\large
\begin{center}
\begin{tabular}{ |>{\centering\arraybackslash}m{2cm}||c|c|c| } 
\hline
\multirow{2}{*}{} & \multicolumn{3}{c|}{Description} \\
\hhline{~---}
                  & 1 & 2 & 3 \\
\hline
\hline
\rowcolor{rowcol}
A & 11 & 11 & 11 \\
\hline
B & 12 & 11 & 12 \\
\hline
\rowcolor{rowcol}
C & 12 & 12 & 12 \\
\hline
\end{tabular}
\end{center}
\caption{Caption} \label{tablelabel}
\end{table}

%table above remake
\begin{table}
\begin{tblr}{
  hlines, vlines,
  colspec={X[l,m]|X[c,m] X[c,m] X[c,m]},
  column{1}={font=\bfseries},
  cell{1}{1,1} = {c=3}{c},
  row{2}={bg=rowcol},
  row{4}={bg=rowcol},
  row{6}={bg=rowcol}
}
  & Description \\
  & 1 & 2 & 3 \\
  A & 10 & 20 & 30 \\
  B & 15 & 25 & 35 \\
  C & 12 & 22 & 32 \\
\end{tblr}
\caption{Table 2}
\label{tablelabel2}
\end{table}

\end{document}

Best Answer

I cannot reproduce the problem with the caption you have and actually it should not matter whether you place a tabular or a tabularray inside a table together with a \caption. Anyways, this should be a proper remake of the original table (but with nicer gaps where the doubled cell borders meet):

\documentclass{article}
\usepackage{xcolor}
\usepackage{tabularray}
\definecolor{rowcol}{rgb}{0.7, 0.7, 0.7}

\begin{document}

\begin{table}
\centering
\begin{tblr}{
  colspec={ Q[c,m,wd=2cm] Q[c,m] Q[c,m] Q[c,m] },
  cells={font=\large},
  cell{1}{1}={r=2}{},
  cell{1}{2}={c=3}{},
  row{odd[3]}={bg=rowcol},
  hline{1-Z}={1}{-}{leftpos=0, rightpos=0},
  hline{3}={2}{-}{leftpos=0, rightpos=0},
  vlines,
  vline{2}={2}{-}{},
}
  & Description \\
  & 1 & 2 & 3 \\
  A & 10 & 20 & 30 \\
  B & 15 & 25 & 35 \\
  C & 12 & 22 & 32 \\
\end{tblr}
\caption{Caption} \label{tablelabel2}
\end{table}

\end{document}

enter image description here

Note that I replaced hlines by hline{1-Z}={1}{-}{leftpos=0, rightpos=0} to achieve the nicer gaps. Also, using row{odd[3]}={bg=rowcol}, you can set the background for all odd rows starting from row 3 in one go. And as suggested by Zarko, it is probably better to use \centering instead of a nested center environment.