1

I'm trying to create a table with square cells and both vertically and horizontally centered content. I defined the table as follows:

\documentclass{article}
\usepackage{tabu}
\newcolumntype{K}[1]{>{\vbox to #1{\vfill}\centering}m{#1}}
\renewcommand{\i}{$\cdot$}
\begin{document}
\begin{tabu}{|[1pt]*{3}{K{3mm}|K{3mm}|[1pt]}}
\tabucline[1pt]{1-6}
0  &  4  & \i  &  4  & \i  & \i  \\ \tabucline{1-6}
2  &  0  & \i  & \i  & \i  & -3  \\ \tabucline[1pt]{1-6}
\i & \i  &  0  &  3  &  5  & -1  \\ \tabucline{1-6}
4  & \i  &  3  &  0  & -6  & \i  \\ \tabucline[1pt]{1-6}
-3 & \i  & \i  & -6  &  0  &  9  \\ \tabucline{1-6}
\i & \i  & -1  &  5  &  7  &  0  \\ \tabucline[1pt]{1-6}
\end{tabu}
\end{document}

The result is

unaligned table

How can I vertically center the numbers?

2 Answers2

1

A different appproach, using a stack, based on my answer at What is the best way to create this kind of binary matrix?.

The grid thickness, color, and size are all preset-able in the preamble.

\documentclass{article}
\usepackage[usestackEOL]{stackengine}
\usepackage{xcolor, graphicx}
\newsavebox{\Bbox}
\def\thk{.8pt}                    % RULE THICKNESS
\def\gsize{1.4cm}                 % GRID SIZE
\def\gridcolor{red!40}
\def\coresize{\dimexpr\gsize-2\dimexpr\thk\relax\relax}
\def\grid{\kern-\thk\fboxsep=.5\coresize\relax%
  \fboxrule=\thk\relax\textcolor{\gridcolor}{\fbox{}}}
\newcommand\G[1][.]{\unskip\stackinset{c}{.0pt}{c}{-.4pt}{\scalebox{2}{$#1$}}{\grid}}
\setstackgap{S}{-\thk}
\begin{document}
\Shortstack{%
\G[0] \G[4]\G    \G[4] \G    \G    \\
\G[2] \G[0]\G    \G    \G    \G[-3]\\
\G    \G   \G[0] \G[3] \G[5] \G[-1]\\
\G[4] \G   \G[3] \G[0] \G[-6]\G    \\
\G[-3]\G   \G    \G[-6]\G[0]\G[9]  \\
\G    \G   \G[-1]\G[5] \G[7]\G[0]
    }
\end{document}

enter image description here

  • The OP's figure appears to have . in the non-numbered cells, even thought the code shows \i. To accommodate the latter, change the optional argment for \G from [.]to [\vphantom{0}\i] – Steven B. Segletes Aug 11 '17 at 15:00
0

Your \vbox is put on the baseline of the cell, which will push everything down, so you need to add depth as well. You can force the cell to a certain height by adding a rule. To center vertically it has to be lowered half its height, plus a compensation for half the height of a letter.

\documentclass{article}
\usepackage{calc}
\usepackage{colortbl}
\usepackage{tabu}
\usepackage{array}
%\newcolumntype{K}[1]{>{a\vbox to #1{b\vfill c}d\centering}m{#1}}
\newcolumntype{K}[1]{>{\rule[0.5\ht\strutbox-#1/2]{0pt}{#1}\centering}m{#1-2\tabcolsep}}
\begin{document}
\begin{tabu}{|[1pt]*{3}{K{10mm}|K{10mm}|[1pt]}}
\tabucline[1pt]{1-6}
0  &  4  & \i  &  4  & \i  & \i  \\ \tabucline{1-6}
2  &  0  & \i  & \i  & \i  & -3  \\ \tabucline[1pt]{1-6}
\i & \i  &  0  &  \cellcolor[gray]{0.8}3  &  5  & -1  \\ \tabucline{1-6}
4  & \i  &  3  &  0  & -6  & \i  \\ \tabucline[1pt]{1-6}
-3 & \i  & \i  & -6  &  0  &  9  \\ \tabucline{1-6}
\i & \i  & -1  &  5  &  7  &  0  \\ \tabucline[1pt]{1-6}
\end{tabu}
\end{document}

enter image description here

StefanH
  • 13,823
  • Thank you for answer! It's exactly what I was looking for!! :) – WalkerTR Aug 11 '17 at 15:03
  • I just tried to color a few cells using cellcolor from the package colortbl, but the command colors a much bigger area. Do you have a solution for this? – WalkerTR Aug 12 '17 at 07:38
  • @WalkerTR, I have updated the solution. In the previous I removed horizontal space with @{} in the column argument. This is moved to the column definition, which seems to solve the issue for colortbl. – StefanH Aug 12 '17 at 12:36