结合 makecell 和 cellcolor 的问题

结合 makecell 和 cellcolor 的问题

我已经看到过类似的问题,但我找到的所有答案似乎都无法解决我的问题。我试图为表格第一列的单元格着色,但找不到正确的方法。我尝试使用\cellcolor和,>{\columncolor{LightGrey}}但两者都导致相同的结果。

    \documentclass[12pt, a4paper,article]{memoir}
    \usepackage[utf8]{inputenc}
    \usepackage[T1]{fontenc}
    \usepackage[french,english]{babel}
    \usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm]{geometry}
    \usepackage{caption}
    \usepackage[dvipsnames, table, svgnames]{xcolor}
    \usepackage{diagbox}
    \usepackage{makecell}
    \usepackage{stackengine}
    \usepackage{adjustbox}
    \newcommand\xrowht[2][0]{\addstackgap[.5\dimexpr#2\relax]{\vphantom{#1}}}
    
    \begin{document}
    
    \begin{table}[ht]
    \centering
    \begin{tabular}{|c|*{5}{c|}}\hline
    \rowcolor{LightGrey}
    \backslashbox[50mm]{Algorithms}{Metrics}
    &\makebox[4em]{Accuracy} &\makebox[4em]{Precision} & \makebox[4em]{Recall} & \makebox[4em]{F1 score} &\makebox[4em]{F2 score}\\
    \hline
    \cellcolor{LightGrey}\makecell{Neural Network \\ with Embedding} & 0.62 & 0.23 & 0.82 & 0.36 & 0.54\\
    \hline
    \makecell{Neural Network \\ without Embedding} & 0.64 & 0.22 & 0.69 & 0.34 & 0.49\\
    \hline
    \makecell{Support Vector Machine \\ (SVM)} & 0.64 & 0.21 & 0.65 &0.32 & 0.46\\
    \hline\xrowht{20pt}
    \makebox[4em]{Logistic Regression} & 0.60 & 0.17 & 0.55 & 0.26 & 0.38\\
    \hline\xrowht{20pt}
    \makebox[4em]{Decision tree} & 0.72 & 0.25 & 0.56 & 0.34 & 0.46\\
    \hline\xrowht{20pt}
    \makebox[4em]{Random Forest} & 0.77 & 0.27 & 0.43 & 0.33 & 0.38\\
    \hline\xrowht{20pt}
    \makebox[4em]{XGBoost} & 0.71 & 0.27 & 0.75 & 0.4 & 0.56\\
    \hline\xrowht{20pt}
    \makebox[4em]{CatBoost} & 0.70 & 0.27 & 0.75 & 0.39 & 0.55\\
    \hline
    \end{tabular}
    \caption{Results using Weight Attention Data Set.}
    \label{tab:tableweightattention}
    \end{table}

在此处输入图片描述

尝试>{\columncolor{LightGrey}}

\documentclass[12pt, a4paper,article]{memoir}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french,english]{babel}
\usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm]{geometry}
\usepackage{caption}
\usepackage[dvipsnames, table, svgnames]{xcolor}
\usepackage{diagbox}
\usepackage{makecell}
\usepackage{stackengine}
\usepackage{adjustbox}
\usepackage{amsmath}

\newcommand\xrowht[2][0]{\addstackgap[.5\dimexpr#2\relax]{\vphantom{#1}}}

\begin{document}

\begin{table}[ht]
\centering
\begin{tabular}{|>{\columncolor{LightGrey}}c|*{5}{c|}}\hline
\rowcolor{LightGrey}
\backslashbox[50mm]{Algorithms}{Metrics}
&\makebox[4em]{Accuracy} &\makebox[4em]{Precision} & \makebox[4em]{Recall} & \makebox[4em]{F1 score} &\makebox[4em]{F2 score}\\
\hline
\makecell{Neural Network \\ with Embedding} & 0.62 & 0.23 & 0.82 & 0.36 & 0.54\\
\hline
\makecell{Neural Network \\ without Embedding} & 0.64 & 0.22 & 0.69 & 0.34 & 0.49\\
\hline
\makecell{Support Vector Machine \\ (SVM)} & 0.64 & 0.21 & 0.65 &0.32 & 0.46\\
\hline\xrowht{20pt}
\makebox[4em]{Logistic Regression} & 0.60 & 0.17 & 0.55 & 0.26 & 0.38\\
\hline\xrowht{20pt}
\makebox[4em]{Decision tree} & 0.72 & 0.25 & 0.56 & 0.34 & 0.46\\
\hline\xrowht{20pt}
\makebox[4em]{Random Forest} & 0.77 & 0.27 & 0.43 & 0.33 & 0.38\\
\hline\xrowht{20pt}
\makebox[4em]{XGBoost} & 0.71 & 0.27 & 0.75 & 0.4 & 0.56\\
\hline\xrowht{20pt}
\makebox[4em]{CatBoost} & 0.70 & 0.27 & 0.75 & 0.39 & 0.55\\
\hline
\end{tabular}
\caption{Results using Weight Attention Data Set.}
% \label{tab:tableweightattention}
\end{table}
\end{document}

在此处输入图片描述

如果有人知道我做错了什么或者有解决这个问题的方法,那么你就是我的英雄。

根据 @leandriis 的评论,我尝试使用nicematrix,但似乎没有解决\makebox。我想增加标题框的大小,但在文档中没有找到任何内容。以下是代码和获得的新表:

\documentclass[12pt, a4paper,article]{memoir}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french,english]{babel}
\usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm]{geometry}
\usepackage{caption}
\usepackage[dvipsnames, table, svgnames]{xcolor}
\usepackage{diagbox}
\usepackage{makecell}
\usepackage{stackengine}
\usepackage{adjustbox}
\usepackage{amsmath}
\usepackage{nicematrix}

\newcommand\xrowht[2][0]{\addstackgap[.5\dimexpr#2\relax]{\vphantom{#1}}}

\begin{document}

\begin{NiceTabular}{|>{\columncolor{LightGrey}}c|*{5}{c|}}\hline
\rowcolor{LightGrey}
\makebox[4em]{\diagbox{Algorithms}{Metrics}} &\makebox[4em]{Accuracy} &\makebox[4em]{Precision} & \makebox[4em]{Recall} & \makebox[4em]{F1 score} &\makebox[4em]{F2 score}\\
\hline
\makecell{Neural Network \\ with Embedding} & 0.62 & 0.23 & 0.82 & 0.36 & 0.54\\
\hline
\makecell{Neural Network \\ without Embedding} & 0.64 & 0.22 & 0.69 & 0.34 & 0.49\\
\hline
\makecell{Support Vector Machine \\ (SVM)} & 0.64 & 0.21 & 0.65 &0.32 & 0.46\\
\hline\xrowht{20pt}
\makebox[4em]{Logistic Regression} & 0.60 & 0.17 & 0.55 & 0.26 & 0.38\\
\hline\xrowht{20pt}
\makebox[4em]{Decision tree} & 0.72 & 0.25 & 0.56 & 0.34 & 0.46\\
\hline\xrowht{20pt}
\makebox[4em]{Random Forest} & 0.77 & 0.27 & 0.43 & 0.33 & 0.38\\
\hline\xrowht{20pt}
\makebox[4em]{XGBoost} & 0.71 & 0.27 & 0.75 & 0.4 & 0.56\\
\hline\xrowht{20pt}
\makebox[4em]{CatBoost} & 0.70 & 0.27 & 0.75 & 0.39 & 0.55\\
\hline
\end{NiceTabular}

\end{document}

在此处输入图片描述

终于得到了想要的结果,谢谢大家!

\documentclass[12pt, a4paper,article]{memoir}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[french,english]{babel}
\usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm]{geometry}
\usepackage{caption}
\usepackage[dvipsnames, table, svgnames]{xcolor}
\usepackage{diagbox}
\usepackage{makecell}
\usepackage{stackengine}
\usepackage{adjustbox}
\usepackage{amsmath}
\usepackage{nicematrix}

\newcommand\xrowht[2][0]{\addstackgap[.5\dimexpr#2\relax]{\vphantom{#1}}}

\begin{document}

\begin{table}[ht]
\centering

\begin{NiceTabular}{c*{5}{wc{4em}}}[ code-before = \columncolor{LightGrey}{1} \rowcolor{LightGrey}{1} , hvlines ]
\xrowht{20pt}
\diagbox{Algorithms}{Metrics} & Accuracy &Precision & Recall & F1 score &F2 score\\
\makecell{Neural Network \\ with Embedding} & 0.62 & 0.23 & 0.82 & 0.36 & 0.54\\
\makecell{Neural Network \\ without Embedding} & 0.64 & 0.22 & 0.69 & 0.34 & 0.49\\
\makecell{Support Vector Machine \\ (SVM)} & 0.64 & 0.21 & 0.65 &0.32 & 0.46\\
\xrowht{20pt}
Logistic Regression & 0.60 & 0.17 & 0.55 & 0.26 & 0.38\\
\xrowht{20pt}
Decision tree & 0.72 & 0.25 & 0.56 & 0.34 & 0.46\\
\xrowht{20pt}
Random Forest & 0.77 & 0.27 & 0.43 & 0.33 & 0.38\\
\xrowht{20pt}
XGBoost & 0.71 & 0.27 & 0.75 & 0.4 & 0.56\\
\xrowht{20pt}
CatBoost & 0.70 & 0.27 & 0.75 & 0.39 & 0.55\\
\end{NiceTabular}

\caption{Results using Weight Attention Data Set.}
\label{tab:tableweightattention}
\end{table}

\end{document}

在此处输入图片描述

答案1

很久以来,makecell 和着色表的问题就已为人所知。一个简单的方法就是用 替换\makecell中的彩色列/行。我借此机会简化了您的代码(例如,现在默认输入编码是 utf8,因此无需加载),\Centerstack并添加了一些改进,键入列以在小数点上对齐,并加载 以在第一列的多行单元格中添加一些垂直填充。stackengineinputencSv=cellspace

   \documentclass[12pt, a4paper,article]{memoir}
    \usepackage[T1]{fontenc}
    \usepackage[french,english]{babel}
    \usepackage[margin=2cm]{geometry}
    \usepackage{caption}
    \usepackage{siunitx} 
    \usepackage[dvipsnames, table, svgnames]{xcolor}
    \usepackage{diagbox}
    \usepackage{makecell}
    \usepackage[usestackEOL]{stackengine}
    \usepackage[column=O]{cellspace}
    \setlength{\cellspacetoplimit}{2pt}
    \setlength{\cellspacebottomlimit}{2pt}
    \usepackage{adjustbox}
    \newcommand\xrowht[2][0]{\addstackgap[.5\dimexpr#2\relax]{\vphantom{#1}}}

    \begin{document}

    \begin{table}[ht]
    \centering
    \sisetup{table-format=1.2, table-column-width = 4em, table-number-alignment=center}
    \begin{tabular}{| >{\columncolor{LightGrey}}Oc|*{5}{S|}}\hline
    \rowcolor{LightGrey}
   \multicolumn{1}{|c|}{ \backslashbox[50mm]{Algorithms}{Metrics}}
    &{Accuracy} &{Precision} & {Recall} &{F1 score} &{F2 score}\\
    \hline
    \cellcolor{LightGrey}\Centerstack{Neural Network \\ with Embedding} & 0.62 & 0.23 & 0.82 & 0.36 & 0.54\\
    \hline
    \Centerstack{Neural Network \\ without Embedding} & 0.64 & 0.22 & 0.69 & 0.34 & 0.49\\
    \hline
    \Centerstack{Support Vector Machine \\ (SVM)} & 0.64 & 0.21 & 0.65 &0.32 & 0.46\\
    \hline\xrowht{20pt}
    Logistic Regression & 0.60 & 0.17 & 0.55 & 0.26 & 0.38\\
    \hline\xrowht{20pt}
    Decision tree & 0.72 & 0.25 & 0.56 & 0.34 & 0.46\\
    \hline\xrowht{20pt}
    Random Forest & 0.77 & 0.27 & 0.43 & 0.33 & 0.38\\
    \hline\xrowht{20pt}
     XGBoost & 0.71 & 0.27 & 0.75 & 0.4 & 0.56\\
    \hline\xrowht{20pt}
     CatBoost & 0.70 & 0.27 & 0.75 & 0.39 & 0.55\\
    \hline
    \end{tabular}
    \caption{Results using Weight Attention Data Set.}
    \label{tab:tableweightattention}
    \end{table}

    \end{document} 

在此处输入图片描述

答案2

包含:{NiceTabular}nicematrix

\documentclass{article}
\usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm]{geometry}
\usepackage[svgnames]{xcolor}
\usepackage{nicematrix}

\begin{document}


\begin{table}[ht]
\centering
\begin{NiceTabular}{c*{5}{wc{4em}}}[hvlines,cell-space-limits=1mm]
\CodeBefore
  \columncolor{LightGrey}{1} 
  \rowcolor{LightGrey}{1} 
\Body
  \diagbox{Algorithms}{Metrics}
  &Accuracy &Precision &Recall &F1 score &F2 score \\
  \Block{}{Neural Network \\ with Embedding} & 0.62 & 0.23 & 0.82 & 0.36 & 0.54\\
  \Block{}{Neural Network \\ without Embedding} & 0.64 & 0.22 & 0.69 & 0.34 & 0.49\\
  \Block{}{Support Vector Machine \\ (SVM)} & 0.64 & 0.21 & 0.65 &0.32 & 0.46\\
  Logistic Regression & 0.60 & 0.17 & 0.55 & 0.26 & 0.38\\
  Decision tree & 0.72 & 0.25 & 0.56 & 0.34 & 0.46\\
  Random Forest & 0.77 & 0.27 & 0.43 & 0.33 & 0.38\\
  XGBoost & 0.71 & 0.27 & 0.75 & 0.4 & 0.56\\
  CatBoost & 0.70 & 0.27 & 0.75 & 0.39 & 0.55\\
\end{NiceTabular}
\caption{Results using Weight Attention Data Set.}
\end{table}

\end{document}

上述代码的输出

相关内容