13

I'm trying to figure out how to create a table with equal-sized cells (every cell with the same width and height, regardless of the content). There should be only one outer border for the tables, and nothing between the cells.

Here's my code:

{\fontsize{22}{22} \selectfont
\begin{tabular}{|ccccccccccccccc|}
\hline
X & H & C & M & O & N & O & Y & J & F & O & C & D & L & D \\
T & Z & L & O & B & O & T & O & Q & L & O & T & H & Y & F \\
O & M & W & M & C & L & F & V & S & R & V & L & R & G & I \\
V & Y & C & Y & P & O & D & Y & R & D & X & M & I & E & Z \\
E & K & O & D & V & J & D & E & C & A & B & R & A & E & O \\
J & B & N & X & L & V & P & R & I & L & V & A & C & A & A \\
A & A & E & V & V & G & J & C & I & R & A & Q & U & Y & D \\
W & L & J & S & P & B & B & G & L & L & Y & B & A & N & I \\
G & L & O & F & C & P & Z & V & S & K & O & G & E & L & F \\
A & E & C & L & E & A & T & O & V & P & U & D & J & J & K \\
T & N & H & M & R & T & B & R & R & T & I & C & Z & A & A \\
O & A & G & V & A & F & W & A & R & R & O & F & R & P & B \\
R & I & J & S & N & B & F & O & L & E & O & Z & G & E & J \\
T & Q & R & W & A & U & T & E & O & L & N & I & D & R & W \\
G & B & J & L & E & Y & C & G & E & C & O & P & M & X & J \\
\hline
\end{tabular}
}

But the output is wrong:

enter image description here

The table is a 15x15 so the final output should be a perfect square... with equally-sized cells and same margins between the cells (columns and rows).

What am I doing wrong?

  • 1
    What do you mean with "nothing between the cells"? – Skillmon likes topanswers.xyz Mar 21 at 8:54
  • @Skillmonlikestopanswers.xyz I mean there should be a single border around the whole table, no separator lines between the columns or rows - just like in the example posted, only with equally-sized cells. – Dr.Kameleon Mar 21 at 8:55
  • Will the cell contents always consist of a single uppercase letter? – Mico Mar 21 at 9:08
  • @Mico yes, correct. – Dr.Kameleon Mar 21 at 9:13
12

With a tikz matrix:

enter image description here

\documentclass{article}
\usepackage[margin=0.5cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}

{
\fontsize{22}{22} \selectfont
\begin{tikzpicture}
  \matrix(table)[
  matrix of nodes,
  row sep =-\pgflinewidth,
  column sep = -\pgflinewidth,
  nodes={anchor=center,
         minimum width=1.25cm, 
         minimum height=1.25cm, 
         },
  ] 
  {X & H & C & M & O & N & O & Y & J & F & O & C & D & L & D \\
T & Z & L & O & B & O & T & O & Q & L & O & T & H & Y & F \\
O & M & W & M & C & L & F & V & S & R & V & L & R & G & I \\
V & Y & C & Y & P & O & D & Y & R & D & X & M & I & E & Z \\
E & K & O & D & V & J & D & E & C & A & B & R & A & E & O \\
J & B & N & X & L & V & P & R & I & L & V & A & C & A & A \\
A & A & E & V & V & G & J & C & I & R & A & Q & U & Y & D \\
W & L & J & S & P & B & B & G & L & L & Y & B & A & N & I \\
G & L & O & F & C & P & Z & V & S & K & O & G & E & L & F \\
A & E & C & L & E & A & T & O & V & P & U & D & J & J & K \\
T & N & H & M & R & T & B & R & R & T & I & C & Z & A & A \\
O & A & G & V & A & F & W & A & R & R & O & F & R & P & B \\
R & I & J & S & N & B & F & O & L & E & O & Z & G & E & J \\
T & Q & R & W & A & U & T & E & O & L & N & I & D & R & W \\
G & B & J & L & E & Y & C & G & E & C & O & P & M & X & J \\
  };
  \draw [black] (table-1-1.north west) -- (table-1-15.north east) -- (table-15-15.south east) -- (table-15-1.south west) -- cycle ;
\end{tikzpicture}
}
\end{document}
| improve this answer | |
10

Here's a non-TikZ solution. It lets you produce square tables of width (and height) \tablewidth. The first table in the following screeshot uses a width of \textwidth and a font size of 22.74pt (22pt is not available by default unless you add some extra packages); the second table uses a width of 0.5\textwidth and a font size of 9pt.

enter image description here

enter image description here

\documentclass{article}
\usepackage[margin=1cm]{geometry} % set suitable page parameters
\usepackage{tabularx}
    \newcolumntype{C}{>{\centering\arraybackslash}X}
\newlength\tablewidth
\newlength\celldim

\newcommand{\ProduceTable}{%
    \setlength\tabcolsep{0pt}
    \setlength\celldim{\dimexpr(\tablewidth-2\arrayrulewidth)/15\relax}
    \setlength\extrarowheight{\dimexpr\celldim-\baselineskip\relax}
    \centering
    \begin{tabularx}{\tablewidth}{|*{15}{C}|}
    \hline
    X&H&C&M&O&N&O&Y&J&F&O&C&D&L&D \\
    T&Z&L&O&B&O&T&O&Q&L&O&T&H&Y&F \\
    O&M&W&M&C&L&F&V&S&R&V&L&R&G&I \\
    V&Y&C&Y&P&O&D&Y&R&D&X&M&I&E&Z \\
    E&K&O&D&V&J&D&E&C&A&B&R&A&E&O \\
    J&B&N&X&L&V&P&R&I&L&V&A&C&A&A \\
    A&A&E&V&V&G&J&C&I&R&A&Q&U&Y&D \\
    W&L&J&S&P&B&B&G&L&L&Y&B&A&N&I \\
    G&L&O&F&C&P&Z&V&S&K&O&G&E&L&F \\
    A&E&C&L&E&A&T&O&V&P&U&D&J&J&K \\
    T&N&H&M&R&T&B&R&R&T&I&C&Z&A&A \\
    O&A&G&V&A&F&W&A&R&R&O&F&R&P&B \\
    R&I&J&S&N&B&F&O&L&E&O&Z&G&E&J \\
    T&Q&R&W&A&U&T&E&O&L&N&I&D&R&W \\
    G&B&J&L&E&Y&C&G&E&C&O&P&M&X&J \\[1ex]
    \hline
    \end{tabularx}}

\begin{document}

\begin{table}
    \fontsize{20.74}{20.74}\selectfont % '22pt' not available in default font family
    \setlength{\tablewidth}{1\textwidth}
    \ProduceTable
\end{table}

\begin{table}
    \ttfamily
    \fontsize{9}{9}\selectfont 
    \setlength{\tablewidth}{0.5\textwidth} 
    \ProduceTable
\end{table}
\end{document}
| improve this answer | |
9

Here is another variant, using an ordinary tabular, fixed width m-columns and a 1 cm heigh, 0-width rule redirected (>{}) into a 0-width column (F). m-columns centre the cell contents vertically. I define two new columns (M and F) to clean the code a little bit.

The only additional package you need, is array. I use geometry just to have more sensible margins.

If you prefer a table that adapts to the margins, you calculate the column width using \dimexpr; subtract the width of two array rules (the vertical lines) from the total text width (the distance between the left and right margins), and divide the result by 15. Use the same calculation to find the row height, i.e.:

\newcolumntype{M}{>{\centering\arraybackslash}m{\dimexpr((\textwidth-2\arrayrulewidth)/15)\relax}}
%  The column width
\newcolumntype{F}{>{\rule{0pt}{\dimexpr((\textwidth-2\arrayrulewidth)/15)\relax}}m{0pt}}
%  The row height

Of cause, instead of repeating the calculation, I could have defined a new command, but since it is only two places, I can defend not to do that. I

enter image description here

\documentclass{article}
\usepackage[margin=2cm]{geometry} % set suitable margins
\usepackage{array}

\newcolumntype{M}{>{\centering\arraybackslash}m{1cm}} % Horizontal and vertically 
                                                      % centred cells 
\newcolumntype{F}{>{\rule{0pt}{1cm}}m{0pt}}           % Minimum 1 cm heigh rows 

\begin{document}

  \setlength\tabcolsep{0pt}             % Remove space between columns
  \renewcommand{\arraystretch}{0}       % Remove space between rows 
  \centering
  \fontsize{22}{22}\selectfont          % Gives 22.74pt 
                                        % unless you load a scalable font
  \begin{tabular}{|*{15}{M}|F}
  \hline
  X & H & C & M & O & N & O & Y & J & F & O & C & D & L & D & \\
  T & Z & L & O & B & O & T & O & Q & L & O & T & H & Y & F & \\
  O & M & W & M & C & L & F & V & S & R & V & L & R & G & I & \\
  V & Y & C & Y & P & O & D & Y & R & D & X & M & I & E & Z & \\
  E & K & O & D & V & J & D & E & C & A & B & R & A & E & O & \\
  J & B & N & X & L & V & P & R & I & L & V & A & C & A & A & \\
  A & A & E & V & V & G & J & C & I & R & A & Q & U & Y & D & \\
  W & L & J & S & P & B & B & G & L & L & Y & B & A & N & I & \\
  G & L & O & F & C & P & Z & V & S & K & O & G & E & L & F & \\
  A & E & C & L & E & A & T & O & V & P & U & D & J & J & K & \\
  T & N & H & M & R & T & B & R & R & T & I & C & Z & A & A & \\
  O & A & G & V & A & F & W & A & R & R & O & F & R & P & B & \\
  R & I & J & S & N & B & F & O & L & E & O & Z & G & E & J & \\
  T & Q & R & W & A & U & T & E & O & L & N & I & D & R & W & \\
  G & B & J & L & E & Y & C & G & E & C & O & P & M & X & J & \\
  \hline
  \end{tabular}

\end{document}
| improve this answer | |
2

This uses a TABstack to create the array, and a \tokencycle to make sure that each entry is placed in a square placeholder (i.e., a \makebox of width \baselineskip).

\documentclass{article}
\usepackage{tabstackengine,tokcycle}
\begin{document}
{\fontsize{22}{22}\selectfont
\fbox{\tokencycle{\ifx&#1\addcytoks{#1}\else
  \addcytoks{\makebox[\baselineskip]{#1}}\fi}
{\processtoks{#1}}{\addcytoks{#1}}{}\tabbedLongstack{
X & H & C & M & O & N & O & Y & J & F & O & C & D & L & D \\
T & Z & L & O & B & O & T & O & Q & L & O & T & H & Y & F \\
O & M & W & M & C & L & F & V & S & R & V & L & R & G & I \\
V & Y & C & Y & P & O & D & Y & R & D & X & M & I & E & Z \\
E & K & O & D & V & J & D & E & C & A & B & R & A & E & O \\
J & B & N & X & L & V & P & R & I & L & V & A & C & A & A \\
A & A & E & V & V & G & J & C & I & R & A & Q & U & Y & D \\
W & L & J & S & P & B & B & G & L & L & Y & B & A & N & I \\
G & L & O & F & C & P & Z & V & S & K & O & G & E & L & F \\
A & E & C & L & E & A & T & O & V & P & U & D & J & J & K \\
T & N & H & M & R & T & B & R & R & T & I & C & Z & A & A \\
O & A & G & V & A & F & W & A & R & R & O & F & R & P & B \\
R & I & J & S & N & B & F & O & L & E & O & Z & G & E & J \\
T & Q & R & W & A & U & T & E & O & L & N & I & D & R & W \\
G & B & J & L & E & Y & C & G & E & C & O & P & M & X & J}%
\endtokencycle}}
\end{document}

enter image description here

The box size can be changed merely by altering the second argument to \fontsize. For example \fontsize{22}{17} produces this result

enter image description here

| improve this answer | |
  • But to make the cells square, both parameters need to be equal? – Sveinung Apr 5 at 11:31
  • @Sveinung No, not true. But, of course, if the 2nd parameter becomes too small relative to the 1st, there will be letter overlap. – Steven B. Segletes Apr 5 at 11:47
  • Thank you for the answer. Sorry if my questions can be interpreted as a statement. That was no my intention. – Sveinung Apr 5 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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