\documentclass{article}
\usepackage[utf8]{inputenc} % 'cp1252'-Western, 'cp1251'-Cyrillic, etc.
\usepackage[a4paper, left=1in, right=1in, top=1in, bottom=1in]{geometry}
\usepackage[english]{babel} % 'french', 'german', 'spanish', 'danish', etc.
\usepackage{txfonts}
\usepackage[classicReIm]{kpfonts}
\usepackage{array, tabularx, blindtext}
\usepackage[table]{xcolor}
\begin{document}
\blindtext[3]
\begin{center}
\begin{table}[h]
\centering
\caption{Caption}
\rowcolors{2}{lightgray!30}{}
\begin{tabularx}{0.5\textwidth}{>{\centering \columncolor{lightgray}}m{2.4cm}|>{\centering}X|>{\centering}X|>{\centering\arraybackslash}X} \hline
\rowcolor{lightgray}
\textbf{Unit Power (MW)} & \textbf{PSO} & \textbf{MFO} & \textbf{GA} \\ \hline\hline
\textbf{P1} & 445.89 & 426.09 & 474.81 \\ \hline
\textbf{P2} & 175.88 & 199.8 & 178.64 \\ \hline
\textbf{P3} & 258.63 & 247.5 & 262.21 \\ \hline
\textbf{P4} & 139.67 & 136.95 & 134.28 \\ \hline
\textbf{P5} & 165.61 & 166.24 & 151.9 \\ \hline
\textbf{P6} & 89.71 & 98.93 & 74.18 \\ \hline
\textbf{Total Power Generation (MW)} & 1275.39 & 1275.51 & 1276.02 \\ \hline
\textbf {Total Demand (MW)} & 1263 & 1263 & 1263 \\ \hline
\textbf {Power Loss (MW)} & 12.39 & 12.51 & 13.02 \\ \hline
\textbf {Total Generation Cost} & 15443 & 15448.72 & 15459 \\ \hline
\end{tabularx}
\end{table}
\end{center}
\end{document}
这里尝试通过 >{\columncolor{}} 在第一列中给出不同的颜色,但 rowcolors{}{}{} 总是主导 columncolor。有没有办法复制这种 MS Word 表格样式?
答案1
另一个使用tabularray
包的建议:
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{siunitx}
\usepackage{eurosym}
\begin{document}
\begin{table}
\centering
\sisetup{table-format=5.2,
group-minimum-digits=3}
\caption{Caption}
\begin{tblr}{hlines=red!50, vlines=red!50,
row{1} = {guard,bg=cyan!50},
row{odd[3]} = {bg=cyan!25},
colspec = { Q[r,cyan!50] *{3}{S} },
row{Z} = {guard},
}
Unit Power (MW) & {PSO} & {MFO} & {GA} \\
P1 & 445.89 & 426.09 & 474.81 \\
P2 & 175.88 & 199.8 & 178.64 \\
P3 & 258.63 & 247.5 & 262.21 \\
P4 & 139.67 & 136.95 & 134.28 \\
P5 & 165.61 & 166.24 & 151.9 \\
P6 & 89.71 & 98.93 & 74.18 \\
Total Power Generation
& 1275.39 & 1275.51 & 1276.02 \\
Total Demand & 1263 & 1263 & 1263 \\
Power Loss & 12.39 & 12.51 & 13.02 \\
Total Generation Cost (\euro)
& 15443 & 15448.72 & 15459 \\
\end{tblr}
\end{table}
\end{document}
答案2
您必须了解包的优先级顺序clortbl
,因此使用\rowcolors
列颜色不会产生任何效果,但\cellcolor
会覆盖任何其他颜色。
另一方面:
永远不要将浮子放在中心环境中。使用
\centering
里面浮点数。tabulary
更好的使用方法tabularx
是,让第一列包含比其他列更多的内容,并且不要错误地将表格调整为适合整个文本宽度或使所有列的宽度相同。对于数字,使用
siunitx
小数对齐。如果每行的数字差别很大(这种情况),结果无论如何都不是很好,但这是表格设计的问题(坏企业将苹果和橘子混在一起)。当然,颜色只是个人喜好问题。但要注意避免使用道尔顿症患者无法看到的颜色图案。
\documentclass{article}
\usepackage{array, tabulary}
\usepackage{siunitx}
\usepackage{eurosym}
\usepackage[table]{xcolor}
\begin{document}
\begin{table}
\centering
\caption{Caption}\extrarowheight1ex
\rowcolors{1}{yellow!05}{green!10}
\arrayrulecolor{green!50!red!30}
\begin{tabulary}{\linewidth}{|R|S[table-format=5.2]S[table-format=5.2]S[table-format=5.2]|} \hline
\rowcolor{green!20} Unit Power (MW) & {PSO} & {MFO} & {GA} \\ \hline
\cellcolor{green!20} P1 & 445.89 & 426.09 & 474.81 \\ \hline
\cellcolor{green!20} P2 & 175.88 & 199.8 & 178.64 \\\hline
\cellcolor{green!20} P3 & 258.63 & 247.5 & 262.21 \\\hline
\cellcolor{green!20} P4 & 139.67 & 136.95 & 134.28 \\ \hline
\cellcolor{green!20} P5 & 165.61 & 166.24 & 151.9 \\\hline
\cellcolor{green!20} P6 & 89.71 & 98.93 & 74.18 \\\hline
\cellcolor{green!20} Total Power Generation & 1275.39 & 1275.51 & 1276.02 \\ \hline
\cellcolor{green!20} Total Demand & 1263 & 1263 & 1263 \\ \hline
\cellcolor{green!20} Power Loss & 12.39 & 12.51 & 13.02 \\ \hline
\cellcolor{green!20} Total Generation Cost (\euro) & 15443 & 15448.72 & 15459 \\\hline
\end{tabulary}
\end{table}
\end{document}
答案3
解决方案如下tabularray
:
\documentclass{article}
%\usepackage[utf8]{inputenc} % no more needed
\usepackage[a4paper, left=1in, right=1in, top=1in, bottom=1in]{geometry}
\usepackage[english]{babel} % 'french', 'german', 'spanish', 'danish', etc.
\usepackage[classicReIm]{kpfonts} % moved before txfonts otherwise it gives error
\usepackage{txfonts}
\usepackage{array, tabularx, blindtext}
\usepackage[table]{xcolor}
\usepackage{tabularray}
\usepackage{caption}
\begin{document}
\blindtext[3]
\begin{table}[h]
\centering
\caption{Caption}
\begin{tblr}{
width={0.51\textwidth},% 0.51 instead of 0.5 to avoid "Overfull \hbox" warning
colspec={Q[c,m,2.4cm]*3{X[c]}}, vlines, hlines,
row{odd} = {bg=cyan!50!gray!10},
row{even} = {bg=gray!30!cyan!40},
column{1}={bg={-red!75!green!80},font=\bfseries},
row{1}={bg={-red!75!green!80},font=\bfseries},
}
Unit Power (MW) & PSO & MFO & GA \\
P1 & 445.89 & 426.09 & 474.81 \\
P2 & 175.88 & 199.8 & 178.64 \\
P3 & 258.63 & 247.5 & 262.21 \\
P4 & 139.67 & 136.95 & 134.28 \\
P5 & 165.61 & 166.24 & 151.9 \\
P6 & 89.71 & 98.93 & 74.18 \\
Total Power Generation (MW) & 1275.39 & 1275.51 & 1276.02 \\
Total Demand (MW) & 1263 & 1263 & 1263\\
Power Loss (MW) & 12.39 & 12.51 & 13.02 \\
Total Generation Cost & 15443 & 15448.72 & 15459 \\
\end{tblr}
\end{table}
\end{document}
PS = 不要将 table 环境放在 center 环境内,请参阅 何时应使用 \begin{center} 而不是 \centering?。
答案4
使用该nice matrix
包获得类似的东西相当简单,它巧妙地统一了许多表格包的功能。
\documentclass{article}
\usepackage[utf8]{inputenc} % 'cp1252'-Western, 'cp1251'-Cyrillic, etc.
\usepackage[a4paper, left=1in, right=1in, top=1in, bottom=1in]{geometry}
\usepackage{nicematrix}
\begin{document}
\begin{table}[h]
\centering
\caption{Colors with the \texttt{nicematrix} package.}
\begin{NiceTabularX}{0.5\textwidth}{>{\centering\bfseries}m{2.4cm}|>{\centering}X|>{\centering}X|>{\centering\arraybackslash}X}[hvlines]
\CodeBefore
\rowcolors{2}{blue!30}{blue!20}
\rectanglecolor{blue!80}{1-1}{11-1}
\rectanglecolor{blue!80}{1-1}{1-4}
\Body
Unit Power (MW) & \textbf{PSO} & \textbf{MFO} & \textbf{GA} \\
P1 & 445.89 & 426.09 & 474.81 \\
P2 & 175.88 & 199.8 & 178.64 \\
P3 & 258.63 & 247.5 & 262.21 \\
P4 & 139.67 & 136.95 & 134.28 \\
P5 & 165.61 & 166.24 & 151.9 \\
P6 & 89.71 & 98.93 & 74.18 \\
Total Power Generation (MW) & 1275.39 & 1275.51 & 1276.02 \\
Total Demand (MW) & 1263 & 1263 & 1263 \\
Power Loss (MW) & 12.39 & 12.51 & 13.02 \\
Total Generation Cost & 15443 & 15448.72 & 15459 \\
\end{NiceTabularX}
\end{table}
\end{document}