\documentclass{article}
\usepackage[table]{xcolor}
\begin{document}
\begin{table}
\caption{a table}
\begin{tabular}{c c c c c c}
\cellcolor{blue!=10} 1 & 2 & 3 & 4 & 5 & 6 \\
4 & 5 & 6 & 7 & 8 & 9 \\
\end{tabular}
\end{table}
\end{document}
我想在表格下方或旁边的某个位置添加一个填充的矩形(颜色=蓝色!=10)(具体位置无关紧要)来解释表格中单元格颜色的含义。基本上,我想创建一个图例。
我是否必须为此目的创建一个 tikzpicture 或者是否有更简单的解决方法?
答案1
将标题和图例与表格对齐,表格会看起来更好。添加threeparttable
包并将标题和图例放在环境中threeparttable
。
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{threeparttable}% added <<<<<<<<<<<
\begin{document}
\begin{table}
\begin{threeparttable}
\caption{A table}
\begin{tabular}{c c c c c c}
\cellcolor{blue!=10} 1 & 2 & 3 & 4 & 5 & 6 \\
4 & 5 & 6 & 7 & 8 & 9 \\
\end{tabular}
\fcolorbox{blue!30}{blue!10}{\small This cell is special}
\end{threeparttable}
\end{table}
\end{document}
选项 要绘制一个小的彩色正方形,请使用命令
\legend[<optional color>]{<text>}
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{threeparttable}% added <<<<<<<<<<<
\newcommand{\legend}[2][white]{%
\setlength{\fboxsep}{0.2pt}\fcolorbox{black}{#1}{\rule{0pt}{1.5ex}\rule{1.5ex}{0pt}}{\enspace \small #2}
}
\begin{document}
\begin{table}
\begin{threeparttable}
\caption{A table}
\begin{tabular}{c c c c c c}
\cellcolor{blue!=10} 1 & 2 & 3 & 4 & 5 & 6 \\
4 & 5 & 6 & 7 & 8 & 9 \\
\end{tabular}
\legend[blue!10]{This cell is special}
\end{threeparttable}
\end{table}
\end{document}
答案2
和tabularray
\documentclass{article}
\usepackage{xcolor}
\usepackage{tabularray}
\begin{document}
\begin{tblr}
{
colspec={*{6}{Q[c,mode=math]}},
cell{1}{1}={bg=blue!10},
cell{2}{3}={bg=red!10},
}
1 & 2 & 3 & 4 & 5 & 6 \\
4 & 5 & 6 & 7 & 8 & 9
\end{tblr}
\qquad
\begin{tblr}
{
colspec={Q[wd=1em] Q},
cell{1}{1}={bg=blue!10},
cell{2}{1}={bg=red!10},
vline{1,2},
hline{1-Z} = {1}{solid},
}
& some text\\
& another
\end{tblr}
\end{document}