[Tex/LaTex] Efficiently colouring block of table cells

colortables

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)?

Best Answer

  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}