为什么 cellcolor 在这里不起作用?
代码来自我上一个问题的答案这里. 我正在尝试为表格的单元格着色,就像代码一样这里。
\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}
答案1
软件包tabularray
有自己的细胞着色机制。请尝试以下操作:
\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}
附录: 您还可以在表格序言中添加彩色单元格:
\`[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}
结果和以前一样。