我有一张表格,我想给其中的行着色/突出显示。我正在使用\cellcolor
函数,但不知道为什么它不能正常工作。另外,请帮我在第一列的中间准确找到“正常”和“高”。谢谢!
\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}
答案1
彩色单元格中的白色条纹是由于使用makecell
单元格而导致的。删除它们,使用tabularx
表格环境(表格不比文本宽)可以解决您的问题。
\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}
(红线表示文本边框)
附录:
解决方案是使用tabularray
包来定义表格前言中行颜色的更复杂一些的方法。因此表格主体代码更简单。除此之外,单元格内容最好垂直放置:
\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}
(绿线表示页面布局)
答案2
我已加载nicematrix
、替换tabular
、NiceTabular
添加密钥,并使用提供的命令colortbl-like
替换。使用命令,您必须指定\multirow
\Block
nicematrix
\multirow
身体的行(这就是为什么你的例子中没有预期的输出),而使用命令\Block
,你可以指定行数逻辑行。
\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}
您需要多次编译(因为nicematrix
在后台使用 PGF/Tikz 节点)。