5
\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}

Here tried to give a different color in the first column by >{\columncolor{}} but the rowcolors{}{}{} always dominates the columncolor. Is there a way to replicate this MS Word table style?enter image description here

4 Answers4

6

You must be aware of the order of priority of clortbl package, so using \rowcolors the column color has no effect, but \cellcolor overrides to any other.

On the other hand:

  1. Never ever place a float in a center environment. Use \centering inside the float.

  2. Use tabulary better than tabularx is you hava first column with more content that the others and you do not have the wrong manias of fit the table to the full text width or make all columns of the same width.

  3. For numbers use siunitx for decimal allignement. If digits are very differen in every row (this case) the results is not very good anyway, but this is a problem of table design (bad businesses mix apples with oranges).

  4. Colors of course are just a matter of personal taste. But take care of avoid colors patterns that people with daltonism cannot see.

mwe

\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}

Fran
  • 80,769
4

A solution with 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}

P.S. = don't put a table environment within a center environment, see When should we use \begin{center} instead of \centering?.

enter image description here

CarLaTeX
  • 62,716
3

It is rather straightforward to get something like this with the nice matrix package, which neatly unifies the features of many table packages.

\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}

enter image description here

1

Another suggestion using tabularray package:

\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}

enter image description here

Stephen
  • 3,826
Zarko
  • 296,517