Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
Can you make your code into a compilable document, instead of just fragments. –  Alan Munn May 21 '11 at 15:04
    
it's done now.. –  scdmb May 21 '11 at 15:17
add comment

1 Answer 1

up vote 11 down vote accepted

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.

share|improve this answer
    
! 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
1  
I updated tikz and it works now, thanks. –  scdmb May 21 '11 at 15:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.