0

Suppose we have a matrix of numbers

$$\begin{matrix}
1 & 2 & 3 & 4 \\ 
1 & 2 & 3 & 4 \\    
1 & 2 & 3 & 4  \\   \end{matrix}$$

I would like to transform in MathJAx a matrix of numbers into the matrix where entries are color squares. The example of this could be the text below however it has a drawback that it has inserted vertical and horizontal spaces between squares.

How to do it without additional spacing? (to obtain continuous rectangular color area)

$\begin{matrix}
\color{red}\blacksquare \color{olive}\blacksquare \color{teal}\blacksquare \color{blue}\blacksquare \\   
\color{red}\blacksquare \color{olive}\blacksquare \color{teal}\blacksquare \color{blue}\blacksquare \\    
\color{red}\blacksquare \color{olive}\blacksquare \color{teal}\blacksquare \color{blue}\blacksquare \\  \end{matrix}$
  • Is this question really about MathJax or about something in TeX? Because MathJax tends to be off-topic here. – TeXnician May 30 '18 at 8:20
  • MathJax is not connected with Latex? Propose solution in Latex.. – Widawensen May 30 '18 at 8:21
  • No knowledge here Does MathJax support \color? – Skillmon May 30 '18 at 8:22
  • Does it have to be in math mode or would a tabular suffice? – Skillmon May 30 '18 at 8:23
  • @Skillmon Yes, it looks fine except spacing is not needed. – Widawensen May 30 '18 at 8:23
1

You can use something like the following:

\documentclass[]{article}

\usepackage[table]{xcolor}
\usepackage{collcell}

\newcolumntype{\colored}{>{\collectcell\colored}l<{\endcollectcell}}

\newcommand*\colored[1]
  {%
    \ifcase#1
      \cellcolor{white}% if cell content is evaluated to be the number 0
    \or \cellcolor{red}%   1
    \or \cellcolor{olive}% 2
    \or \cellcolor{teal}%  3
    \or \cellcolor{blue}%  4
      % add more \or \cellcolor constructs for more colours
    \fi
  }

\begin{document}
\begin{tabular}[]{*4\colored}
  1 & 2 & 3 & 4 \\
  1 & 2 & 3 & 4 \\
  1 & 2 & 3 & 4 \\
\end{tabular}
\end{document}

enter image description here

The white lines in the above image are rendering issues of pdftocairo. If you zoom 800 % with xpdf there are no seems.

If you slightly alter the definition of \colored (the macro not the column) you can even place stuff inside of your table cells (while still colouring them). If the contents start with a number, that number is considered to be the colour specification and the rest is typeset.

\documentclass[]{standalone}

\usepackage[table]{xcolor}
\usepackage{collcell}

\newcolumntype{\colored}{>{\collectcell\colored}l<{\endcollectcell}}

\newcommand*\colored[1]
  {%
    \ifcase0#1
      \cellcolor{white}% if cell content is evaluated to be the number 0
    \or \cellcolor{red}\ifnum0<#1\fi%   1
    \or \cellcolor{olive}\ifnum0<#1\fi% 2
    \or \cellcolor{teal}\ifnum0<#1\fi%  3
    \or \cellcolor{blue}\ifnum0<#1\fi%  4
      % add more \or \cellcolor constructs for more colours
    % if the encountered number is greater than the defined ones \else is
    % used
    \else \cellcolor{white}\ifnum0<#1\fi
    \fi
  }

\begin{document}
\begin{tabular}[]{*4\colored}
  0 different & content & is & possible\\
  1 foo & 2 & 3 & 4 \\
  1 & 2 & 3 & 4 \\
  1 & 2 & 3 & 4 \\
\end{tabular}
\end{document}

enter image description here

  • The result is excellent. Nothing better I'm not expecting. – Widawensen May 30 '18 at 8:39

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.