# Coloring rows and columns in a tabular environment

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?

-
There is some precedence set in terms of colour application. Using \cellcolor{gray!75} inside the affected cells (y_3 and y_5) overwrites what \rowcolors insert. – Werner Nov 2 '12 at 16:11
Unfortunately, this particular TeX is being generated by a rather intensive script. At that point, it'd be easier to simply have every 'affected' cell be colored this way. – Sean Allred Nov 2 '12 at 16:15
So it seems like you're after reversing the precedence here. That is, column colour should be written after the row colour (always)? – Werner Nov 2 '12 at 16:19
That would actually work, I think. Post it as an answer and it'll have a good chance of being marked :D – Sean Allred Nov 2 '12 at 16:22

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:

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

\makeatletter
\tmp#2!{#1}{#3}}

\def\tmp#1\CT@column@color\CT@row@color#2!#3#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}

-