7

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}[caption={"sdsdssdfsdfds"}]{
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}
4
  • Your code doesn't compile. How or where is \scalebox defined? Which document class do you employ?
    – Mico
    Commented Feb 24, 2022 at 6:40
  • I edited the code Commented Feb 24, 2022 at 6:41
  • 2
    For a caption you can put it in a table environment. Or use the caption or capt-of package that allows you to add captions with the \captionofcommand. Commented Feb 24, 2022 at 7:12
  • You can use the longtblr for this. I know that it is meant for use with long tables which extend over one page, but it will work in a case like this, where you have a short table. Commented Oct 29, 2023 at 16:34

2 Answers 2

9

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
1
  • 3
    the doco for tabularray could be improved! :)
    – baradhili
    Commented Apr 6, 2022 at 0:18
4

You can replace tblr with talltblr to get the caption.

\documentclass{article}
\usepackage{tabularray}
\usepackage{xcolor} % tabularray doesn't need `table` option
\usepackage{graphicx}
\begin{document}

\scalebox{0.8}{
\begin{talltblr}[
  caption={"sdsdssdfsdfds"}
]{
  colspec={cccccccc},
  rowspec={Q[gray!20]QQ[gray!10]QQ[gray!10]QQ[gray!10]Q}, 
  %rowhead = 1, % only for long tables
  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{talltblr}}

\end{document}
4

You must log in to answer this question.

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