Suppose we have a matrix of numbers

$$\begin{matrix} 1 & 3 & 2 & 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 (visible as the final effect)

$$\begin{matrix} \color{red}\blacksquare \color{teal}\blacksquare \color{#0F0}\blacksquare \color{blue}\blacksquare \\ \color{red}\blacksquare \color{#0F0}\blacksquare \color{teal}\blacksquare \color{blue}\blacksquare \\ \color{red}\blacksquare \color{#0F0}\blacksquare \color{teal}\blacksquare \color{blue}\blacksquare \\ \end{matrix}$$

however it has a drawback that it has inserted vertical and horizontal spaces between squares. Mapping between integer numbers and colors is fixed.

  • How to do it without additional spacing? (to obtain continuous rectangular color area - at least to remove spacing between lines)

The solution is possible in Latex, but how to obtain similar result only in MathJAx?

  • 1
    $\begingroup$ This seems (to some extent) related: Does MathJaX have a command to stretch or reduce vertical space? $\endgroup$ – Martin Sleziak May 30 '18 at 11:28
  • $\begingroup$ @MartinSleziak Thank you Martin for the link, for sure these propositions are useful in many situations. However in my problem I have tried the proposed solutions and I don't see significant effect.. $\endgroup$ – Widawensen May 30 '18 at 11:49
  • $\begingroup$ There are a few CSS-type annotations that are possible to specify in mathjax. But these are aimed at the website owners (who choose which configuration to load) instead of at us users. I suppose it would be possible to load your own mathjax if you wanted... $\endgroup$ – davidlowryduda May 30 '18 at 17:54

You can do it, but you need to use large squares and hide their height and depth using \smash. It helps to use a definition or two. Here is one approach:


which produces:

$$ \def\smallstrut{\Space{0em}{.6em}{.2em}} \def\cbox#1{\textstyle\smash{\color{#1}{\Rule{1em}{.8em}{.2em}}}\smallstrut} \begin{smallmatrix} \cbox{red}\cbox{teal}\cbox{#0F0}\cbox{blue}\\ \cbox{red}\cbox{#0F0}\cbox{teal}\cbox{blue}\\ \cbox{red}\cbox{#0F0}\cbox{teal}\cbox{blue}\\ \cbox{red}\cbox{#0F0}\cbox{teal}\cbox{blue}\\ \end{smallmatrix} $$

If you want a small amount of space between the rows and columns, you can change the size of the \smallstrut and add a little width to it


with the rest being the same:

$$ \def\smallstrut{\Space{.2em}{.8em}{.2em}} \def\cbox#1{\textstyle\smash{\color{#1}{\Rule{1em}{.8em}{.2em}}}\smallstrut} \begin{smallmatrix} \cbox{red}\cbox{teal}\cbox{#0F0}\cbox{blue}\\ \cbox{red}\cbox{#0F0}\cbox{teal}\cbox{blue}\\ \cbox{red}\cbox{#0F0}\cbox{teal}\cbox{blue}\\ \cbox{red}\cbox{#0F0}\cbox{teal}\cbox{blue}\\ \end{smallmatrix} $$

  • $\begingroup$ Excellent answer! The method can be very useful in graphical presenting matrices. Thank you. $\endgroup$ – Widawensen Jun 5 '18 at 11:28

You must log in to answer this question.

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