5

I have seen similar question to this one already but none of the answers I have found seem to solve my problem. I'm trying to color the cells of the first column of my table, but I cannot find a correct way to do this. I tried using \cellcolor and >{\columncolor{LightGrey}} but both of them led to the same results.

    \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}

enter image description here

trying with >{\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}

enter image description here

If anyone has an idea about what I'm doing wrong or has solution to this problem, you would be my hero.

Following @leandriis comment I tried using the nicematrix, but does not seems to take care of the \makebox. I would like to increase the size of the header box, i did not find anything on the documentation. Here is the code and the new table obtained:

\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}

enter image description here

I finally managed to obtain the desired results, thanks everyone!

\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}

enter image description here

  • Take a look at the NiceTabular environment from the nicematrix package. – leandriis Aug 11 at 15:06
  • @leandriis thanks for telling me about this package, if you have any insight on how I can modify the size of a given row it would be awesome! – Netim Aug 11 at 15:57
  • Hi and welcome. Please give a fully compilable code. – AndréC Aug 11 at 16:01
  • @AndréC Hi and thank you, I updated the last 2 code snippet, should be better know. – Netim Aug 11 at 16:10
  • 1
    @jsbibra Yes indeed by doing so i obtain the result that is wished for! Thanks – Netim Aug 11 at 16:26
6

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, Stype 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} 

enter image description here

| improve this answer | |
5

With {NiceTabular} of nicematrix:

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

\begin{document}

\NiceMatrixOptions{cell-space-top-limit=1mm,cell-space-bottom-limit=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 \\
\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\\
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.}
% \label{tab:tableweightattention}
\end{table}

\end{document}

Ouput of the above code

| improve this answer | |
  • Following your example I adapted my previous solution and manage to obtain the result that I wanted ! Thanks a lot – Netim Aug 11 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.