# Create multicolored off diagonals in a table

Is there a way to create the following coloring for a table? (the column and row headings having different colors... this is just the coloring of the "body" of the table)

I looked around and it seemed like the best bet was manual entry which will be a pain since I have multiple tables that need this type of coloring applied. The specific colors don't matter as much, but having different colors across diagonals is essential (preferably not repeated as in the example above as well).

So I guess I'm looking for a way to put a coloring algorithm into latex which given a table will give me the colored table? Also, it seems like generic colors could be achieved based on the size of the table.

I borrowed code from @David Carlisle's answer to this question. It defines counters for rows and columns of an array, and I just had to test the value of the sum of these counters to choose the colour of a given cell. I defined a new column type to simplify the typing of the table:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{fourier, erewhon}
\usepackage{blkarray}%
\usepackage[table]{xcolor}
\colorlet{ccol1}{red}
\colorlet{ccol2}{yellow}
\colorlet{ccol3}{cyan}
\colorlet{ccol4}{green}
\colorlet{ccol5}{brown}
\colorlet{ccol6}{magenta}
\colorlet{ccol7}{orange}
\colorlet{ccol8}{lime!80!}
\colorlet{ccol9}{pink}
\colorlet{ccol10}{violet!50!}
\colorlet{ccol11}{olive!50!}

%%%% Code by David Carlisle
\makeatletter
\def\insert@column{%
\the@toks \the \@tempcnta
\ignorespaces \@sharp \unskip
\the@toks \the \count@ \relax}

\let\old@arraycr\@arraycr

\let\old@tabarray\@tabarray
\def\@tabarray{\global\c@tabrow\@ne\global\c@tabcol\z@\old@tabarray}

\makeatother
\newcounter{tabcol}\newcounter{tabrow}
%%%%%%%%%%%%%%%
\newcounter{diagctr}
\newcolumntype{C}{>{\setcounter{diagctr}{\numexpr \value{tabrow} + \value{tabcol}\relax}\cellcolor{ccol\arabic{diagctr}}}c}

\begin{document}

$\renewcommand\arraystretch{1.5} \begin{array}{*{6}{C}}% e & a & a^2 & a^3 & a^4 & a^5 \\ a & a^2 & a^ 3 & a^4 & a^5 & e \\ a^2 & a^3 & a^4 & a^5 & e & a \\ a^3 & a^4 & a^5 & e & a & a^2 \\ a^4 & a^5 & e & a & a^2 & a^3 \\ a^5 & e & a & a^2 & a^3 & a^4 \end{array}$
\end{document}


If one wants to add an uncoloured row above, or a column on the left or both, one must change the formula which defines the index of the colour and add a test to determine if \cellcolor must be added on entering the cell or not. This is done with the help of etoolbox:

\usepackage{etoolbox}
\newcounter{diagctr}
\newcolumntype{C}{>{\setcounter{diagctr}{\numexpr \value{tabrow} + \value{tabcol}-2\relax}%
\ifboolexpr{test {\ifnumequal{\value{tabcol}}{0}} or test {\ifnumequal{\value{tabrow}}{1}}}{\relax}{\cellcolor{ccol\arabic{diagctr}}}}%
c}


\end{document}


• Great! Is there any way to make this work for tables or block arrays? I am trying to color an addition table with this. For example, is there a way to just have the body of the array or table be this but still add column and row headings somehow? – user2154420 Jul 11 '16 at 18:19
• @user2154420: I've added a code for one more uncoloured column or row. This changes the formula for determining the colours and adds tests. – Bernard Jul 11 '16 at 22:52