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:

\usepackage{fourier, erewhon}

%%%% Code by David Carlisle
   \the@toks \the \@tempcnta
   \ignorespaces \@sharp \unskip
   \the@toks \the \count@ \relax}



\newcolumntype{C}{>{\setcounter{diagctr}{\numexpr \value{tabrow} + \value{tabcol}\relax}\cellcolor{ccol\arabic{diagctr}}}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

enter image description here

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:

\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}}}}%

    & e & a & a^2 & a^3 & a^4 & a^5\\
\cline {2-7}\noalign{\vskip2pt}
    e & e & a & a^2 & a^3 & a^4 & a^5 \\
    a & a & a^2 & a^ 3 & a^4 & a^5 & e \\
    a^2 & a^2 & a^3 & a^4 & a^5 & e & a \\
    a^3 & a^3 & a^4 & a^5 & e & a & a^2 \\
    a^4 & a^4 & a^5 & e & a & a^2 & a^3 \\
    a^5 & a^5 & e & a & a^2 & a^3 & a^4\\
    \cline {2-7}


enter image description here

  • 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

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.