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.

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.

enter image description here

share|improve this question
    
Related Question: A table with square cells –  Peter Grill Mar 28 '12 at 18:32
add comment

3 Answers 3

up vote 9 down vote accepted

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
                        \advance\count@-26
                \fi
                \hbox to\baselineskip{\hss\char\count@\hss}%
                \advance\trcol\@ne
        \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
                        \advance\trrow\@ne
                \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.

share|improve this answer
    
@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
add comment

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.

share|improve this answer
1  
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
add comment

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}.

share|improve this answer
1  
Well, it's OK, but you'll never get a perfect square like this. –  Hendrik Vogt Dec 18 '10 at 7:39
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.