Coloring columns, rows, cells and lines
This guide explains how to use colors in tblr
tables. You will learn how to color columns, rows,
individual cells and horizontal and vertical lines. For users familiar with the \columncolor
,
\rowcolor
and cellcolor
commands, analogous tabularray
commands will be provided.
Let's take a look at a ugly but colorfull example.
On the left is the solution using the tabular
environment with commands defined by
\usepackage[table]{xcolor}
, while the right side shows the same table using the tblr
environment.
Excerpt for some extra vertical spacing, the tables look identical. In the code below, you can
also see that the code is very similar. \rowcolor
and \cellcolor
are replaced by
\SetRow
and \SetCell
respectively. Note that \SetRow
is not needed in row 1, because the
color is set in the \SetCell
command.
Coloring columns is done with \columncolor
in the
xcolor
syntax. A user can use >
in the column definition or define a new column type, e.q.
the a
column in the example. With tabularray
, you can specify a color using the primitive
Q
column type. Of course, you can also define a new column type with \NewColumnType
, see
column A
for an example. Note The Q
column type supports usefull keys, e.g. vertical
alignment, font color and many more. See the table "Keys for Columns" in the documentation.
The appearance of horizontal and vertical lines in tabular
environment can be customized by
commands such as \arrayrulewidth
and \arrayrulecolor
. tabularray
provides see keys hlines
and vlines
for setting horizontal and vertical lines respectively. Note that you don't need the
\hline
commands in the table content.
%%% Code for tabular
%\usepackage[table]{xcolor}
\newcolumntype{a}{ >{\columncolor{yellow!50}} l }
\setlength{\arrayrulewidth}{2pt}
\arrayrulecolor{gray}
\begin{tabular}{|a|>{\columncolor{red!50}}r|r|}
\hline
\rowcolor{green} \multicolumn{3}{|c|}{{\LARGE \textcolor{red}{StackExchange Sites}}} \\ \hline
\rowcolor{blue!50} Site & questions & answers \\ \hline
Stack Overflow & 22m & 33m \\ \hline
Mathematics & 1.5m & \cellcolor{magenta} 2m \\ \hline
Super User & 472k & 684k \\ \hline
\rowcolor{orange!50} TeX - LaTeX & 228k & 293k \\ \hline
\end{tabular
%%% Code for tblr
%\usepackage{tabularray}
\NewColumnType{A}{Q[l, yellow!50]}
\begin{tblr}{colspec={AQ[r,red!50]r}, hlines={2pt, gray}, vlines={2pt, gray}}
\SetCell[c=3]{c, green, fg=red, font=\LARGE} StackExchange Sites && \\
\SetRow{blue!50} Site & questions & answers \\
Stack Overflow & 22m & 33m \\
Mathematics & 1.5m & \SetCell{magenta} 2m \\
Super User & 472k & 684k \\
\SetRow{orange!50} TeX - LaTeX & 228k & 293k \\
\end{tblr}
Remark Small lines are sometimes not correctly rendered, when using the xcolor
commands.
Additionaly, \cline
commands don't work. Both of these issues are not present when using the
tabularray
syntax.
Using the new keyval interface of tabularray
tabularray
also has a new keyval interface allowing you to customize the format of the table in the
mandatory argument of \begin{tblr}
. Using this new syntax, the table can be created with
\begin{tblr}{
colspec={lrr}, hlines={2pt, gray}, vlines={2pt, gray},
column{1} = {yellow!50}, column{2} = {red!50},
row{2} = {blue!50}, row{Z} = {orange!50},
cell{1}{1} = {c=3}{c, green, fg=red, font=\LARGE},
cell{4}{3} = {magenta},
}
StackExchange Sites && \\
Site & questions & answers \\
Stack Overflow & 22m & 33m \\
Mathematics & 1.5m & 2m \\
Super User & 472k & 684k \\
TeX - LaTeX & 228k & 293k \\
\end{tblr}
You can specify columns and rows using the column
and row
keys respectively. The
\SetCell[<span>{<styles>}
at cell in row i
and j
is replaced by the option
cell{i}{j}={<span>}{<styles>}
. For example, cell{1}{1} = {c=3}{c, green, fg=red, font=\LARGE}
sets the cell in the first row and column to span 3 columns {c=3}
, centered c
, with green
background color, red
font color and LARGE
font. For a full list of options, have a look at the
tables "Keys for the Content of Cells", "Keys for Rows" and "Keys for Columns" in the documentation.
You can mix both interfaces in your tblr
environment as you like. For example, you can specify the
columns
and rows
keys and use the \SetCell
for customizing individual cells.
Color alternating rows
Another often wanted style is to color alternating rows with different colors. With
\usepackage[table]{xcolor}
you can use the command \rowcolors{<start-row>}{<odd-row-color>}{<even-row-color>}
for this. tabularray
provides the selectors odd
and even
, which can be used in the row
and
column
keys. As of now, specifying odd
and even
from a specified starting index is not supported.
The feature will be added soon (see here). In the
meantime you have to override the cells which should not be colored, as in the example below.
\begin{tblr}{
colspec={lrr}, hlines,
row{even} = {blue!20},
row{odd} = {red!20},
row{1} = {white, font=\large\bfseries},
}
Site & questions & answers \\
Stack Overflow & 22m & 33m \\
Mathematics & 1.5m & 2m \\
Super User & 472k & 684k \\
TeX - LaTeX & 228k & 293k \\
\end{tblr}
Advanced examples
The odd
and even
selectors can also produce more complex patterns, e.g. a
chesboard.
Very advanced users can also define custom selectors with \NewChildSelector
(see an
example), but that is beyond the scope of this guide.
That concludes the short introduction for using colors with the tabularray
package.
If you think there is something wrong or missing, feel free to improve
this answer.