tables – How to Add Captions to Tables Designed by Tabularray

captionstablestabularray

I want to add the caption above the table. There is no information on the package documentationhere. The documentation only explains adding caption to long table which is not the case of mine. Any help appreciated!

enter image description here

This is the code:

\usepackage{tabularray}
\usepackage[table]{xcolor}
\usepackage{graphicx}
\begin{document}

\scalebox{0.8}{

\begin{tblr}{
colspec={cccccccc},
rowspec={Q[gray!20]Q[]Q[gray!10]Q[]Q[gray!10]Q[]Q[gray!10]Q[]}, 
rowhead = 1,
vline{2-8} = {0-1}{0.3pt,gray!50},
vline{2-8} = {2-7}{0.3pt,gray!30},
hline{1,2,8} = {0.1pt,azure5}}
header  & header & header & header & header & header & header & header\\
1  & Beta & Gamma & Alpha & Beta & Gamma & Beta & Gamma\\
2  & Beta & Gamma & Alpha & Beta & Gamma & Beta & Gamma\\
3  & Beta & Gamma & Alpha & Beta & Gamma & Beta & Gamma\\
4  & Beta & Gamma & Alpha & Beta & Gamma & Beta & Gamma\\
5  & Beta & Gamma & Alpha & Beta & Gamma & Beta & Gamma\\
6  & Beta & Gamma & Alpha & Beta & Gamma & Beta & Gamma\\

\end{tblr}

}
\end{document}

Best Answer

I think you are confusing the tblr environment with the table environment. The former is for creating a tabular like structure, while the latter is the floating environment. The caption (and label) have to go in the floating environment.

\documentclass{article}

\usepackage[margin=2.5cm]{geometry}
\usepackage{xcolor}
\usepackage{graphicx}

\usepackage{tabularray}

\begin{document}
    
\begin{table}
    \centering
    \caption{Example caption}
    \label{tab:Example}
    \begin{tblr}{
            colspec={*{8}{c}},
            row{odd}={gray!10}, row{1}={gray!20},
            vline{2-Y} = {1}{0.3pt,gray!50},
            vline{2-Y} = {2-Z}{0.3pt,gray!30},
            hline{1,2,Z} = {0.1pt,azure5}
        }
        header  & header & header & header & header & header & header & header\\
        1  & Beta & Gamma & Alpha & Beta & Gamma & Beta & Gamma\\
        2  & Beta & Gamma & Alpha & Beta & Gamma & Beta & Gamma\\
        3  & Beta & Gamma & Alpha & Beta & Gamma & Beta & Gamma\\
        4  & Beta & Gamma & Alpha & Beta & Gamma & Beta & Gamma\\
        5  & Beta & Gamma & Alpha & Beta & Gamma & Beta & Gamma\\
        6  & Beta & Gamma & Alpha & Beta & Gamma & Beta & Gamma\\   
    \end{tblr}
\end{table}     
            
\end{document}

tblr output

I also made some further improvements to your code

  • table option for xcolor is not needed when using tabularray
  • remove rowhead option, as it is only needed for longtblr
  • avoid using \scalebox as it leads to inconsistent font sizes
  • use *{8}{c} in colspec to repeat the c column 8 times
  • replaced rowspec definition with odd option for row coloring. This is far more flexible, as it doesn't depend on the number of rows
  • simplified vline and hline definition: you can use the letters X, Y and Z to refer to the last three columns (or rows) respectively. This makes your code more readable and is also more flexible should you change the number of columns later