1

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)

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.

2 Answers 2

2

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
   \global\advance\c@tabcol\@ne
   \ignorespaces \@sharp \unskip
   \the@toks \the \count@ \relax}

\let\old@arraycr\@arraycr
\def\@arraycr{\global\c@tabcol\z@\global\advance\c@tabrow\@ne\old@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} 

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:

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

\[\renewcommand\arraystretch{1.5}
\renewcommand\arrayrulewidth{2pt}
\begin{array}{C*{6}{C}}
    & 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}
      \end{array}
\]

\end{document}

enter image description here

2
  • 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? Jul 11, 2016 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, 2016 at 22:52
1

Here is a way to do that with {NiceArray} of nicematrix.

\documentclass{article}
\usepackage{nicematrix}
\usepackage{tikz}

\begin{document}

\[\renewcommand\arraystretch{1.5}
  \begin{NiceArray}{*{6}{c}}[first-row,first-col,hvlines,rules={width=0.2pt,color=white}]
  \CodeBefore
    \foreach \x in {1,...,\arabic{iRow}}
       \foreach \y in {1,...,\arabic{jCol}}
         { 
           \pgfmathsetmacro\z{(\x+\y)/(\arabic{iRow}+\arabic{jCol})*100}
           \cellcolor{cyan!\z!magenta}{\x-\y}
         } 
  \Body
      &   e & a   & a^2  & a^3 & a^4 & a^5 \\
  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^6 & a^5 & e   & a    & a^2 & a^3 & a^4
  \end{NiceArray}
\]


\end{document}

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

Output of the above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .