[Tex/LaTex] Coloring rows and columns in a tabular environment

colortables

I'm trying to put together a series of tables that show row reduction with simplex method (operations research concept). I'm trying to highlight the rows and columns used in simplex method, and these specific rows and columns change all the time. I've attempted the following:

\documentclass[12pt]{article}
\usepackage[margin=1cm,landscape,paperwidth=3in,paperheight=3in]{geometry}
\usepackage{amsmath,booktabs,colortbl}
\usepackage[table]{xcolor}
\pagestyle{empty}

\begin{document}
\newcolumntype{M}{>{$}c<{$}}
\newcolumntype{P}{>{\columncolor{gray!75}}M}
\rowcolors{2}{gray!25}{}
\begin{tabular}{|MPM|}
\hline
x_1 & y_1 & z_1 \\
x_2 & y_2 & z_2 \\
x_3 & y_3 & z_3 \\
x_4 & y_4 & z_4 \\
x_5 & y_5 & z_5 \\
x_6 & y_6 & z_6 \\
\hline
\end{tabular}
\end{document}

but it seems that each call to \rowcolor overrides the column color specified. Is there anyway to work around this behavior?

Best Answer

colortbl fairly arbitrarily gives row color precedence over column color, if you wish to reverse that decision you just need to change the order of \CT@column@color and \CT@row@color You can do this by applying a patch macro as below:

enter image description here

\documentclass[12pt]{article}
\usepackage[margin=1cm,landscape,paperwidth=3in,paperheight=3in]{geometry}
\usepackage{amsmath,booktabs,colortbl}
\usepackage[table]{xcolor}

\makeatletter
\def\tmpp#1\@addtopreamble#2#3!{%
    \tmp#2!{#1}{#3}}



\def\tmp#1\CT@column@color\CT@row@color#2!#3#4{%
\def\@classz{#3\@addtopreamble{#1\CT@row@color\CT@column@color#2}#4}}

\expandafter\tmpp\@classz!
\makeatother
\pagestyle{empty}

\begin{document}
\newcolumntype{M}{>{$}c<{$}}
\newcolumntype{P}{>{\columncolor{gray!75}}M}
\rowcolors{2}{gray!25}{}
\begin{tabular}{|MPM|}
\hline
x_1 & y_1 & z_1 \\
x_2 & y_2 & z_2 \\
x_3 & y_3 & z_3 \\
x_4 & y_4 & z_4 \\
x_5 & y_5 & z_5 \\
x_6 & y_6 & z_6 \\
\hline
\end{tabular}
\end{document}
Related Question