Issue combining makecell and cellcolor
The problem with makecell and colouring tables has been known for quite some time. A simple possibility, consists in replacing \makecell
in coloured columns/rows with \Centerstack
from stackengine
. I took the opportunity to simplify a bit your code (the default input encoding is now utf8, for instance, so needless to load inputenc
) and added some improvelments, S
type columns for the alignment on the decimal dot, and loading of v=cellspace
to add some vertical padding to multiline cells in the first column.
\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}
With {NiceTabular}
of nicematrix
:
\documentclass{article}
\usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm]{geometry}
\usepackage[svgnames]{xcolor}
\usepackage{nicematrix}
\begin{document}
\NiceMatrixOptions{cell-space-limits=1mm}
\begin{table}[ht]
\centering
\begin{NiceTabular}{c*{5}{wc{4em}}}%
[ code-before = \columncolor{LightGrey}{1} \rowcolor{LightGrey}{1} , hvlines ]
\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}