
我有這張表,想更改儲存格的顏色。我定義了顏色“Orange”、“DarkGrey”和“lightGrey”。我將頂部標題行設為橙色,並希望第一列中的剩餘單元格為深灰色,而每隔一列中的剩餘單元格為淺灰色。我怎樣才能做到這一點?
\centering
\begin{tabularx}{1\textwidth} {
| >{\centering\arraybackslash}X
| >{\centering\arraybackslash}X
| >{\centering\arraybackslash}X | }
\hline
\rowcolor{Orange}\multicolumn{3}{|c|}{\textcolor{white}{\textbf{Title}}} \\
\hline
item 11 & item 12 & item 13 \\
\hline
item 21 & item 22 & item 23 \\
\hline
\end{tabularx}
答案1
如果你想為背景上色,這裡是:
\documentclass{article}
\usepackage[table, svgnames]{xcolor}
\usepackage{tabularx}
\begin{document}
\centering
\begin{tabularx}{1\textwidth} {
| >{\centering\arraybackslash\columncolor{DarkGrey}}X
| >{\centering\arraybackslash\columncolor{LightGrey}}X
| >{\centering\arraybackslash\columncolor{LightGrey}}X | }
\hline
\rowcolor{Orange}\multicolumn{3}{|c|}{\textcolor{white}{\textbf{Title}}} \\
\hline
item 11 & item 12 & item 13 \\
\hline
item 21 & item 22 & item 23 \\
\hline
\end{tabularx}
\end{document}
答案2
使用 tabularray 套件
\documentclass{article}
\usepackage{tabularray}
\usepackage{xcolor}
\begin{document}
\begin{tblr}
{
hlines,vlines,
colspec=*{3}{X[c,bg=gray!25]},
column{1} = {bg=gray!75},
row{1} = {bg=orange, fg=white, font=\bfseries},
cell{1}{1} = {c=3}{c},%<-- the multicol
}
Title\\
item 11 & item 12 & item 13 \\
item 21 & item 22 & item 23
\end{tblr}
\end{document}
答案3
與.{NiceTabular}
nicematrix
\documentclass{article}
\usepackage{nicematrix}
\usepackage{xcolor}
\begin{document}
\begin{NiceTabular}{X[c]X[c]X[c]}[hvlines]
\CodeBefore
\columncolor{gray!75}{1}
\columncolor{gray!25}{2,3}
\rowcolor{orange}{1}
\Body
\Block{1-3}{\color{white}\sffamily\bfseries Title}\\
item 11 & item 12 & item 13 \\
item 21 & item 22 & item 23
\end{NiceTabular}
\end{document}
您需要多次編譯(因為nicematrix
在背景使用 PGF/Tikz 節點)。