我想为表格的顶行着色。但是,当我使用 \rowcolor 命令时,第二列和第三列会出现空白(单元格未完全填充颜色)。我该如何解决这个问题?我在多个表格上都遇到了这个问题。是因为某些包之间发生冲突还是其他原因?
\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}
答案1
您可以使用overhang
的可选参数\rowcolor
(这是通过反复试验得出的,取决于字体大小)。我擅自简化了您的代码:不需要嵌套\thead
s,并且可以通过选项colortbl
自动加载。此外,通过降低 的值,我可以增加字体大小(以提高可读性),而不会溢出到边距。table
xcolor
\tabcolsep
\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}
答案2
\thead
不占据整个列宽,因此您将获得空白。我建议改为定义新命令:
\newcommand\mcccc[1]{\multicolumn{3}{>{\bfseries}c|}{%
\begin{tabular}{ @{} >{\bfseries}c @{}} #1 \end{tabular}}}
这将消除空白:
\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}
答案3
如果您使用{NiceTabular}
及其nicematrix
键color-inside
,则可直接获得预期输出(我也使用了键hvlines
)来绘制所有规则。
\documentclass[11pt,a4paper,twoside,openrights,hidelinks]{book}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[english]{babel}
\usepackage{amsmath,amssymb}
\usepackage{setspace}
\usepackage{makecell}
\usepackage{threeparttablex}
\usepackage{xcolor}
\usepackage{nicematrix}
\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{NiceTabular}{ccccccc}[color-inside,hvlines]
\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}]$}}\\
& \textbf{BC} & \textbf{PSA-1} & \textbf{PSA-2} & \textbf{BC} & \textbf{PSA-1} & \textbf{PSA-2} \\
\textit{Feeder 1} &38.3 &30.7 (-19.8\%) &37.0 (-3.4\%) &38.3 &30.7 (-19.8\%) &37.0 (-3.4\%)\\
\textit{Feeder 2} &73.2 &53.2 (-27.5\%) &65.0 (-11.2\%) &71.1 &49.7 (-30.1\%) &61.1 (-14.1\%)\\
\textit{Feeder} 3 &493.6 &278.1 (-43.6\%) &339.1 (-31.3\%) &487.0 &266.5 (-45.3\%) &332.0 (-31.8\%)\\
\textit{Network} &605.1 &362.0 (-40.2\%) &441.1 (-27.1\%) &596.4 &346.9 (-41.8\%) &430.1 (-27.8\%)\\
\end{NiceTabular}
\end{threeparttable}
\begin{tablenotes}\footnotesize
\item BC: Base Case
\end{tablenotes}
\label{chp4_Table1}
\end{table}
\end{document}