[OS X TeX] Square cells in tabular
Will Robertson
will at guerilla.net.au
Thu Dec 23 10:23:22 CET 2004
Hi all
Arno contacted me off-list about some help with this problem, and I've
posted my solution here for all to see.
Hope it's still of help and sorry about the delay.
Regards,
Will
=== snip LaTeX document from here to end ===
\documentclass{article}
\usepackage{array,calc}
\setlength\parindent{0pt}
\setlength\parskip{1em}
\begin{document}
\section*{Square cells in a tabular}
Arno Kruse asked if it was possible to create a tabular with square
cells. The solution involves some use of the \textsf{array} package,
uses several \LaTeX\ length commands and the \textsf{calc} package. I
thought that the curious might like to look at my proposal for interest
in the solution, critique of my coding style, or (I hope!) to learn a
little about some \LaTeX\ concepts. Please refer to the array manual
for further explanation.
First, set every column to some width (I have used 2\,em in the current
font). This is done with the \verb|p{...}| column specifier.
\newlength\squarecell
\setlength\squarecell{2em}
\begin{tabular}{|p{\squarecell}|p{\squarecell}|}
\hline
1 & 2 \\
\hline
3 & 4 \\
\hline
\end{tabular}
If you measure the widths of the cells, however, you'll notice that
they are in fact \emph{larger} than 2\,em since the tabular environment
inserts space in between each column. (This is controlled by the
\verb|\tabsepcol| length, but I leave it alone in case other tabulars
are being used in the document.) The intercolumn space may be
suppressed by using the \verb|@{}| column specifier. Now each cell is
exactly \verb|\squarecell|. I have also added a centring command to
each cell of the tabular.
\newcolumntype{C}{@{}>{\centering\arraybackslash}p{\squarecell}@{}}
\begin{tabular}{|C|C|}
\hline
1 & 2 \\
\hline
3 & 4 \\
\hline
\end{tabular}
Now we need to set the height of each cell equal to the width. This can
be achieved by inserting a strut (a zero width rule) of a certain
height and a certain depth. First, I measure the height of a left
parenthesis (`\verb|(|') to get the approximate height required to
vertically centre the text. Then I subtract this value from the square
cell size and divide by two to get the surrounding space.
\newlength\fontheight
\newlength\surroundcellheight
\settoheight\fontheight{(}
\setlength\surroundcellheight{(\squarecell - \fontheight)/2}
\newcolumntype{S}{ @{}
>{\centering\arraybackslash}
p{\squarecell}
<{\rule{0pt}{\fontheight +
\surroundcellheight}\rule[-
\surroundcellheight]{0pt}{\surroundcellheight}}
% change 0pt to 1pt to see what the rules do.
@{} }
\begin{tabular}{|S|S|}
\hline
1 & 2 \\
\hline
3 & 4 \\
\hline
\end{tabular}
\newpage
\section*{Example}
So now that's done, a proper example.
I define a new environment for setting these special tables called
\texttt{squarecells}.
\newenvironment{squarecells}[1]
{\setlength\squarecell{2em} % set the cell dimension to 2em in the
current font
\settoheight\fontheight{(}
\setlength\surroundcellheight{(\squarecell - \fontheight)/2}
\begin{tabular}{#1}}
{\end{tabular}}
\hfill
\begin{minipage}{0.3\textwidth}
\large
\begin{squarecells}{|S|S|S|S|}
\hline
17 & & 0 & 11 \\ \hline
& & 16 & 6 \\ \hline
& 4 & & \\ \hline
2 & 16 & & 3 \\ \hline
\end{squarecells}
\end{minipage}
\hfill
\begin{minipage}{0.45\textwidth}
\begin{verbatim}
\begin{squarecells}{|S|S|S|S|}
\hline
17 & & 0 & 11 \\ \hline
& & 16 & 6 \\ \hline
& 4 & & \\ \hline
2 & 16 & & 3 \\ \hline
\end{squarecells}
\end{verbatim}
\end{minipage}
\hfill\null
\normalsize
So, in order to get this working in your own document, you'll need
something along the lines of the following into your preamble. (I'm too
lazy to make a proper package for it.) It doesn't work perfectly, but
it scales reasonably well so I hope it may be of use to at least Arno.
\begin{verbatim}
\newlength\squarecell
\newlength\fontheight
\newlength\surroundcellheight
\newenvironment{squarecells}[1]
{\setlength\squarecell{2em}
\settoheight\fontheight{(}
\setlength\surroundcellheight{(\squarecell - \fontheight)/2}
\begin{tabular}{#1}}
{\end{tabular}}
\end{verbatim}
\end{document}
--------------------- Info ---------------------
Mac-TeX Website: http://www.esm.psu.edu/mac-tex/
& FAQ: http://latex.yauh.de/faq/
TeX FAQ: http://www.tex.ac.uk/faq
List Post: <mailto:MacOSX-TeX at email.esm.psu.edu>
More information about the macostex-archives
mailing list