4

I made a table with the following code and the hhline package:

\documentclass{article}
\usepackage{hhline}
\begin{document}
\begin{tabular}{l || l l l l l | l}
& 1 & 2 & 3 & 4 & 5 & 6\\\hhline{=#=====|=}
1 & 1 & 1 & 1 & 1 & 1 & 0\\
2 & 1 & 1 & 1 & 1 & 1 & 0\\
3 & 1 & 1 & 1 & 1 & 1 & 0\\
4 & 1 & 1 & 1 & 1 & 1 & 0\\
5 & 1 & 1 & 1 & 1 & 1 & 0\\\hline
6 & 0 & 0 & 0 & 0 & 0 & 1
\end{tabular}
\end{document}

enter image description here

Now, I would like to colour the 5×5 block in a specific colour, and the 1×1 block in another colour. The other cells should remain as they are.

I have seen Color merged and regular cells in a table individually which suggests \cellcolor, \rowcolor and columncolor - however, in this case I could only use \cellcolor, since there are no full rows or columns. But that would mean I have to type 26 times \cellcolor. Is there no efficient way to do this (either algorithmically or by colouring a whole block at once)?

Keelan
  • 5,425
  • 1
    While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. – Peter Grill Nov 21 '14 at 18:49
  • @PeterGrill I'm sorry, I edited my question. – Keelan Nov 21 '14 at 18:52

3 Answers3

4
  1. Manual Solution: You can apply \rowcolor and selectively color in white the cells which should not have color

    enter image description here

  2. \ColorBlock{<start row>}{<end row>}{<color>} Using collcell package we can define a macro which specifies which rows of the table to color. This applies only to columns with the L alignment defined in the MWE.

    enter image description here


Code:

\documentclass{article}

\usepackage{hhline}
\usepackage{xcolor}
\usepackage{colortbl}

\newcommand{\RowColor}{\rowcolor{red!50} \cellcolor{white}}

\begin{document}
\begin{tabular}{l || l l l l l | l}
                  & 1 & 2 & 3 & 4 & 5 & \cellcolor{yellow}6 \\\hhline{=#=====|=}
\RowColor       1 & 1 & 1 & 1 & 1 & 1 & \cellcolor{white}0  \\
\RowColor       2 & 1 & 1 & 1 & 1 & 1 & \cellcolor{white}0  \\
\RowColor       3 & 1 & 1 & 1 & 1 & 1 & \cellcolor{white}0  \\
\RowColor       4 & 1 & 1 & 1 & 1 & 1 & \cellcolor{white}0  \\
\RowColor       5 & 1 & 1 & 1 & 1 & 1 & \cellcolor{white}0  \\\hline
\cellcolor{cyan}6 & 0 & 0 & 0 & 0 & 0 & \cellcolor{green}1
\end{tabular}
\end{document}

Code: \ColorBlock{<start row>}{<end row>}{<color>}

\documentclass{article}

\usepackage{hhline}
\usepackage{xcolor}
\usepackage{colortbl}

\usepackage{collcell}

\newcounter{CurrentRow}
\newcommand*{\StartRow}{1}%
\newcommand*{\EndRow}{1}%
\newcommand*{\CellColor}{white}%
\newcommand{\ColorBlock}[3]{%
    \renewcommand*{\StartRow}{#1}%
    \renewcommand*{\EndRow}{#2}%
    \renewcommand*{\CellColor}{#3}%
}

\newcommand*{\ApplyCellColor}[1]{%
    \ifnum\arabic{CurrentRow}>\numexpr\StartRow-1\relax
        \ifnum\arabic{CurrentRow}<\numexpr\EndRow+1\relax
            \cellcolor{\CellColor}#1%
        \else
            #1%
        \fi
    \else
        #1%
    \fi
}
\newcolumntype{L}{>{\collectcell\ApplyCellColor}{l}<{\endcollectcell}}%%  for left alignment
%\newcolumntype{C}{>{\collectcell\ApplyCellColor}{c}<{\endcollectcell}}%% if need center alignment
%\newcolumntype{R}{>{\collectcell\ApplyCellColor}{r}<{\endcollectcell}}%% if need right alignment

\newcommand{\RowColor}{\rowcolor{red!50} \cellcolor{white}}

\newcommand{\EndOfRow}{\stepcounter{CurrentRow}\\}% So that we know when we have ended a row
\newenvironment{MyTabular}[1]{%
    \setcounter{CurrentRow}{1}%
    \begin{tabular}{#1}%
}{%
    \end{tabular}%
    \gdef\CellColor{white}%
}


\begin{document}
\ColorBlock{2}{6}{red} 
\begin{MyTabular}{l || L L L L L | l}
       & 1 & 2 & 3 & 4 & 5 & 6 \EndOfRow\hhline{=#=====|=}
     1 & 1 & 1 & 1 & 1 & 1 & 0 \EndOfRow
     2 & 1 & 1 & 1 & 1 & 1 & 0 \EndOfRow
     3 & 1 & 1 & 1 & 1 & 1 & 0 \EndOfRow
     4 & 1 & 1 & 1 & 1 & 1 & 0 \EndOfRow
     5 & 1 & 1 & 1 & 1 & 1 & 0 \EndOfRow\hline
     6 & 0 & 0 & 0 & 0 & 0 & 1
\end{MyTabular}\verb|\ColorBlock{2}{6}{red}:|

\medskip
\ColorBlock{3}{5}{cyan} 
\begin{MyTabular}{l || L L L L L | l}
       & 1 & 2 & 3 & 4 & 5 & 6 \EndOfRow\hhline{=#=====|=}
     1 & 1 & 1 & 1 & 1 & 1 & 0 \EndOfRow
     2 & 1 & 1 & 1 & 1 & 1 & 0 \EndOfRow
     3 & 1 & 1 & 1 & 1 & 1 & 0 \EndOfRow
     4 & 1 & 1 & 1 & 1 & 1 & 0 \EndOfRow
     5 & 1 & 1 & 1 & 1 & 1 & 0 \EndOfRow\hline
     6 & 0 & 0 & 0 & 0 & 0 & 1
\end{MyTabular}\verb|\ColorBlock{3}{5}{cyan}:|

\end{document}
Peter Grill
  • 223,288
  • Great! I was kind of hoping for a solution in the line of \colorblock{start-col}{end-col}{start-row}{end-row}{color}, but, I suppose that doesn't exist. This works, thanks! – Keelan Nov 21 '14 at 19:02
  • @CamilStaps: That could be programmed with the collcell package, but if you are only doing a few tables then it may not be worth the effort. – Peter Grill Nov 21 '14 at 19:04
  • Oh - no, then it's not worth the effort. But I'll bookmark that! – Keelan Nov 21 '14 at 19:05
  • @CamilStaps: Revised solution which defines a \ColorBlock macro similar to what you described. – Peter Grill Nov 21 '14 at 19:52
  • Very nice, now I can decide which answer to accept :) even though it's not worth it for just one table – Keelan Nov 21 '14 at 19:59
4

If you color the cells, you need no rule. And you never need vertical rules in tables (nor double rules).

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

\begin{document}
\begin{tabular}{l *{5}{>{\columncolor{blue!20}}l} l}
\rowcolor{white}%
& 1 & 2 & 3 & 4 & 5 & 6\\
1 & 1 & 1 & 1 & 1 & 1 & 0\\
2 & 1 & 1 & 1 & 1 & 1 & 0\\
3 & 1 & 1 & 1 & 1 & 1 & 0\\
4 & 1 & 1 & 1 & 1 & 1 & 0\\
5 & 1 & 1 & 1 & 1 & 1 & 0\\
\rowcolor{white}%
6 & 0 & 0 & 0 & 0 & 0 & \cellcolor{green!20}1
\end{tabular}
\end{document}

enter image description here

Note that \rowcolor takes precedence over \columncolor and \cellcolor overrides both.

egreg
  • 1,121,712
  • I just made the double lines here to clearly show the blocks. I know that double lines are evil :) anyway, this works, thanks! – Keelan Nov 21 '14 at 19:04
  • @CamilStaps Good to know! Go ahead and build the best tables! – egreg Nov 21 '14 at 19:05
1

In {NiceTabular} of nicematrix, you have commands in the \CodeBefore to color the cells, rectangles, rows or columns you want.

You won't have the thin white lines you see in some PDF viewers at some levels of zoom.

\documentclass{article}
\usepackage{xcolor}
\usepackage{nicematrix}

\begin{document} \begin{NiceTabular}{lllllll} \CodeBefore \rectanglecolor{blue!20}{2-2}{6-6} \cellcolor{green!20}{7-7} \Body & 1 & 2 & 3 & 4 & 5 & 6\ 1 & 1 & 1 & 1 & 1 & 1 & 0\ 2 & 1 & 1 & 1 & 1 & 1 & 0\ 3 & 1 & 1 & 1 & 1 & 1 & 0\ 4 & 1 & 1 & 1 & 1 & 1 & 0\ 5 & 1 & 1 & 1 & 1 & 1 & 0\ 6 & 0 & 0 & 0 & 0 & 0 & 1\ \end{NiceTabular} \end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

F. Pantigny
  • 40,250