使用单元格背景颜色时表格出现问题

使用单元格背景颜色时表格出现问题

我遇到了一个问题,表格使用颜色作为某些单元格的背景。使用的代码是:

\documentclass[sigconf]{acmart}

\usepackage{comment}
\usepackage{balance}
\usepackage{graphicx}
\usepackage{booktabs} % For formal tables
\usepackage{xcolor}
\usepackage[table,xcdraw]{}

% Please add the following required packages to your document preamble:
% \usepackage{graphicx}
% \usepackage[table,xcdraw]{xcolor}
% If you use beamer only pass "xcolor=table" option, i.e. \documentclass[xcolor=table]{beamer}
\begin{table}[]
\centering
\caption{xxxxyyyyzzzzz}
\label{tab:xxxx}
\resizebox{\columnwidth}{!}{%
\begin{tabular}{|
>{\columncolor[HTML]{FFFFFF}}l |l|l|
>{\columncolor[HTML]{D9D9D9}}l |
>{\columncolor[HTML]{FF6D01}}l |
>{\columncolor[HTML]{D9D9D9}}l |
>{\columncolor[HTML]{D9D9D9}}l |
>{\columncolor[HTML]{FFFFFF}}l }
\cline{1-7}
 &
  \cellcolor[HTML]{FFFFFF}\textbf{S} &
  \cellcolor[HTML]{FFFFFF}\textbf{T} &
  \cellcolor[HTML]{FFFFFF}\textbf{R} &
  \cellcolor[HTML]{FFFFFF}\textbf{I} &
  \cellcolor[HTML]{FFFFFF}\textbf{D} &
  \cellcolor[HTML]{FFFFFF}\textbf{E} &
  \textbf{} \\ \cline{1-7}
\textbf{DDoS} &
  \cellcolor[HTML]{D9D9D9} &
  \cellcolor[HTML]{D9D9D9} &
   &
  \cellcolor[HTML]{D9D9D9} &
  \cellcolor[HTML]{FF6D01} &
   &
   \\ \cline{1-7}
\textbf{Tampering} &
  \cellcolor[HTML]{FF6D01} &
  \cellcolor[HTML]{0B5394} &
  \cellcolor[HTML]{FBBC04} &
   &
   &
   &
   \\ \cline{1-7}
\textbf{Hijacking} &
  \cellcolor[HTML]{FF6D01} &
  \cellcolor[HTML]{FF6D01} &
   &
   &
   &
   &
   \\ \cline{1-7}
\textbf{Jamming} &
  \cellcolor[HTML]{D9D9D9} &
  \cellcolor[HTML]{D9D9D9} &
   &
   &
  \cellcolor[HTML]{FF6D01} &
   &
   \\ \cline{1-7}
\textbf{Man-in-the-middle} &
  \cellcolor[HTML]{FBBC04} &
  \cellcolor[HTML]{FF6D01} &
  \cellcolor[HTML]{FBBC04} &
   &
   &
  \cellcolor[HTML]{FBBC04} &
   \\ \cline{1-7}
\textbf{Eavesdropping} &
  \cellcolor[HTML]{D9D9D9} &
  \cellcolor[HTML]{D9D9D9} &
   &
   &
   &
   &
   \\ \cline{1-7}
\textbf{Skimming} &
  \cellcolor[HTML]{FBBC04} &
  \cellcolor[HTML]{0B5394} &
  \cellcolor[HTML]{FBBC04} &
   &
   &
   &
   \\ \cline{1-7}
\textbf{Wormhole} &
  \cellcolor[HTML]{D9D9D9}\textbf{} &
  \cellcolor[HTML]{D9D9D9}\textbf{} &
  \textbf{} &
  \textbf{} &
  \cellcolor[HTML]{0B5394}\textbf{} &
  \textbf{} &
   \\ \cline{1-7}
\textbf{Bruteforce} &
  \cellcolor[HTML]{FBBC04} &
  \cellcolor[HTML]{D9D9D9} &
   &
  \cellcolor[HTML]{D9D9D9} &
   &
   &
   \\ \cline{1-7}
\textbf{Spoofing} &
  \cellcolor[HTML]{FF6D01}\textbf{} &
  \cellcolor[HTML]{0B5394} &
   &
  \textbf{} &
  \cellcolor[HTML]{0B5394} &
   &
   \\ \cline{1-7}
\textbf{Sybil Attack} &
  \cellcolor[HTML]{FBBC04} &
  \cellcolor[HTML]{D9D9D9} &
   &
  \cellcolor[HTML]{FBBC04} &
   &
  \cellcolor[HTML]{FF6D01}\textbf{} &
   \\ \cline{1-7}
\textbf{Node Replication} &
  \cellcolor[HTML]{D9D9D9} &
  \cellcolor[HTML]{0B5394} &
   &
  \cellcolor[HTML]{FBBC04} &
  \cellcolor[HTML]{FF6D01}\textbf{} &
   &
   \\ \cline{1-7}
\textbf{Relay Attack} &
  \cellcolor[HTML]{FBBC04} &
  \cellcolor[HTML]{0B5394} &
  \cellcolor[HTML]{FBBC04} &
   &
   &
   &
   \\ \cline{1-7}
\end{tabular}%
}
\end{table}

我在 Overleaf 中使用 ACMsigconf模板,出现许多与该表相关的错误:

<argument> | {\columncolor 
                           [HTML]{FFFFFF}}l |l|l| >{\columncolor [HTML]{D9D9...
l.207 }
       
The control sequence at the end of the top line of your error message was never \def'ed. If you have misspelled it (e.g., `\hobx'), type `I' and the correct spelling (e.g., `I\hbox'). Otherwise just continue, and I'll forget about whatever was undefined.

有人知道我为什么会收到这些错误吗?我使用表格生成器

答案1

您的 MWE(最小工作示例,包含表格的一个小但完整的文档)有两个错误:

  • 错过了begin{document}
  • 错误使用xcolor
    • 如果使用article文档类,你应该
\usepackage{xcolor} 
\usepackage[table,xcdraw]{}   

你应该写

\usepackage[table,xcdraw]{xcolor}
  • 如果使用acmart文档类,您应该更改
\documentclass[sigconf]{acmart}

您在评论中建议的内容

\documentclass[sigconf,prologue,table,xcdraw]{acmart}

并删除

\usepackage{xcolor} 
\usepackage[table,xcdraw]{}   

在 Overleaf 中测试你的 MWE(我还没有安装acmart文档类)我得到:

在此处输入图片描述

附录:
离题:使用包tblr中定义的表格tabularray和命名颜色的手写表格示例:

\documentclass[sigconf,dvipsnames]{acmart}
\usepackage[skip=0.33\baselineskip]{caption}
\usepackage[dvipsnames]{xcolor}
\usepackage{tabularray}
\NewTableCommand\SCC[1]{\SetCell{bg=#1}}


\begin{document}
    \begin{table}[ht]
    \centering
\caption{Threats classified with STRIDE and CVSS}
\label{tab:stride}
    \begin{tblr}{hlines, vlines,
                 colspec = {l *{6}{Q[c, wd=2em, bg=gray!20]}},
                 row{1} = {font=\bfseries, bg=white}
                 }
        & S & T & R & I & D & E             \\
DDoS    &   &   &   &   & \SCC{orange}
                            &               \\
Tampering
        & \SCC{Orange}
            & \SCC{NavyBlue}
                & \SCC{Dandelion}
                    & \SCC{Orange}
                        &   &               \\
Hijacking
        & \SCC{Orange}
            & \SCC{Orange}
                &   & \SCC{Orange}
                        &   &               \\
Jamming
        &   &   &   &  \SCC{Orange}
                        & \SCC{Orange}
                            &               \\
Man-in-the-middle
        & \SCC{Dandelion}
            & \SCC{Orange}
                & \SCC{Dandelion}
                    & \SCC{Orange}
                        &   & \SCC{Dandelion}  \\
Eavesdropping
        &   &   &   & \SCC{Orange}
                        &   &               \\
Skimming
        & \SCC{Dandelion}
            & \SCC{NavyBlue}
                & \SCC{Dandelion}
                    & \SCC{Orange}
                        &   &               \\ 

Wormhole    
        &   &   &   & \SCC{Orange}
                        & \SCC{NavyBlue}
                            &               \\
Bruteforce
        & \SCC{Dandelion}
            &   &   &   &   &               \\
Spoofing
        & \SCC{Orange}
            & \SCC{NavyBlue}
                &   & \SCC{Orange}
                        & \SCC{NavyBlue}
                            &               \\
Sybil Attack
       & \SCC{Orange}
            &   &   & \SCC{Dandelion}
                        &   & \SCC{Orange}  \\
Node Replication
       &    &\SCC{NavyBlue}
                &   & \SCC{Dandelion}
                        & \SCC{Orange}  
                            &           \\
Relay Attack
       & \SCC{Dandelion}
            & \SCC{NavyBlue}
                & \SCC{Dandelion}
                    & \SCC{Orange}
                        &   &           \\
     \end{tblr}
    \end{table}
\end{document}




\documentclass{article}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.18}

\begin{document}
    \begin{figure}[ht]
\begin{tikzpicture}
    \begin{groupplot}[
group style={
    group size=3 by 1,
    ylabels at=edge left,
    horizontal sep=6mm,
            },
%
    width=0.27\linewidth, %height=4cm,
    scale only axis,
    title style = {font=\small},
%
    enlarge x limits=0.3,
    xlabel={x},     ylabel={y},
    symbolic x coords={3-6, 6-12, 12-24},
    %xtick={3-6, 6-12, 12-24},
    xticklabel style={font=\small},
    ytick={0,2,...,12},
    ymin=0,     ymax=12,
%
    nodes near coords,
    every node near coord/.append style={font=\scriptsize},
%
    legend image code/.code={\draw[#1] (0cm,-0.1cm) rectangle (0.6cm,0.1cm);},
    legend style={at={(0.5,-0.3)},
                  anchor=north,
                  legend columns=-1,
                  font=\footnotesize,
                  /tikz/every even column/.append style={column sep=1em}
                  }
                    ]
\nextgroupplot[title=MADDPG]
    \addplot[ybar, fill=olive]  coordinates {  (3-6,   6.886) };
    \addplot[ybar, fill=orange] coordinates {  (6-12,  8.501) };
    \addplot[ybar, fill=teal]   coordinates {  (12-24,10.179) };

\nextgroupplot[title={\scriptsize MATD3}]
    \addplot[ybar, fill=olive]  coordinates {  (3-6,   6.886) };
    \addplot[ybar, fill=orange] coordinates {  (6-12,  8.501) };
    \addplot[ybar, fill=teal]   coordinates {  (12-24,10.179) };
\legend{\color{olive}{Random},
        \color{orange}{++Cost},
        \color{teal}{++FTE}},

\nextgroupplot[title={\scriptsize MASAC}]
    \addplot[ybar, fill=olive]  coordinates {  (3-6,   6.886) };
    \addplot[ybar, fill=orange] coordinates {  (6-12,  8.501) };
    \addplot[ybar, fill=teal]   coordinates {  (12-24,10.179) };
    \end{groupplot}
\end{tikzpicture}
        \caption{Triage++ Performance}
        \label{PlusPlusCombinedBar}
    \end{figure}
\end{document}


\documentclass{article}
\usepackage[margin=25mm]{geometry}
\usepackage[T1]{fontenc}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx}

\begin{document}
    \begin{longtblr}[
caption={Add caption},
  label={tab:addlabel}    
                    ]{hlines, vlines,
                      colspec={l 
                               X[2,l] 
                               *{4}{X[c, si={table-format=-1.4}]}},
                      row{1,2}={guard, font=\bfseries, m},
                      rowhead=2
                      }
\SetCell[r=2]{l}    Value
        &    \SetCell[r=2]{l}    Group
            &    \SetCell[c=4]{c}    Tukey HSD test
                &    &    &                                 \\
        &   &    diff
                &    lwr
                    &    upr
                        &    {Pr(>\,F) \\ (0.05)}           \\
\SetCell[c=6]{l, font=\bfseries}   Gender                                   \\
    HEDONISM
        & Male-Female
            & -0.2468   & -0.2890   & -0.2046   &  0.0000   \\
    SELF\_DIRECTION
        & Male-Female
            & -0.2015   & -0.2506   & -0.1524   &  0.0000   \\
    STIMULATION
        & Male-Female
            &  0.1442   &  0.0919   &  0.1966   &  0.0000   \\
    SECURITY
        & Male-Female
            &  0.0264   & -0.0300   &  0.0828   &  0.3584   \\
    CONFORMITY
        & Male-Female
            &  0.0378   & -0.0132   &  0.0888   &  0.1459   \\
    TRADITION
        & Male-Female
            &  0.0619   &  0.0121   &  0.1117   &  0.0148   \\
    ACHIEVEMENT
        & Male-Female
            & -0.0389   & -0.0928   &  0.0150   &  0.1567   \\
    POWER
        & Male-Female
            &  0.2788   &  0.2352   &  0.3225   &  0.0000    \\*
    UNIVERSALISM
        & Male-Female
            &  0.2295   &  0.1844   &  0.2746   &  0.0000   \\*
    BENEVOLENCE
        & Male-Female
            &  0.0388   & -0.0143   &  0.0920   &  0.1520   \\
\SetCell[c=6]{l, font=\bfseries}   Age                                      \\
\SetCell[r=3]{l}    HEDONISM
    & Old-Middle
            &  0.1400   &  0.0624   &  0.2176   &  0.0001   \\*
    & Young-Middle
            & -0.3025   & -0.3800   & -0.2250   &  0.0000   \\*
    & Young-Old
            & -0.4425   & -0.5306   & -0.3543   &  0.0000   \\
    \SetCell[r=3]{l}    SELF\_DIRECTION
        & Old-Middle
            &  0.1956   &  0.1292   &  0.2620   &  0.0000   \\*
    & Young-Middle
            & -0.3776   & -0.4439   & -0.3113   &  0.0000   \\*
    & Young-Old
            & -0.5732   & -0.6486   & -0.4978   &  0.0000   \\
    \end{longtblr}
    \end{document}

结果与上面的非常相似(颜色可能略有不同,并且第一列中的文本不是粗体):

在此处输入图片描述

在这种情况下,由于某种原因(在我看来,排版不太美观),然后将`colspec更改为:

                 colspec = {Q[font=\bfseries *{6}{Q[c, wd=2em, bg=gray!20]}},

相关内容