![彩色表格单元格不显示](https://linux22.com/image/452640/%E5%BD%A9%E8%89%B2%E8%A1%A8%E6%A0%BC%E5%8D%95%E5%85%83%E6%A0%BC%E4%B8%8D%E6%98%BE%E7%A4%BA.png)
在下表中,未显示颜色:
\documentclass[
11pt,english, onehalfspacing, nolistspacing, liststotoc, headsepline, oneside, chapterinoneline, reqno, a4paper, toctotoc, table]{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[shortlabels]{enumitem}
\usepackage[autostyle=true]{csquotes}
\usepackage[backend=biber, style=apa, natbib=true, autocite=inline, uniquename=false]{biblatex}
\usepackage[font=large,labelfont=bf]{caption}
\usepackage{xcolor}
\usetikzlibrary{calc}
%
\usepackage{amsthm, amsmath, amssymb, amsfonts, exscale, latexsym, float, eucal, mathtools, adjustbox, textcomp, mathpazo, lipsum, rotating, pdflscape, subcaption, booktabs, makecell, flafter, xpatch, wrapfig, tikz, minted, arydshln, blkarray, tabularx, array}
\begin{table}[!htbp]
\begin{adjustbox}{width=.85\textwidth}
\begin{threeparttable}
\setlength\tabcolsep{.1cm}
\renewcommand{\arraystretch}{1.3}
\caption{Agreement}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} l *{11}{c}}
\toprule\toprule
& I & II & III & IV & V & VI & VII & VIII & IX & X & XI \\ \midrule
AffinityPropagation & - & \cellcolor{red!25} & & & & & & \\
Agglomerative & \cellcolor{red!25} & - & & \cellcolor{red!25} & & & & \\
BIRCH & & & - & & & & & \\
CLIQUE & \cellcolor{green!25} & \cellcolor{red!25} & & - & \cellcolor{red!25} \cellcolor{green!25} & \cellcolor{green!25} & \cellcolor{green!25} & \cellcolor{red!25} & \cellcolor{red!25} & \cellcolor{green!25} & \cellcolor{green!25} \\
DBSCAN & \cellcolor{green!25} & & & & - & & & \\
GaussianMixture & \cellcolor{green!25} & \cellcolor{red!25} & & & & - & & \\
$K$-means & \cellcolor{green!25} & \cellcolor{red!25} & & & & & - & \\
$K$-medoids & \cellcolor{green!25} & \cellcolor{red!25} & & & & & & - \\
MeanShift & \cellcolor{red!25} & \cellcolor{green!25} & & & & & & & - & & \\
OPTICS & \cellcolor{green!25} & \cellcolor{red!25} & & & & & & & & - & \\
Spectral & \cellcolor{green!25} & \cellcolor{red!25} & & & & & & & & & - \\
\bottomrule\bottomrule
\end{tabular*}
\end{threeparttable}
\end{adjustbox}
\end{table}
我还想知道我是否需要所有这些环境:table
,,,tabular
adjustbox
threeparttable
答案1
我建议您使用一个tabularx
环境,使 11 个数据列具有相同的宽度,并将环境的整体宽度设置tabularx
为\textwidth
如果您认为彩色矩形必须用空格隔开,同时仍使 11 个数据列等宽,我建议您使用环境tabular*
。请参阅下面的第二个表格以了解此想法的实现。
tabularx
我不会将或环境的宽度设置tabular*
为小于1\textwidth
除非您的文档边距非常窄(这是您迄今为止尚未提供的信息。)无论如何,请直接设置tabularx
或tabular*
环境的预期整体宽度,而不是通过adjustbox
临时设置。使用该临时设置会导致表格中的字体大小极不一致……
噢,请不要重复使用\toprule
和\bottomrule
指令——除非你想让你的桌子看起来故意显得业余,甚至粗俗。
\documentclass{article}
\usepackage[table]{xcolor} % load the 'colortbl' package automatically
\newcommand\crd{\cellcolor{red!25}}
\newcommand\cgr{\cellcolor{green!25}}
\usepackage{booktabs} % \toprule, \cmidrule, \bottomrule, \addlinespace macros
\usepackage{tabularx} % for tabularx environment
\newcolumntype{C}{>{\centering\arraybackslash}X} % centered version of X col. type
\usepackage{array} % for 'w' column type
\newlength\mylen
\settowidth{\mylen}{VIII} % measure usable width of the 'w'-type columns
\begin{document}
\begin{table}[!htbp]
\centering
\setlength\tabcolsep{1pt} % default: 6pt
\renewcommand{\arraystretch}{1.25}
\caption{\texttt{tabularx}-based solution\strut}
\begin{tabularx}{1\textwidth}{@{} l @{\quad} *{11}{C} }
\toprule
& I & II & III & IV & V & VI & VII & VIII & IX & X & XI \\
\cmidrule{2-12}
Affinity Propagation & -- & \crd \\
Agglomerative & \crd & -- & & \crd \\
BIRCH & & & -- \\
CLIQUE & \cgr & \crd & & -- & \crd & \cgr & \cgr & \crd & \crd & \cgr & \cgr \\
DBSCAN & \cgr & & & & -- \\
Gaussian Mixture & \cgr & \crd & & & & -- \\
$K$-means & \cgr & \crd & & & & & -- \\
$K$-medoids & \cgr & \crd & & & & & & -- \\
Mean Shift & \crd & \cgr & & & & & & & -- \\
OPTICS & \cgr & \crd & & & & & & & & -- \\
Spectral & \cgr & \crd & & & & & & & & & -- \\
\bottomrule
\end{tabularx}
\end{table}
\begin{table}[!htbp]
\centering
\setlength\tabcolsep{0pt}
\caption{\texttt{tabular*}-based solution\strut}
\begin{tabular*}{1\textwidth}{@{\extracolsep{\fill}} l *{11}{wc{\mylen}} }
\toprule
& I & II & III & IV & V & VI & VII & VIII & IX & X & XI \\
\cmidrule{2-12}
Affinity Propagation & -- & \crd \\ \addlinespace[3pt]
Agglomerative & \crd & -- & & \crd \\ \addlinespace[3pt]
BIRCH & & & -- \\ \addlinespace[3pt]
CLIQUE & \cgr & \crd & & -- & \crd & \cgr & \cgr & \crd & \crd & \cgr & \cgr \\ \addlinespace[3pt]
DBSCAN & \cgr & & & & -- \\ \addlinespace[3pt]
Gaussian Mixture & \cgr & \crd & & & & -- \\ \addlinespace[3pt]
$K$-means & \cgr & \crd & & & & & -- \\ \addlinespace[3pt]
$K$-medoids & \cgr & \crd & & & & & & -- \\ \addlinespace[3pt]
Mean Shift & \crd & \cgr & & & & & & & -- \\ \addlinespace[3pt]
OPTICS & \cgr & \crd & & & & & & & & -- \\ \addlinespace[3pt]
Spectral & \cgr & \crd & & & & & & & & & -- \\
\bottomrule
\end{tabular*}
\end{table}
\end{document}