3

I want to color the top row of my table. However, when I used the \rowcolor command, there are white spaces appearing in the second and third columns (cell is not filling completely with the color). How can I solve this? I am facing this problem over multiple tables. Is it because of a clash between some packages or something else?

\documentclass[11pt,a4paper,twoside,openrights,hidelinks]{book}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{multirow}
\usepackage{amsmath,amssymb}
\usepackage{setspace}
\usepackage{makecell}
\usepackage{tabu}
\usepackage{threeparttablex}
\usepackage{hhline}
\usepackage[RGYK]{xcolor}
\usepackage{colortbl}
\begin{document}
\begin{table}[t!]
    \renewcommand{\arraystretch}{1.3} 
    \centering
    \scriptsize
    \caption{Feeder energy losses for 10 days before and after phase switching of the loads for both phase switching algorithms.}
    \begin{threeparttable}
        \centering
        \begin{tabular}{|c|c|c|c|c|c|c|}
            \hline
            \rowcolor{gray!50}      \textbf{Feeder} & \multicolumn{3}{c|}{\thead{\thead{\textbf{Losses for} \textbf{Passive Network} \\ \textbf{(Scenario S1)}} \\ $[\textbf{kWh}]$}}& \multicolumn{3}{c|}{\thead{\thead{\textbf{Losses for} \textbf{Active Network} \\ \textbf{(Scenario S2)}} \\ $[\textbf{kWh}]$}}\\
            \hline
            & \textbf{BC} & \textbf{PSA-1} & \textbf{PSA-2} & \textbf{BC} & \textbf{PSA-1}  & \textbf{PSA-2}  \\ \hline
            \textit{Feeder 1}        &38.3  &30.7   (-19.8\%) &37.0  (-3.4\%)  &38.3  &30.7  (-19.8\%) &37.0  (-3.4\%)\\ \hline 
            \textit{Feeder 2}        &73.2  &53.2   (-27.5\%) &65.0  (-11.2\%) &71.1  &49.7  (-30.1\%) &61.1  (-14.1\%)\\ \hline
            \textit{Feeder} 3        &493.6 &278.1  (-43.6\%) &339.1 (-31.3\%) &487.0 &266.5 (-45.3\%) &332.0 (-31.8\%)\\ \hline 
            \textit{Network}         &605.1 &362.0  (-40.2\%) &441.1 (-27.1\%) &596.4 &346.9 (-41.8\%) &430.1 (-27.8\%)\\ \hline
            %       \end{tabularx}
        \end{tabular}
    \end{threeparttable}
    \begin{tablenotes}\footnotesize
        \item BC: Base Case 
    \end{tablenotes}
    \label{chp4_Table1}
\end{table}
\end{document}
  • seems like problem with thead. Is using tabularx an option? – nidhin Oct 17 at 9:51
  • @nidhin no, it is not possible as I have other tables as well throughout my document. I also fear that thead is creating the problem but I am sure how to solve it – Muhammad Usman Oct 17 at 10:14
3

You can play with the overhang optional arguments of \rowcolor (this is by trial and error and depends on the font size). I took the liberty to simplify your code: there's no need for nested \theads, and colortbl is automatically loaded by the table option of xcolor. Also, reducing the value of \tabcolsep, I could increase the font size (for better readability) without overflowing into the margin.

\documentclass[11pt,a4paper,twoside,openrights,hidelinks, english]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{babel}
\usepackage{multirow}
\usepackage{amsmath,amssymb}
\usepackage{setspace}
\usepackage{makecell}
\usepackage{tabu}
\usepackage{threeparttablex}
\usepackage{hhline}
\usepackage[RGYK, table]{xcolor}

\begin{document}

\begin{table}[t!]
    \renewcommand{\arraystretch}{1.3}
    \renewcommand{\theadfont}{\bfseries}
    \setlength{\tabcolsep}{3.25pt}
    \centering
    \footnotesize
    \begin{threeparttable}
    \caption{Feeder energy losses for 10 days before and after phase switching of the loads for both phase switching algorithms.}
        \begin{tabular}{|*{7}{c|}}
            \hline
            \rowcolor{gray!50}[\tabcolsep][2.5em]\textbf{Feeder} & \multicolumn{3}{c|}{\thead{Losses for Passive Network \\ (Scenario S1) \\[1ex] [kWh]}}& \multicolumn{3}{c|}{\thead{Losses for Active Network \\ (Scenario S2) \\[1ex] [kWh]}}
            \\ \hline
            & \textbf{BC} & \textbf{PSA-1} & \textbf{PSA-2} & \textbf{BC} & \textbf{PSA-1} & \textbf{PSA-2} \\ \hline
            \textit{Feeder 1} &38.3 &30.7 (-19.8\%) &37.0 (-3.4\%) &38.3 &30.7 (-19.8\%) &37.0 (-3.4\%)\\ \hline
            \textit{Feeder 2} &73.2 &53.2 (-27.5\%) &65.0 (-11.2\%) &71.1 &49.7 (-30.1\%) &61.1 (-14.1\%)\\ \hline
            \textit{Feeder} 3 &493.6 &278.1 (-43.6\%) &339.1 (-31.3\%) &487.0 &266.5 (-45.3\%) &332.0 (-31.8\%)\\ \hline
            \textit{Network} &605.1 &362.0 (-40.2\%) &441.1 (-27.1\%) &596.4 &346.9 (-41.8\%) &430.1 (-27.8\%)\\ \hline
        \end{tabular}
    \end{threeparttable}
    \begin{tablenotes}\footnotesize
        \item BC: Base Case
    \end{tablenotes}
    \label{chp4_Table1}
\end{table}

\end{document} 

enter image description here

3

\thead doesn't occupy complete column width, consequently you obtain white space. Instead using it I suggest to define new command:

\newcommand\mcccc[1]{\multicolumn{3}{>{\bfseries}c|}{%
                \begin{tabular}{ @{} >{\bfseries}c @{}} #1 \end{tabular}}}

which will eliminate white space:

\documentclass[11pt,a4paper,twoside,openrights,hidelinks]{book}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{multirow}
\usepackage{amsmath,amssymb}
\usepackage{setspace}
\usepackage{makecell}
\renewcommand\theadfont{\normalsize\bfseries}           %  <---
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}} % <---

\newcommand\mcccc[1]{\multicolumn{3}{>{\bfseries}c|}{%
                \begin{tabular}{ @{} >{\bfseries}c @{}} #1 \end{tabular}}}
\usepackage{tabu}  % not recomended, it is buggy
\usepackage{threeparttablex}
\usepackage{hhline}
\usepackage[RGYK, table]{xcolor}

\begin{document}
\begin{table}[t!]
    \renewcommand{\arraystretch}{1.3}
    \centering
    \scriptsize
    \caption{Feeder energy losses for 10 days before and after phase switching of the loads for both phase switching algorithms.}
    \begin{threeparttable}
        \centering
        \begin{tabular}{|*{7}{c|}}
            \hline
            \rowcolor{gray!30} 
        \textbf{Feeder} 
                & \mcccc{Losses for Passive Network\\
                        (Scenario S1)\\
                        {[kWh]} }
                    & \mcccc{Losses for Active Network\\
                            (Scenario S2)\\
                            {[kWh]}}                  \\
            \hline
            & \textbf{BC} & \textbf{PSA-1} & \textbf{PSA-2} & \textbf{BC} & \textbf{PSA-1}  & \textbf{PSA-2}  \\ \hline
            \textit{Feeder 1}        &38.3  &30.7   (-19.8\%) &37.0  (-3.4\%)  &38.3  &30.7  (-19.8\%) &37.0  (-3.4\%)\\ \hline
            \textit{Feeder 2}        &73.2  &53.2   (-27.5\%) &65.0  (-11.2\%) &71.1  &49.7  (-30.1\%) &61.1  (-14.1\%)\\ \hline
            \textit{Feeder} 3        &493.6 &278.1  (-43.6\%) &339.1 (-31.3\%) &487.0 &266.5 (-45.3\%) &332.0 (-31.8\%)\\ \hline
            \textit{Network}         &605.1 &362.0  (-40.2\%) &441.1 (-27.1\%) &596.4 &346.9 (-41.8\%) &430.1 (-27.8\%)\\ \hline
            %       \end{tabularx}
        \end{tabular}
    \end{threeparttable}
    \begin{tablenotes}\footnotesize
        \item BC: Base Case
    \end{tablenotes}
    \label{chp4_Table1}
\end{table}
\end{document}

enter image description here

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.