# How do I make a square table in LaTeX?

How can I make a perfectly square table in LaTeX? Each cell will just have a single character in it. I'm trying to print a pretty tabula recta as shown below.

-
Related Question: A table with square cells –  Peter Grill Mar 28 '12 at 18:32

Here's one way to do it.

\newcount\trrow
\newcount\trcol
\makeatletter
\def\maketrrow{%
\begingroup
\loop\ifnum\trcol<26
\count@\numexpr \trrow + \trcol + A\relax
\ifnum\count@>Z
\fi
\hbox to\baselineskip{\hss\char\count@\hss}%
\repeat
\endgroup
}

\newcommand\tabularecta[1][\baselineskip]{%
\vbox{%
\fontsize{\f@size}{#1}\selectfont
\hsize\dimexpr 27\baselineskip + .4\p@ \relax
\parindent\z@
\parskip\z@
\trrow\z@
\leavevmode
\hbox to\baselineskip{\hfil}%
\strut\vrule
\maketrrow
\par
\hrule
\loop\ifnum\trrow<26
\leavevmode
\trcol\z@
\hbox to\baselineskip{\hss\char\numexpr\trrow + A\relax\hss}%
\strut\vrule
\maketrrow
\par
\repeat
}%
}
\makeatother


Just place a \tabularecta wherever you want it to appear. You can use font size changes like \small if you want. Since the spacing is based entirely on the baseline, you can simply change it using \fontsize{..}{..}\selectfont, or for convenience, you can give \tabularecta an optional argument that is the size you want for each cell.

The implementation is straight forward. It iterates over the rows and then over the columns placing boxes of the appropriate size filled with the desired character inside.

-
@TH Nice implementation of looping through an alphabet. I would use two arguments, one for fontsize and another for baseline, that way you can scale the whole table easily - you never know cryptographers might want to use more than 26 letters! –  Yiannis Lazarides Dec 18 '10 at 3:58
Very nice! One question: The .4\p@ are the width of the \vrule, yes? –  Hendrik Vogt Dec 18 '10 at 7:58
@Hendrik: Yeah. I'm not even positive that's the right width. I briefly googled, saw that number and plugged it in. Initially, I used \hbox to\z@{\hss\strut\vrule\hss} but then I figured that probably was too crowded. –  TH. Dec 18 '10 at 9:06
@Yiannis: Well, you can just change the fontsize before the \tabularecta. E.g., \tiny before it works just fine. –  TH. Dec 18 '10 at 9:07

In ConTeXt, you can make each cell of the same height and width by

\setupTABLE[each][each][width=2em,height=2em,align={middle,middle}]


If you want to see how to generate the output as a loop, have a look at a similar example on the context wiki.

-
I'm afraid I don't know enough (okay, any) ConTeXt to turn that into a compilable example. Will the baselines of each character in the row line up? If so, that's a pretty nice solution. –  TH. Dec 18 '10 at 10:09
Yes, the baselines are aligned. Internally, each cell is a framed (one of the core ConTeXt constructs), and the option align={middle, middle} is translated to align=middle and location=lohi. The key location=lohi aligns at the baseline. –  Aditya Dec 18 '10 at 16:25

The easiest way possible is to use the tabular environment:

\documentclass[11pt]{article}
\begin{document}
\begin{tabular}{l|llllll}
A &B &C &D &E &F &G\\[1pt]
\hline
A &A &B &C &D &E &F\\[1pt]
B &B &C &D &E &F &G\\[1pt]
C &C &D &E &F &G &H\\[1pt]
D &D &E &F &G &H &I\\[1pt]
E &E &F &G &H &I &J\\[1pt]
F &F &G &H &I &J &K\\[1pt]
\end{tabular}
\end{document}


I added the \\[1pt] to make it look more square (it will depend on the final font size you use). One could also define a short-cut for it to avoid typing it so many times. For me it looks it may take quite a bit of space, so if you want to make the whole thing smaller to fit somewhere, you can enclose it in a \scalebox{0.5}{\begin{tabular}...\end{tabular}}. You get the command \scalebox, by using \usepackage{graphicx}`.

-
Well, it's OK, but you'll never get a perfect square like this. –  Hendrik Vogt Dec 18 '10 at 7:39