0

Why does cellcolor not work here?

The code is from answer of my previous question here. I'm trying to color a cell of a table as the code here.

\documentclass[journal]{IEEEtran}
\usepackage{mathtools}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\usepackage[table]{xcolor}

\begin{document} \begin{table}[!t] \centering \caption{Adjacency Matrix} \label{tab:Adjacency Matrix} \begin{tblr}{ colspec={*6{c}}, vline{1} = {2-Z}{0.8pt}, vline{2,Z} = {0.8pt}, vline{3-Y} = {0.3pt}, hline{1} = {2-Z}{0.8pt}, hline{2,Z} = {0.8pt}, hline{3-Y} = {0.3pt}, row{1} = {mode=math}, column{1} = {mode=math}, } & V_1 & V_2 & V_3 & V_4 & V_5 \ V_1 & \cellcolor{gray!25}1 & 1 & 0 & 0 & 1 \ V_2 & 1 & 0 & 1 & 0 & 1 \ V_3 & 0 & 1 & 0 & 1 & 0 \ V_4 & 0 & 0 & 1 & 0 & 1 \ V_5 & 1 & 1 & 0 & 1 & 0 \ \end{tblr} \end{table} \end{document}

emnha
  • 445
  • 3
  • 11
  • 1
    tabularay provides two ways to colourise cells: either in a table definition via cell{rowA-rowB}{colX-colY} or directly in the "data" via \SetCell{...} as @Zarko answered. If it's just one cell, it doesn't matter. But if you have a range of cells to colourise, I'd use the former way. You can change both foreground and background of cells. Try to add the following to the table definition: cell{1}{2-Z} = {bg=blue,fg=white}, cell{2-Z}{1} = {bg=black!50!green,fg=white},. – Celdor Aug 18 '22 at 18:11

1 Answers1

3

Package tabularray has own mechanism for coloring cells. Try the following:

\documentclass[journal]{IEEEtran}

\usepackage{xcolor} \usepackage{tabularray} \UseTblrLibrary{booktabs} \NewTableCommand\SCC[1]{\SetCell{bg=#1}} % <--- shortcut

\begin{document} \begin{table}[!t] \centering \caption{Adjacency Matrix} \label{tab:Adjacency Matrix} \begin{tblr}{ colspec={*6{c}}, vline{1} = {2-Z}{0.8pt}, vline{2,Z} = {0.8pt}, vline{3-Y} = {0.3pt}, hline{1} = {2-Z}{0.8pt}, hline{2,Z} = {0.8pt}, hline{3-Y} = {0.3pt}, row{1} = {mode=math}, column{1} = {mode=math}, } & V_1 & V_2 & V_3 & V_4 & V_5 \ V_1 & \SCC{gray!25}1 & 1 & 0 & 0 & 1 \ V_2 & 1 & 0 & 1 & 0 & 1 \ V_3 & 0 & 1 & 0 & 1 & 0 \ V_4 & 0 & 0 & 1 & 0 & 1 \ V_5 & 1 & 1 & 0 & 1 & 0 \ \end{tblr} \end{table} \end{document}

enter image description here

Addendum: you also an color cel(s) in table preamble:

\`[journal]{IEEEtran}

\usepackage{xcolor} \usepackage{tabularray} \UseTblrLibrary{booktabs} \NewTableCommand\SCC[1]{\SetCell{bg=#1}}

\begin{document} \begin{table}[!t] \centering \caption{Adjacency Matrix} \label{tab:Adjacency Matrix} \begin{tblr}{ colspec={*6{c}}, vline{1} = {2-Z}{0.8pt}, vline{2,Z} = {0.8pt}, vline{3-Y} = {0.3pt}, hline{1} = {2-Z}{0.8pt}, hline{2,Z} = {0.8pt}, hline{3-Y} = {0.3pt}, row{1} = {mode=math}, column{1} = {mode=math}, cell{2}{2}={bg=gray!9} % <--- } & V_1 & V_2 & V_3 & V_4 & V_5 \ V_1 & & 1 & 0 & 0 & 1 \ V_2 & 1 & 0 & 1 & 0 & 1 \ V_3 & 0 & 1 & 0 & 1 & 0 \ V_4 & 0 & 0 & 1 & 0 & 1 \ V_5 & 1 & 1 & 0 & 1 & 0 \ \end{tblr} \end{table} \end{document}

result is the same as before.

Zarko
  • 296,517