3

I have a table that I would like to color/highlight its rows. I am using \cellcolor function but have no idea why it is not working properly. Also, please help me to locate "Normal" and "High" exactly in the middle of the first column. Thanks!

enter image description here

\documentclass[11pt]{article}

\usepackage[utf8]{inputenc}
\usepackage[a4paper,margin=2.5cm]{geometry}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{makecell}
\usepackage[fleqn]{amsmath}
\usepackage[table]{xcolor}
\usepackage{color,soul}

\begin{document}

\begin{table}[h!]
    \centering
    \caption{Comparison of performance metrics for heuristic scheduling rules}
    \begin{tabular}{l|l|l l l l l}
    \hline
        \multirow{2}{*}{Inst.} & \multirow{2}{*}{Demand} & \multicolumn{5}{c}{Heuristic scheduling rules} \\ \cline{3-7}
                               &  & Myopic & PPQ & DMB & AOP & EPPQ \\\hline
        \multirow{5}{*}{Normal} & \cellcolor{gray!30}1    & \cellcolor{gray!30}\makecell[l]{34.1 \\ $[4.6, 0, 0.2]$} & \cellcolor{gray!30} \makecell[l]{\color{blue}{11.4} \\ $[2.1, 0, 0.4]$} & \cellcolor{gray!30} \makecell[l]{18.2 \\ $[2.8, 0, 0.6]$} & \cellcolor{gray!30}\makecell[l]{11.5 \\ $[2.1, 0, 0.4]$} & \cellcolor{gray!30}\makecell[l]{11.5 \\ $[2.1, 0, 0.4]$} \\
                                & 2    & \makecell[l]{84.7 \\ $[6.6, 0, 3.4]$} & \makecell[l]{44.0 \\ $[2.9, 0, 2.1]$} & \makecell[l]{44.1 \\ $[2.9, 0, 2.1]$} & \makecell[l]{42.8 \\ $[2.2, 0, 1.7]$} & \makecell[l]{44.3 \\ $[2.9, 0, 3.2]$} \\
                                & \cellcolor{gray!30}3        & \cellcolor{gray!30}\makecell[l]{124.6 \\ $[7.5, 11.7, 0.2]$} & \cellcolor{gray!30}\makecell[l]{47.3 \\ $[3.8, 6.7, 0.2]$} & \cellcolor{gray!30}\makecell[l]{51.9 \\ $[4.1, 7.3, 0.2]$} & \cellcolor{gray!30}\makecell[l]{11.8 \\ $[2.3, 0.6, 0.3]$} & \cellcolor{gray!30}\makecell[l]{12.4 \\ $[2.4, 1.0, 0.4]$} \\
                               &  4    & \makecell[l]{ \\ $[, , ]$} & \makecell[l]{ \\ $[, , ]$} & \makecell[l]{ \\ $[, , ]$} & \makecell[l]{ \\ $[, , ]$} & \makecell[l]{ \\ $[, , ]$} \\
                               &  \cellcolor{gray!30}5    & \cellcolor{gray!30}\makecell[l]{ \\ $[, , ]$} & \cellcolor{gray!30}\makecell[l]{ \\ $[, , ]$} & \cellcolor{gray!30}\makecell[l]{ \\ $[, , ]$} & \cellcolor{gray!30}\makecell[l]{ \\ $[, , ]$} & \cellcolor{gray!30}\makecell[l]{ \\ $[, , ]$} \\\hline
        \multirow{5}{*}{High} & 1    & \makecell[l]{138.0 \\ $[6.9, 0, 2.6]$} & \makecell[l]{79.3 \\ $[2.9, 0, 2.6]$} & \makecell[l]{79.7 \\ $[2.9, 0, 2.7]$} & \makecell[l]{79.5 \\ $[2.9, 0, 2.6]$} & \makecell[l]{79.8 \\ $[2.9, 0, 2.7]$} \\
                                & \cellcolor{gray!30}2    & \cellcolor{gray!30}\makecell[l]{104.96 \\ $[6.8, 0, 6.0]$} & \cellcolor{gray!30}\makecell[l]{61.5 \\ $[2.9, 0, 4.4]$} & \cellcolor{gray!30}\makecell[l]{61.6 \\ $[3.0, 0, 4.4]$} & \cellcolor{gray!30}\makecell[l]{59.9 \\ $[2.6, 0, 3.9]$} & \cellcolor{gray!30}\makecell[l]{61.0 \\ $[2.9, 0, 4.5]$} \\
                                & 3        &  \makecell[l]{225.5 \\ $[8.9, 16.6, 2.2]$} & \makecell[l]{154.5 \\ $[4.9, 16.2, 2.2]$} & \makecell[l]{154.7 \\ $[4.9, 16.2, 2.2]$} & \makecell[l]{83.0 \\ $[2.9, 4.6, 2.3]$} & \makecell[l]{79.2 \\ $[3.0, 4.5, 2.1]$} \\
                                & \cellcolor{gray!30}4    & \cellcolor{gray!30}\makecell[l]{ \\ $[, , ]$} & \cellcolor{gray!30}\makecell[l]{ \\ $[, , ]$} & \cellcolor{gray!30}\makecell[l]{ \\ $[, , ]$} & \cellcolor{gray!30}\makecell[l]{ \\ $[, , ]$} & \cellcolor{gray!30}\makecell[l]{ \\ $[, , ]$} \\
                                &  5    & \makecell[l]{ \\ $[, , ]$} & \makecell[l]{ \\ $[, , ]$} & \makecell[l]{ \\ $[, , ]$} & \makecell[l]{ \\ $[, , ]$} & \makecell[l]{ \\ $[, , ]$} \\\hline
    \multicolumn{7}{l}{\makecell[l]{Performance metrics consist of the immediate cost and [wait time of scheduled patients, number of \\ referrals, number of rejections] - average values.}}
    \end{tabular}
    \label{tabCOM}
\end{table}

\end{document}

2 Answers 2

1

White strips in colored cells are caused by use of makecell in ceLls. Removing them, using tabularx table environment (that table is not wider than text) solve your problem.

\documentclass[11pt]{article}
\usepackage[a4paper,margin=2.5cm]{geometry}
\usepackage[table]{xcolor}
%--------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%
\usepackage{lipsum}                             % for dummy text
%---------------------------------------------------------------%
\usepackage{graphicx}
\usepackage{multirow, tabularx}
\usepackage[fleqn]{amsmath}
\usepackage{soul}

\begin{document}

\begin{table}[h!]
\caption{Comparison of performance metrics for heuristic scheduling rules}
    \label{tabCOM}

\begin{tabularx}{\linewidth}{l|c| *{5}{X}}
    \hline
\multirow{2}{*}{Inst.} 
    & \multirow{2}{*}{Demand} 
        & \multicolumn{5}{c}{Heuristic scheduling rules}    \\   \cline{3-7}
            &   & Myopic    & PPQ   & DMB   & AOP   & EPPQ  \\  \hline
    \rowcolor{gray!30}
\cellcolor{white}
    & 1    
        &   34.1\par $[4.6, 0, 0.2]$ 
            &   \textcolor{blue}{11.4}\par $[2.1, 0, 0.4]$
                &  18.2\par $[2.8, 0, 0.6]$
                    &   11.5\par $[2.1, 0, 0.4]$
                        &   11.5\par $[2.1, 0, 0.4]$        \\
    & 2 &   84.7\par $[6.6, 0, 3.4]$
            &   44.0\par $[2.9, 0, 2.1]$
                &   44.1\par $[2.9, 0, 2.1]$
                    &   42.8\par $[2.2, 0, 1.7]$  
                        &   44.3\par $[2.9, 0, 3.2]$         \\
    \rowcolor{gray!30}
\cellcolor{white}
    &   &   124.6\par $[7.5, 11.7, 0.2]$
            &   47.3\par $[3.8, 6.7, 0.2]$
                &   51.9\par $[4.1, 7.3, 0.2]$
                    &   11.8\par $[2.3, 0.6, 0.3]$
                        &   12.4\par $[2.4, 1.0, 0.4]$      \\
    & 4 &   ~\par $[, , ]$ 
            &   ~\par $[, , ]$
                &   ~\par $[, , ]$
                    &   ~\par $[, , ]$
                        &   ~\par $[, , ]$                  \\
    \rowcolor{gray!30}
\cellcolor{white}
\multirow{-10}{*}{Normal}
    & 5 &   ~\par $[, , ]$
            &   ~\par $[, , ]$
                &   ~\par $[, , ]$  
                    &   ~\par $[, , ]$  
                        &   ~\par $[, , ]$              \\
    \hline
    & 1 &   138.0\par $[6.9, 0, 2.6]$
            &   79.3\par $[2.9, 0, 2.6]$
                &   79.7\par $[2.9, 0, 2.7]$
                    &   79.5\par $[2.9, 0, 2.6]$
                        &   79.8\par $[2.9, 0, 2.7]$    \\
    \rowcolor{gray!30}
\cellcolor{white}
    & 2 &   104.96   $[6.8, 0, 6.0]$
            &   61.5     $[2.9, 0, 4.4]$
                &   61.6\par $[3.0, 0, 4.4]$
                    &   59.9\par $[2.6, 0, 3.9]$
                        &   61.0\par $[2.9, 0, 4.5]$     \\
    & 3 &   225.5\par $[8.9, 16.6, 2.2]$
            &   154.5\par $[4.9, 16.2, 2.2]$ 
                &   154.7\par $[4.9, 16.2, 2.2]$
                    &   83.0\par $[2.9, 4.6, 2.3]$
                        &   79.2\par $[3.0, 4.5, 2.1]$      \\
    \rowcolor{gray!30}
\cellcolor{white}
    & 4 &   ~\par $[, , ]$
            &   ~\par $[, , ]$
                &   ~\par $[, , ]$
                    &   ~\par $[, , ]$
                        &   ~\par $[, , ]$                   \\
\multirow{-10}{*}{High}
    & 5 &   ~\par $[, , ]$
            &   ~\par $[, , ]$
                &   ~\par $[, , ]$
                    &   ~\par $[, , ]$
                        &   ~\par $[, , ]$                   \\
    \hline
\multicolumn{7}{@{} p{\linewidth} @{}}{Performance metrics consist of the immediate cost and [wait time of scheduled patients, number of referrals, number of rejections] - average values.}
\end{tabularx}
    \end{table}
\end{document}

first example

(red lines indicate text borders)

Addendum: The solution which use tabularray package with defining a bit more sophisticated way for rows coloring in table preamble. Consequently table's body code is simpler. Beside this cells contents is beter vertical positioned:

\documentclass[11pt]{article}
\usepackage[a4paper,margin=2.5cm]{geometry}
\usepackage{xcolor}
%--------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{teal}}
%---------------------------------------------------------------%
\usepackage{graphicx}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\usepackage[fleqn]{amsmath}
\usepackage{soul}

\begin{document}

\begin{table}[ht]
\ExplSyntaxOn   % rule for table coloring
\NewChildSelector{eachtwocolor}
  {
  \int_step_inline:nnnn {3}{2}{\l_tblr_childs_total_tl}
      { \clist_put_right:Nn \l_tblr_childs_clist {##1} }
  }
\ExplSyntaxOff

\caption{Comparison of performance metrics for heuristic scheduling rules}
    \label{tabCOM}

\begin{tblr}{row{eachtwocolor} = {bg=red!15},   % set color of your choice
             row{Z} = {bg=white},
             colspec  = {Q[l, bg=white] |c| *{5}{X[l]}},
             hspan=minimal,
             }
    \toprule
\SetCell[r=2]{c}    Inst.
    &   \SetCell[r=2]{c}    Demand
        &   \SetCell[c=5]{c}    Heuristic scheduling rules
            &       &       &       &       &       \\
    \midrule
    &   & Myopic    & PPQ   & DMB   & AOP   & EPPQ  \\
    \midrule[0.7pt]
\SetCell[r=5]{c}   Normal
    & 1 &   {34.1\\  $[4.6, 0, 0.2]$}
            &  { \textcolor{blue}{11.4}\\  $[2.1, 0, 0.4]$}
                &  {18.2\\  $[2.8, 0, 0.6]$}
                    &   {11.5\\  $[2.1, 0, 0.4]$}
                        &   {11.5\\  $[2.1, 0, 0.4]$}      \\
    & 2 &   {84.7\\  $[6.6, 0, 3.4]$}
            &   {44.0\\  $[2.9, 0, 2.1]$}
                &   {44.1\\  $[2.9, 0, 2.1]$}
                    &   {42.8\\  $[2.2, 0, 1.7]$}
                        &   {44.3\\  $[2.9, 0, 3.2]$}      \\
    & 3 &   {124.6\\  $[7.5, 11.7, 0.2]$}
            &   {47.3\\  $[3.8, 6.7, 0.2]$}
                &   {51.9\\  $[4.1, 7.3, 0.2]$}
                    &   {11.8\\  $[2.3, 0.6, 0.3]$}
                        &   {12.4\\  $[2.4, 1.0, 0.4]$}      \\
    & 4 &   {~\\  $[, , ]$}
            &   {~\\  $[, , ]$}
                &   {~\\  $[, , ]$}
                    &   {~\\  $[, , ]$}
                        &   {~\\  $[, , ]$}                 \\
    & 5 &   {~\\  $[, , ]$}
            &   {~\\  $[, , ]$}
                &   {~\\  $[, , ]$}
                    &   {~\\  $[, , ]$}
                        &   {~\\  $[, , ]$}              \\
    \midrule
\SetCell[r=5]{c}   High
    & 1 &   {138.0\\  $[6.9, 0, 2.6]$}
            &   {79.3\\  $[2.9, 0, 2.6]$}
                &   {79.7\\  $[2.9, 0, 2.7]$}
                    &   {79.5\\  $[2.9, 0, 2.6]$}
                        &   {79.8\\  $[2.9, 0, 2.7]$}    \\
    & 2 &   {104.96   $[6.8, 0, 6.0]$}
            &   {61.5     $[2.9, 0, 4.4]$}
                &   {61.6\\  $[3.0, 0, 4.4]$}
                    &   {59.9\\  $[2.6, 0, 3.9]$}
                        &   {61.0\\  $[2.9, 0, 4.5]$}     \\
    & 3 &   {225.5\\  $[8.9, 16.6, 2.2]$}
            &   {154.5\\  $[4.9, 16.2, 2.2]$}
                &   {154.7\\  $[4.9, 16.2, 2.2]$}
                    &   {83.0\\  $[2.9, 4.6, 2.3]$}
                        &   {79.2\\  $[3.0, 4.5, 2.1]$}      \\
    & 4 &   {~\\  $[, , ]$}
            &   {~\\  $[, , ]$}
                &   {~\\  $[, , ]$}
                    &   {~\\  $[, , ]$}
                        &   {~\\  $[, , ]$}                   \\
    & 5 &   {~\\  $[, , ]$}
            &   {~\\  $[, , ]$}
                &   {~\\  $[, , ]$}
                    &   {~\\  $[, , ]$}
                        &   {~\\  $[, , ]$}                   \\
    \midrule[1pt]
\SetCell[c=7]{j}   Performance metrics consist of the immediate cost and
                   [wait time of scheduled patients, number of referrals, number of rejections]
                   - average values.
    &   &   &   &   &   &                                   
\end{tblr}
    \end{table}
\end{document}

second example

(green lines indicates page layout)

1

I have loaded nicematrix, replaced tabular by NiceTabular, added the key colortbl-like and replaced the \multirow by the command \Block provided by nicematrix. With the command \multirow, you have to specify the number of physical rows (and that's why you have not the expected output in your example) whereas with the command \Block, you specify the number of logical rows.

\documentclass[11pt]{article}
\usepackage[a4paper,margin=2.5cm]{geometry}
\usepackage{graphicx}
\usepackage{makecell}
\usepackage[fleqn]{amsmath}
\usepackage{xcolor}
\usepackage{color,soul}
\usepackage{multirow}

\usepackage{nicematrix}

\begin{document}

\begin{table}[h!]
    \centering
    \caption{Comparison of performance metrics for heuristic scheduling rules}
    \begin{NiceTabular}{l|l|l l l l l}[colortbl-like]
    \hline
        \Block{2-1}{Inst.} & \multirow{2}{*}{Demand} & \multicolumn{5}{c}{Heuristic scheduling rules} \\ \cline{3-7}
                               &  & Myopic & PPQ & DMB & AOP & EPPQ \\\hline
        \Block{5-1}{Normal} & \cellcolor{gray!30}1    & \cellcolor{gray!30}\makecell[l]{34.1 \\ $[4.6, 0, 0.2]$} & \cellcolor{gray!30} \makecell[l]{\color{blue}{11.4} \\ $[2.1, 0, 0.4]$} & \cellcolor{gray!30} \makecell[l]{18.2 \\ $[2.8, 0, 0.6]$} & \cellcolor{gray!30}\makecell[l]{11.5 \\ $[2.1, 0, 0.4]$} & \cellcolor{gray!30}\makecell[l]{11.5 \\ $[2.1, 0, 0.4]$} \\
                                & 2    & \makecell[l]{84.7 \\ $[6.6, 0, 3.4]$} & \makecell[l]{44.0 \\ $[2.9, 0, 2.1]$} & \makecell[l]{44.1 \\ $[2.9, 0, 2.1]$} & \makecell[l]{42.8 \\ $[2.2, 0, 1.7]$} & \makecell[l]{44.3 \\ $[2.9, 0, 3.2]$} \\
                                & \cellcolor{gray!30}3        & \cellcolor{gray!30}\makecell[l]{124.6 \\ $[7.5, 11.7, 0.2]$} & \cellcolor{gray!30}\makecell[l]{47.3 \\ $[3.8, 6.7, 0.2]$} & \cellcolor{gray!30}\makecell[l]{51.9 \\ $[4.1, 7.3, 0.2]$} & \cellcolor{gray!30}\makecell[l]{11.8 \\ $[2.3, 0.6, 0.3]$} & \cellcolor{gray!30}\makecell[l]{12.4 \\ $[2.4, 1.0, 0.4]$} \\
                               &  4    & \makecell[l]{ \\ $[, , ]$} & \makecell[l]{ \\ $[, , ]$} & \makecell[l]{ \\ $[, , ]$} & \makecell[l]{ \\ $[, , ]$} & \makecell[l]{ \\ $[, , ]$} \\
                               &  \cellcolor{gray!30}5    & \cellcolor{gray!30}\makecell[l]{ \\ $[, , ]$} & \cellcolor{gray!30}\makecell[l]{ \\ $[, , ]$} & \cellcolor{gray!30}\makecell[l]{ \\ $[, , ]$} & \cellcolor{gray!30}\makecell[l]{ \\ $[, , ]$} & \cellcolor{gray!30}\makecell[l]{ \\ $[, , ]$} \\\hline
        \Block{5-1}{High} & 1    & \makecell[l]{138.0 \\ $[6.9, 0, 2.6]$} & \makecell[l]{79.3 \\ $[2.9, 0, 2.6]$} & \makecell[l]{79.7 \\ $[2.9, 0, 2.7]$} & \makecell[l]{79.5 \\ $[2.9, 0, 2.6]$} & \makecell[l]{79.8 \\ $[2.9, 0, 2.7]$} \\
                                & \cellcolor{gray!30}2    & \cellcolor{gray!30}\makecell[l]{104.96 \\ $[6.8, 0, 6.0]$} & \cellcolor{gray!30}\makecell[l]{61.5 \\ $[2.9, 0, 4.4]$} & \cellcolor{gray!30}\makecell[l]{61.6 \\ $[3.0, 0, 4.4]$} & \cellcolor{gray!30}\makecell[l]{59.9 \\ $[2.6, 0, 3.9]$} & \cellcolor{gray!30}\makecell[l]{61.0 \\ $[2.9, 0, 4.5]$} \\
                                & 3        &  \makecell[l]{225.5 \\ $[8.9, 16.6, 2.2]$} & \makecell[l]{154.5 \\ $[4.9, 16.2, 2.2]$} & \makecell[l]{154.7 \\ $[4.9, 16.2, 2.2]$} & \makecell[l]{83.0 \\ $[2.9, 4.6, 2.3]$} & \makecell[l]{79.2 \\ $[3.0, 4.5, 2.1]$} \\
                                & \cellcolor{gray!30}4    & \cellcolor{gray!30}\makecell[l]{ \\ $[, , ]$} & \cellcolor{gray!30}\makecell[l]{ \\ $[, , ]$} & \cellcolor{gray!30}\makecell[l]{ \\ $[, , ]$} & \cellcolor{gray!30}\makecell[l]{ \\ $[, , ]$} & \cellcolor{gray!30}\makecell[l]{ \\ $[, , ]$} \\
                                &  5    & \makecell[l]{ \\ $[, , ]$} & \makecell[l]{ \\ $[, , ]$} & \makecell[l]{ \\ $[, , ]$} & \makecell[l]{ \\ $[, , ]$} & \makecell[l]{ \\ $[, , ]$} \\\hline
    \multicolumn{7}{l}{\makecell[l]{Performance metrics consist of the immediate cost and [wait time of scheduled patients, number of \\ referrals, number of rejections] - average values.}}
    \end{NiceTabular}
    \label{tabCOM}
\end{table}

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

4
  • It's interesting that I cannot get a similar outcome using overleaf! Also, would you recommend any other way of creating the table that requires less compilation? Plus, I don't want to bold the horizontal lines of the table. Thanks.
    – mdslt
    Aug 22, 2022 at 22:42
  • I had forgotten a line in the code. I have modified my answer. Aug 22, 2022 at 22:55
  • 1
    The code of my answer compiles on Overleaf (with the version 2022 of TeXLive). Overleaf does the right number of compilations for you. The horizontal rules are not bold. In the picture I have uploaded, some rules seem bolder than others, but it's only an artifact of the screen capture. Aug 22, 2022 at 23:00
  • Thanks for the explanation! I just copied and pasted your code in an empty project, it is interesting that I still face errors: "Package nicematrix Error: The option 'colortbl-like' is unknown for the"
    – mdslt
    Aug 22, 2022 at 23:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .