7

I want to produce a table in which certain rows and columns are coloured, but I want the column colour to dominate. In my example, the row colour dominates. How can I achieve this?

\documentclass{article}
\usepackage{colortbl}
\usepackage{xcolor}
\begin{document}
\newcolumntype{g}{>{\columncolor{gray!30}}r}
\[
  \begin{array}{rrrgrrg}
  & & 1 & 2 & 3 & 4 & 5 \\ \hline
  \rowcolor{gray!70}
  2 & 4 & 3 & 0 & 2 & 1 & 1 \\
  \rowcolor{gray!70}
  3 & 2 & 1 & 1 & 1 & 1 & 0 \\
  2 & 5 & 0 & 6 & 0 & 2 & 3 \\
  3 & 2 & 2 & 3 & 5 & 0 & 1 \\
  \rowcolor{gray!70}
  2 & 3 & 1 & 0 & 2 & 1 & 0 \\
  \end{array}
\]
\newcolumntype{g}{>{\columncolor{gray!80}}r}
\[
  \begin{array}{rrrgrrg}
  & & 1 & 2 & 3 & 4 & 5 \\ \hline
  \rowcolor{gray!50}
  2 & 4 & 3 & 0 & 2 & 1 & 1 \\
  \rowcolor{gray!50}
  3 & 2 & 1 & 1 & 1 & 1 & 0 \\
  2 & 5 & 0 & 6 & 0 & 2 & 3 \\
  3 & 2 & 2 & 3 & 5 & 0 & 1 \\
  \rowcolor{gray!50}
  2 & 3 & 1 & 0 & 2 & 1 & 0 \\
  \end{array}
\]
\end{document}
  • 3
    use \cellcolor on the intersections. – David Carlisle Dec 30 '14 at 13:27
  • 1
    Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – karlkoeller Dec 30 '14 at 15:51
  • @DavidCarlisle Since it is a simple thing, why don't add an option columnsfirst (or whatever) to the package to do that? – karlkoeller Dec 30 '14 at 17:32
  • To ask a follow-up question, you have to open a fresh new question, with a link to this one. And, since my answer seems to solve this specific question, please consider to accept it. – karlkoeller Jan 4 '15 at 11:24
10

The colortbl package gives precedence to the color of rows than the color of columns.

But you can invert this behavior adding these lines to your preamble:

\usepackage{etoolbox}

\makeatletter
\patchcmd{\@classz}
  {\CT@row@color}
  {\oldCT@column@color}
  {}
  {}
\patchcmd{\@classz}
  {\CT@column@color}
  {\CT@row@color}
  {}
  {}
\patchcmd{\@classz}
  {\oldCT@column@color}
  {\CT@column@color}
  {}
  {}
\makeatother

MWE (I've removed your \hlines since they look ugly)

\documentclass{article}
\usepackage{colortbl}
\usepackage{xcolor}

\usepackage{etoolbox}

\makeatletter
\patchcmd{\@classz}
  {\CT@row@color}
  {\oldCT@column@color}
  {}
  {}
\patchcmd{\@classz}
  {\CT@column@color}
  {\CT@row@color}
  {}
  {}
\patchcmd{\@classz}
  {\oldCT@column@color}
  {\CT@column@color}
  {}
  {}
\makeatother

\begin{document}
\newcolumntype{g}{>{\columncolor{gray!30}}r}
\[
  \begin{array}{rrrgrrg}
  & & 1 & 2 & 3 & 4 & 5 \\ 
  \rowcolor{gray!70}
  2 & 4 & 3 & 0 & 2 & 1 & 1 \\
  \rowcolor{gray!70}
  3 & 2 & 1 & 1 & 1 & 1 & 0 \\
  2 & 5 & 0 & 6 & 0 & 2 & 3 \\
  3 & 2 & 2 & 3 & 5 & 0 & 1 \\
  \rowcolor{gray!70}
  2 & 3 & 1 & 0 & 2 & 1 & 0 \\
  \end{array}
\]
\[
  \begin{array}{rrrgrrg}
  & & 1 & 2 & 3 & 4 & 5 \\ 
  \rowcolor{gray!50}
  2 & 4 & 3 & 0 & 2 & 1 & 1 \\
  \rowcolor{gray!50}
  3 & 2 & 1 & 1 & 1 & 1 & 0 \\
  2 & 5 & 0 & 6 & 0 & 2 & 3 \\
  3 & 2 & 2 & 3 & 5 & 0 & 1 \\
  \rowcolor{gray!50}
  2 & 3 & 1 & 0 & 2 & 1 & 0 \\
  \end{array}
\]
\end{document} 

Output:

enter image description here

  • 1
    @Psionman You're welcome. Remember that you can accept the answer to mark the question as solved. See How do you accept an answer? – karlkoeller Dec 31 '14 at 9:31
  • This was just what I needed for a similar problem, great answer. Thanks – beacon_bonanza Jul 14 '15 at 13:28
  • @beacon_bonanza Happy that this answer helped you! – karlkoeller Jul 17 '15 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.