使用颜色突出显示多列表格内的值

使用颜色突出显示多列表格内的值

我试图突出显示多列表格内的值,但没有得到准确的结果。

所以我的表格如下所示:

在此处输入图片描述

代码

\pdfoutput=1
\documentclass[11pt]{article}
\usepackage[review]{emnlp2021}
\usepackage{color}
\usepackage{rotating}
\definecolor{mycolor}{gray}{0.8}
\usepackage{booktabs}
\usepackage{makecell}
\usepackage{xcolor}
\usepackage{tabularx}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{microtype}
\begin{document}

Example of color code \colorbox{mycolor}{ \footnotesize  [SEP] }

\begin{table*}[t!]
    \centering
    \resizebox{0.85\textwidth}{!}{%
    \begin{tabular}{@{}l|c@{ }c@{ }c@{ }|c@{ }c@{ }c@{ }|ccc@{ }}
    \toprule
    {} & \multicolumn{3}{c|}{\bf Metric A} & \multicolumn{3}{c|}{\bf Metric B} & \multicolumn{3}{c}{\bf Metric C}\\
    \midrule
    \bf Model & \multicolumn{1}{c}{\bf Precision} &  \multicolumn{1}{c}{\bf Recall} & \multicolumn{1}{r|}{\bf F1-score} & \multicolumn{1}{c}{\bf Precision} & \multicolumn{1}{c}{\bf Recall} & \multicolumn{1}{r|}{\bf F1-score} & \multicolumn{1}{c}{\bf Precision} &  \multicolumn{1}{c}{\bf Recall} &  \multicolumn{1}{c}{\bf F1-score} \\
    \midrule
   
  modela & 92.21 & 92.54 & 92.28 & 90.13 & 90.38 & 90.19 & 89.06 & 89.31 & 88.08 \\
  modelb & 92.21 & 92.54 & 92.28 & 90.13 & 90.38 & 90.19 & 89.06 & 89.31 & 88.08 \\
  modelc & 92.21 & 92.54 & 92.28 & 90.13 & 90.38 & 90.19 & 89.06 & 89.31 & 88.08 \\
  modeld & 92.21 & 92.54 & 92.28 & 92.13 & 92.38 & 92.19 & 89.06 & 89.31 & 88.08 \\
  modelf & 95.21 & 95.54 & 95.28 & 90.13 & 90.38 & 90.19 & 97.06 & 98.31 & 99.08 \\
    
    \bottomrule
    \end{tabular}
    }
    \caption{Performance of all baseline models
    }
    \label{tab:accbinary_token}
\end{table*}
\end{document}

我在用这个模板

我定义了一个像这样的自定义颜色代码:

\definecolor{mycolor}{gray}{0.8}

现在,如果我尝试给表格的行着色,

\colorbox{mycolor}{95.21 & 95.54 & 95.28} & 90.13 & 90.38 & 90.19 & 97.06 & 98.31 & 99.08 \\

它显示如下:

在此处输入图片描述

预期输出:

在此处输入图片描述

我怎样才能为上表所示的多列的不同行着色?

答案1

不建议重新缩放表格,\resizebox因为这会导致规则宽度不一致。

这就是我编写此表的{sidewaystable}原因\rotating

对于某些行下的灰色形状,我使用{NiceTabular}nicematrix

该环境与经典环境类似,但会在单元{tabular}array下创建 PGF/Tikz 节点。可以将这些节点与 Tikz 结合使用,在表格下绘制我们想要的任何内容。

pdfoutput=1
\documentclass[11pt]{article}
\usepackage{emnlp2021}
\usepackage{times}
\usepackage{latexsym}
\usepackage[T1]{fontenc}

\usepackage{booktabs}
\usepackage{xcolor}
\usepackage{rotating}
\usepackage{nicematrix,tikz}
\usetikzlibrary{fit}


\begin{document}

\begin{sidewaystable}
    \centering
    \begin{NiceTabular}{@{}l|c@{ }c@{ }c@{ }|c@{ }c@{ }c@{ }|ccc@{ }}
    \CodeBefore [create-cell-nodes]
       \begin{tikzpicture} [every node/.style = {fill=gray!20,rounded corners}]
          \node [fit = (7-2) (7-4)] {} ;
          \node [fit = (6-5) (6-7)] {} ;
          \node [fit = (7-8) (7-10)] {} ;
       \end{tikzpicture}
    \Body
    \toprule
    \RowStyle[nb-rows=2]{\bfseries}
      & \multicolumn{3}{c}{Metric A} & \multicolumn{3}{c}{Metric B} & \multicolumn{3}{c}{Metric C}\\
    \midrule
    Model & Precision & Recall & F1-score & Precision & Recall & F1-score & Precision & Recall & F1-score \\
    \midrule
      modela & 92.21 & 92.54 & 92.28 & 90.13 & 90.38 & 90.19 & 89.06 & 89.31 & 88.08 \\
      modelb & 92.21 & 92.54 & 92.28 & 90.13 & 90.38 & 90.19 & 89.06 & 89.31 & 88.08 \\
      modelc & 92.21 & 92.54 & 92.28 & 90.13 & 90.38 & 90.19 & 89.06 & 89.31 & 88.08 \\
      modeld & 92.21 & 92.54 & 92.28 & 92.13 & 92.38 & 92.19 & 89.06 & 89.31 & 88.08 \\
      modelf & 95.21 & 95.54 & 95.28 & 90.13 & 90.38 & 90.19 & 97.06 & 98.31 & 99.08 \\
    \bottomrule
    \end{NiceTabular}
    \caption{Performance of all baseline models
    }
    \label{tab:accbinary_token}
\end{sidewaystable}
\end{document}

您需要多次编译(因为 PGF/Tikz 节点)。

上述代码的输出


为了获得更好的设计,人们可能会建议您仅使用水平规则,例如(本着 的精神\booktabs):

pdfoutput=1
\documentclass[11pt]{article}
\usepackage{emnlp2021}
\usepackage{times}
\usepackage{latexsym}
\usepackage[T1]{fontenc}

\usepackage{booktabs}
\usepackage{xcolor}
\usepackage{rotating}
\usepackage{nicematrix,tikz}
\usetikzlibrary{fit}


\begin{document}



\begin{sidewaystable}[t!]
    \centering
    \renewcommand{\arraystretch}{1.1}
    \begin{NiceTabular}{@{}lccccccccc@{}}
    \CodeBefore [create-cell-nodes]
       \begin{tikzpicture} [every node/.style = {fill=gray!20,rounded corners}]
          \node [fit = (7-2) (7-4)] {} ;
          \node [fit = (6-5) (6-7)] {} ;
          \node [fit = (7-8) (7-10)] {} ;
       \end{tikzpicture}
    \Body
    \toprule
    \Block{2-1}{Model}  & \Block{1-3}{Metric A} &&& \Block{1-3}{Metric B} &&& \Block{1-3}{Metric C}\\
    \cmidrule(rl){2-4} \cmidrule(rl){5-7} \cmidrule(l){8-10}
    & Precision & Recall & F1-score & Precision & Recall & F1-score & Precision & Recall & F1-score \\
    \midrule
      modela & 92.21 & 92.54 & 92.28 & 90.13 & 90.38 & 90.19 & 89.06 & 89.31 & 88.08 \\
      modelb & 92.21 & 92.54 & 92.28 & 90.13 & 90.38 & 90.19 & 89.06 & 89.31 & 88.08 \\
      modelc & 92.21 & 92.54 & 92.28 & 90.13 & 90.38 & 90.19 & 89.06 & 89.31 & 88.08 \\
      modeld & 92.21 & 92.54 & 92.28 & 92.13 & 92.38 & 92.19 & 89.06 & 89.31 & 88.08 \\
      modelf & 95.21 & 95.54 & 95.28 & 90.13 & 90.38 & 90.19 & 97.06 & 98.31 & 99.08 \\
    \bottomrule
    \end{NiceTabular}
    \caption{Performance of all baseline models
    }
    \label{tab:accbinary_token}
\end{sidewaystable}

\end{document}

上述代码的输出

答案2

我没有打包emnlp2021,所以我只能怀疑它(除其他外)还定义了两列页面布局。此布局是使用文档类twocolumn中的选项进行模仿的。article

Bay 使用tabularray包和\footnotesize列标题字体,可以使您的表格适合跨两列和简单的颜色选定单元格:

\documentclass[11pt, twocolumn]{article}
%\usepackage[review]{emnlp2021} % I haven't this package
\usepackage[T1]{fontenc}
\usepackage{newtxtext, newtxmath}
\usepackage{microtype}

\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}


\begin{document}
\begin{table*}
    \centering
    %\small
\begin{tblr}{colsep=3pt,
             colspec={l @{\qquad} *{10}{X[c]} },
             cell{7}{2-4} = {bg=gray!20},  % cells background color
             cell{6}{5-7} = {bg=gray!20},
             cell{7}{8-10}= {bg=gray!20},
             row{1,2} = {font=\footnotesize\bfseries}
            }
    \toprule
    &   \SetCell[c=3]{c}    Metric A
        &   &   &   \SetCell[c=3]{c}  Metric B
                    &   &   &   \SetCell[c=3]{c}    Metric C
                                &   &           \\
    \cmidrule[lr]{2-4}
    \cmidrule[lr]{5-7}
    \cmidrule[l]{2-10}
Model   & Precision & Recall    & F1-score
        & Precision & Recall    & F1-score
        & Precision & Recall    & F1-score      \\
    \midrule
model a  & 92.21 & 92.54 & 92.28 & 90.13 & 90.38 & 90.19 & 89.06 & 89.31 & 88.08 \\
model b  & 92.21 & 92.54 & 92.28 & 90.13 & 90.38 & 90.19 & 89.06 & 89.31 & 88.08 \\
model c  & 92.21 & 92.54 & 92.28 & 90.13 & 90.38 & 90.19 & 89.06 & 89.31 & 88.08 \\
model c  & 92.21 & 92.54 & 92.28 & 90.13 & 90.38 & 90.19 & 89.06 & 89.31 & 88.08 \\
model d  & 92.21 & 92.54 & 92.28 & 90.13 & 90.38 & 90.19 & 89.06 & 89.31 & 88.08 \\
    \bottomrule
\end{tblr}
    \caption{Performance of all baseline models}
    \label{tab:accbinary_token}
\end{table*}
\end{document}

在此处输入图片描述

如果您出于某种原因希望所有单元格都被水平和垂直线“监禁”,如下所示:

在此处输入图片描述

那么您只需要按如下方式更改上述 MWE:

  • 添加\hlinesvlinestblr序言
  • 删除表体所有规则
  • 略有变化colspec(参见下文的 MWE)
\documentclass[11pt, twocolumn]{article}
%\usepackage[review]{emnlp2021} % I haven't this package
\usepackage[T1]{fontenc}
\usepackage{newtxtext, newtxmath}
\usepackage{microtype}

\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}


\begin{document}
\begin{table*}
    \centering
    %\small
\begin{tblr}{hlines, vlines,             % <--- added 
             colsep = 4pt,
             colspec={l *{10}{X[c]} },   % <--- chNGED
             cell{7}{2-4} = {bg=gray!20},
             cell{6}{5-7} = {bg=gray!20},
             cell{7}{8-10} = {bg=gray!20},
             row{1,2} = {font=\footnotesize\bfseries}
            }
    &   \SetCell[c=3]{c}    Metric A
        &   &   &   \SetCell[c=3]{c}  Metric B
                    &   &   &   \SetCell[c=3]{c}    Metric C
                                &   &           \\
Model   & Precision & Recall    & F1-score
        & Precision & Recall    & F1-score
        & Precision & Recall    & F1-score      \\
model a  & 92.21 & 92.54 & 92.28 & 90.13 & 90.38 & 90.19 & 89.06 & 89.31 & 88.08 \\
model b  & 92.21 & 92.54 & 92.28 & 90.13 & 90.38 & 90.19 & 89.06 & 89.31 & 88.08 \\
model c  & 92.21 & 92.54 & 92.28 & 90.13 & 90.38 & 90.19 & 89.06 & 89.31 & 88.08 \\
model c  & 92.21 & 92.54 & 92.28 & 90.13 & 90.38 & 90.19 & 89.06 & 89.31 & 88.08 \\
model d  & 92.21 & 92.54 & 92.28 & 90.13 & 90.38 & 90.19 & 89.06 & 89.31 & 88.08 \\
\end{tblr}
    \caption{Performance of all baseline models}
    \label{tab:accbinary_token}
\end{table*}
\end{document}

相关内容