TransWikia.com

Issue combining makecell and cellcolor

TeX - LaTeX Asked by Netim on October 15, 2020

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}
    newcommandxrowht[2][0]{addstackgap[.5dimexpr#2relax]{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
    hlinexrowht{20pt}
    makebox[4em]{Logistic Regression} & 0.60 & 0.17 & 0.55 & 0.26 & 0.38
    hlinexrowht{20pt}
    makebox[4em]{Decision tree} & 0.72 & 0.25 & 0.56 & 0.34 & 0.46
    hlinexrowht{20pt}
    makebox[4em]{Random Forest} & 0.77 & 0.27 & 0.43 & 0.33 & 0.38
    hlinexrowht{20pt}
    makebox[4em]{XGBoost} & 0.71 & 0.27 & 0.75 & 0.4 & 0.56
    hlinexrowht{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}

newcommandxrowht[2][0]{addstackgap[.5dimexpr#2relax]{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
hlinexrowht{20pt}
makebox[4em]{Logistic Regression} & 0.60 & 0.17 & 0.55 & 0.26 & 0.38
hlinexrowht{20pt}
makebox[4em]{Decision tree} & 0.72 & 0.25 & 0.56 & 0.34 & 0.46
hlinexrowht{20pt}
makebox[4em]{Random Forest} & 0.77 & 0.27 & 0.43 & 0.33 & 0.38
hlinexrowht{20pt}
makebox[4em]{XGBoost} & 0.71 & 0.27 & 0.75 & 0.4 & 0.56
hlinexrowht{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}

newcommandxrowht[2][0]{addstackgap[.5dimexpr#2relax]{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
hlinexrowht{20pt}
makebox[4em]{Logistic Regression} & 0.60 & 0.17 & 0.55 & 0.26 & 0.38
hlinexrowht{20pt}
makebox[4em]{Decision tree} & 0.72 & 0.25 & 0.56 & 0.34 & 0.46
hlinexrowht{20pt}
makebox[4em]{Random Forest} & 0.77 & 0.27 & 0.43 & 0.33 & 0.38
hlinexrowht{20pt}
makebox[4em]{XGBoost} & 0.71 & 0.27 & 0.75 & 0.4 & 0.56
hlinexrowht{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}

newcommandxrowht[2][0]{addstackgap[.5dimexpr#2relax]{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

2 Answers

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}
    newcommandxrowht[2][0]{addstackgap[.5dimexpr#2relax]{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
    hlinexrowht{20pt}
    Logistic Regression & 0.60 & 0.17 & 0.55 & 0.26 & 0.38
    hlinexrowht{20pt}
    Decision tree & 0.72 & 0.25 & 0.56 & 0.34 & 0.46
    hlinexrowht{20pt}
    Random Forest & 0.77 & 0.27 & 0.43 & 0.33 & 0.38
    hlinexrowht{20pt}
     XGBoost & 0.71 & 0.27 & 0.75 & 0.4 & 0.56
    hlinexrowht{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

Correct answer by Bernard on October 15, 2020

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

Answered by F. Pantigny on October 15, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP