12

I would like to have array with square cells and fill cells with colors. There is done implementation of table with square cells by W. Robertson http://www.tug.org/pracjourn/2005-2/robertson/robertson.pdf :

\newlength\celldim \newlength\fontheight \newlength\extraheight
\newcounter{sqcolumns}

\newcolumntype{S}{ @{}
  >{\centering \rule[-0.5\extraheight]{0pt}{\fontheight + \extraheight}}
  p{\celldim} @{} }

\newcolumntype{Z}{ @{} >{\centering} p{\celldim} @{} }

\newenvironment{squarecells}[1]
  {\setlength\celldim{2em}%
  \settoheight\fontheight{A}%
  \setlength\extraheight{\celldim - \fontheight}%
  \setcounter{sqcolumns}{#1 - 1}%
  \begin{tabular}{|S|*{\value{sqcolumns}}{Z|}}\hline}
  % squarecells tabular goes here
  {\end{tabular}}

\newcommand\nl{\tabularnewline\hline}

So I write:

\documentclass[a4paper,12pt]{report}

\usepackage[MeX]{polski}
\usepackage[utf8]{inputenc}
\usepackage[table]{xcolor}
\usepackage{array}
\usepackage{calc}
\newlength\celldim \newlength\fontheight \newlength\extraheight
\newcounter{sqcolumns}

\newcolumntype{S}{ @{}
  >{\centering \rule[-0.5\extraheight]{0pt}{\fontheight + \extraheight}}
  p{\celldim} @{} }

\newcolumntype{Z}{ @{} >{\centering} p{\celldim} @{} }

\newenvironment{squarecells}[1]
  {\setlength\celldim{4em}%
  \settoheight\fontheight{A}%
  \setlength\extraheight{\celldim - \fontheight}%
  \setcounter{sqcolumns}{#1 - 1}%
  \begin{tabular}{|S|*{\value{sqcolumns}}{Z|}}\hline}
  % squarecells tabular goes here
  {\end{tabular}}

\newcommand\nl{\tabularnewline\hline} 

\begin{document}

\begin{squarecells}{4}
\cellcolor{red}16 & 3 & 2 & 13 \nl
5 & 10 & 11 & 8 \nl
9 & 6 + 2 & 7 & 12 \nl
4 & 15 & 14 & 1 \nl
\end{squarecells}

\end{document}

Output is:

Output

Cell is filled with red color a little more than it should be. Question is, how to properly fill cell with color? Or maybe is there some other good solution to create table with square cells?

lockstep
  • 250,273
scdmb
  • 1,241

2 Answers2

15

If you're not set on the array approach and would be okay with explicitly specifying the size of the squares, you could use TikZ for this. Based on the answer to the question TikZ matrix as a replacement for tabular, you could define a style that adjusts a TikZ matrix to have all the cells join each other and have identical heights and widths:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}

\tikzset{square matrix/.style={
    matrix of nodes,
    column sep=-\pgflinewidth, row sep=-\pgflinewidth,
    nodes={draw,
      minimum height=#1,
      anchor=center,
      text width=#1,
      align=center,
      inner sep=0pt
    },
  },
  square matrix/.default=1.2cm
}

\matrix[square matrix]
{
16 & 3 & 2 & 13 \\
5 & 10 &|[fill=yellow]| 11 & 8 \\
9 & 6 + 2 & 7 & 12 \\
4 & 15 & 14 & 1 \\
};

\end{tikzpicture}
\end{document}

tikz matrix with square elements

To adjust the size of the elements, use square matrix=<length>. To fill elements with different colours, use |[fill=<colour>]| in the cell you want to adjust.

David Carlisle
  • 757,742
Jake
  • 232,450
  • ! Package pgfkeys Error: I do not know the key '/tikz/align' and I am going to ignore it. Perhaps you misspelled it. -- When I remove align, then numbers are aligned to left-bottom corner. – scdmb May 21 '11 at 15:31
  • @scdmb: You're using an old version of TikZ (v 2.0, probably?), the current one is v 2.1 – Jake May 21 '11 at 15:32
  • @scdmb: Try changing text width=#1 to minimum width=#1, that should work with the old version as well. – Jake May 21 '11 at 15:36
  • @Jake is this answer updated? Because in beamer I cannot test with \matrix, it throws me a lot of errors. – manooooh Jul 23 '20 at 22:50
1

If, in your code, I replace {tabular} by {NiceTabular} (of nicematrix, which I have loaded) with the key color-inside (alias: colortbl-like), I have directly the expected output. The reason is that nicematrix colors the cells by using PGF/Tikz under the hood and do not use colortbl).

\documentclass[a4paper,12pt]{report}

\usepackage[MeX]{polski} \usepackage[table]{xcolor} \usepackage{nicematrix} \usepackage{calc}

\newlength\celldim \newlength\fontheight \newlength\extraheight \newcounter{sqcolumns}

\newcolumntype{S}{ @{} >{\centering \rule[-0.5\extraheight]{0pt}{\fontheight + \extraheight}} p{\celldim} @{} }

\newcolumntype{Z}{ @{} >{\centering} p{\celldim} @{} }

\newenvironment{squarecells}[1] {\setlength\celldim{4em}% \settoheight\fontheight{A}% \setlength\extraheight{\celldim - \fontheight}% \setcounter{sqcolumns}{#1 - 1}% \begin{NiceTabular}[color-inside]{|S|*{\value{sqcolumns}}{Z|}}\hline} % squarecells tabular goes here {\end{NiceTabular}}

\newcommand\nl{\tabularnewline\hline}

\begin{document}

\begin{squarecells}{4} \cellcolor{red}16 & 3 & 2 & 13 \nl 5 & 10 & 11 & 8 \nl 9 & 6 + 2 & 7 & 12 \nl 4 & 15 & 14 & 1 \nl \end{squarecells}

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz under the hood).

Output of the above code

F. Pantigny
  • 40,250