# To align numbers and boxes that appear in table column

I have the following table where the columns consist of numbers and a box symbol. Unfortunately it looks messy because the numbers and boxes aren't aligned. Is there a way to align them. This is my MWE.

\documentclass[12pt,a4paper,twoside]{report}
\usepackage{tabularx,ragged2e,booktabs,caption}
\usepackage{amsmath,amsthm,mathtools}
\begin{document}
\begin{table}[!htbp]
\centering
\begin{tabular}{
|c|c|c@{\hskip 1cm}
|c|c|c@{\hskip 1cm}
|c|c|c@{\hskip 1cm}
}
\cline{1-2}\cline{4-5}\cline{7-8}
$n$ & $w_n \pmod{29}$ && $n$ & $w_n \pmod{29}$ && $n$ & $w_n \pmod{29}$ \\
\cline{1-2}\cline{4-5}\cline{7-8}
1 & 1 $\Box$ && 35 & 16 $\Box$ && 69 & 22 $\Box$ \\
\cline{1-2}\cline{4-5}\cline{7-8}
2 & 1 $\Box$ && 36 & 18 $\boxtimes$ && 70 & 4 $\Box$ \\
\cline{1-2}\cline{4-5}\cline{7-8}
3 & 1 $\Box$ && 37 & 5 $\Box$ && 71 & 23 $\Box$ \\
\cline{1-2}\cline{4-5}\cline{7-8}
4 & 19 $\boxtimes$ && 38 & 10 $\boxtimes$ && 72 & 28 $\Box$ \\
\cline{1-2}\cline{4-5}\cline{7-8}
5 & 13 $\Box$ && 39 & 20 $\Box$ && 73 & 22 $\Box$ \\
\cline{1-2}\cline{4-5}\cline{7-8}
6 & 11 $\boxtimes$ && 40 & 28 $\Box$ && 74 & 24 $\Box$ \\
\cline{1-2}\cline{4-5}\cline{7-8}
\end{tabular}
\captionof{table}{$B_n=4w_n^2$} \label{table:Bnwn29}
\end{table}
\end{document}

• Please add documentclass, begin and end document, packages and preamble material needed to run your MWE. – Runar Sep 10 '16 at 10:47
• where is defined \swb? – Zarko Sep 10 '16 at 11:07
• and please provide image of table, which you obtain with your code. – Zarko Sep 10 '16 at 11:14
• \swb is still not defined ... – Zarko Sep 10 '16 at 11:19
• Sorry. It turns out I am not suppose to use \swb. I changed it to \Box – shahrina ismail Sep 10 '16 at 11:22

I guess that you like to obtain something like this:

In all your MWE version is not defined nor \swb nor \Box, so I (re)define \swb as \square from amssymb package. For aligning I introduce one more column in each, "table" and change columns type:

\documentclass[12pt,a4paper,twoside]{report}
\usepackage{tabularx,ragged2e,booktabs,caption}
\usepackage{amssymb,mathtools}
\newcommand\swb{\square}

\begin{document}
\begin{table}[!htbp]
\centering
\begin{tabular}{*{3}{|>{$}c<{$}
|>{\raggedleft $}p{1cm}<{$}@{\,}>{$}p{1cm}<{$}
|>{$}c<{$}@{\hskip 1cm}} }
\cline{1-3}\cline{5-7}\cline{9-11}
n   &  \multicolumn{2}{c|}{$w_n \pmod{29}$}
&&  n   &  \multicolumn{2}{c|}{$w_n\pmod{29}$}
&&  n   &  \multicolumn{2}{c|}{$w_n\pmod{29}$}              \\
\cline{1-3}\cline{5-7}\cline{9-11}
1   & 1  & \swb         && 35   & 16  & \swb        && 69   & 22  & \swb\\
\cline{1-3}\cline{5-7}\cline{9-11}
2   & 1  & \swb         && 36   & 18  & \boxtimes   && 70   &  4  & \swb\\
\cline{1-3}\cline{5-7}\cline{9-11}
3   & 1  & \swb         && 37   & 5   & \swb        && 71   & 23  & \swb\\
\cline{1-3}\cline{5-7}\cline{9-11}
4   & 19 & \boxtimes    && 38   & 10  & \boxtimes   && 72   & 28  & \swb\\
\cline{1-3}\cline{5-7}\cline{9-11}
5   & 13 & \swb         && 39   & 20  & \swb        && 73   & 22  & \swb\\
\cline{1-3}\cline{5-7}\cline{9-11}
6   & 11 & \boxtimes    && 40   & 28  & \swb        && 74   & 24  & \swb\\
\cline{1-3}\cline{5-7}\cline{9-11}
7   & 15 &              && 41   & 10  & \swb        && 75   & 10  &            \\
\cline{1-3}\cline{5-7}\cline{9-11}
\end{tabular}
\caption{$B_n=4w_n^2$}
\label{table:Bnwn29}
\end{table}
\end{document}


Is this what you looking for?

Edit: it seems that my answer is close to what you like to obtain :-), so some explanations:

• column type p{...} is selected that equal with colummns for numbers and symbols can be obtain
• space between p{...} columns in above MWE is left to @{...}. Temporary set to @{\,}. If you like to have bigger space, than you replace @{\,} with @{\hspace{<length>}
• maby the natural distance betwee tabule columns is adequate. In this case you only delete @{\,} (see table image for this case below)

and changed table columns options definition:

   \begin{tabular}{*{3}{|>{$}c<{$}
|>{\raggedleft $}p{1cm}<{$} >{$}p{1cm}<{$}
|>{$}c<{$}@{\hskip 1cm}} }


Addendum: Let mi show two variant of above table design, which are more according to my taste:

A MWE for it is:

\documentclass[12pt,a4paper,twoside]{report}
\usepackage{tabularx,ragged2e,booktabs,caption}
\usepackage{amssymb,mathtools}
\newcommand\swb{\square}

\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\newcolumntype{R}{>{\raggedleft \arraybackslash}X}

\begin{document}
\begin{table}[htb]
\centering
\begin{tabularx}{0.25\textwidth}[t]{%
|>{$}c<{$}
|>{$}R<{$} >{$}L<{$}|}
\hline
n   &   \multicolumn{2}{c|}{$w_n \pmod{29}$}    \\
\hline
1   &   1   & \swb                              \\
\hline
\end{tabularx}\hfil
\begin{tabularx}{0.3\textwidth}[t]{%
|>{$}c<{$}
|>{$}R<{$} >{$}L<{$}|}
\hline
n   &   \multicolumn{2}{c|}{$w_n \pmod{29}$}    \\
\hline
35   & 16  & \swb                               \\
\hline
\end{tabularx}
\hfil
\begin{tabularx}{0.3\textwidth}[t]{%
|>{$}c<{$}
|>{$}R<{$} >{$}L<{$}|}
\hline
n   &   \multicolumn{2}{c|}{$w_n \pmod{29}$}    \\
\hline
69   & 22  & \swb                               \\
\hline
\end{tabularx}
\caption{$B_n=4w_n^2$}
\label{table:Bnwn29}
\end{table}
\begin{table}[htb]
\centering
\begin{tabularx}{0.25\textwidth}[t]{%
>{$}c<{$}
>{$}R<{$} >{$}L<{$}}
\toprule
n   &   \multicolumn{2}{c}{$w_n \pmod{29}$}     \\
\midrule
1   &   1   & \swb                              \\
\bottomrule
\end{tabularx}\hfil
\begin{tabularx}{0.3\textwidth}[t]{%
>{$}c<{$}
>{$}R<{$} >{$}L<{$}}
\toprule
n   &   \multicolumn{2}{c}{$w_n \pmod{29}$}     \\
\midrule
35   & 16  & \swb                               \\
\bottomrule
\end{tabularx}
\hfil
\begin{tabularx}{0.3\textwidth}[t]{%
>{$}c<{$}
>{$}R<{$} >{$}L<{$} }
\toprule
n   &   \multicolumn{2}{c}{$w_n \pmod{29}$}     \\
\midrule
69   & 22  & \swb                               \\
\bottomrule
\end{tabularx}
\caption{$B_n=4w_n^2$}
\label{table:Bnwn29}
\end{table}
\end{document}

• I'd split the table into three top-aligned ones; also makes it easier to restructure the tables if lines are added or the number of tables is changed. But this is a different story ... – gernot Sep 10 '16 at 11:48
• I agree with you, but I already put quit a lot effort to redesign table (introduce new column) ... and to make OP MWE compilable. Maybe latter I will do this too (if someone else will not do this meanwhile :-) ) – Zarko Sep 10 '16 at 11:51
• @Zarko thank you for your effort. Yes this is what I am looking for, except that it would be great if there's a little gap between the numbers and the box symbol. – shahrina ismail Sep 10 '16 at 12:02
• @gernot In a simular but earlier question by the OP, an answer was given which splits the table into 3 tables, so the OP already knows this. Probably not worth redoing it. See tex.stackexchange.com/questions/327659/… – Runar Sep 10 '16 at 12:07
• @shahrinaismail, see edit of my answer. gap can be simple adjust wit set appropriate value in @{...} between p{...} columns – Zarko Sep 10 '16 at 12:13

Here's a solution with siunitx and 6 more columns in the table (because of the wide column heads). As I don't know why you use \captionof{table} within a table, I restored the normal \caption:

\documentclass[12pt,a4paper,twoside]{report}
\usepackage{tabularx,ragged2e,booktabs,caption}
\usepackage{amsthm,amssymb, mathtools}
\usepackage{siunitx}
\newcommand\myBox{\enspace\ensuremath{\Box}}
\newcommand\Boxt{\enspace\ensuremath{\boxtimes}}
\def\modhead{\multicolumn{3}{c|}{$w_n \pmod{29}$}}

\begin{document}

\begin{table}[!htbp]
\centering\sisetup{table-format =2.0, table-number-alignment=center, table-space-text-pre = {$\Box$}}
\setlength\extrarowheight{3pt}
\begin{tabular}{
|c|cSc|c@{\hskip 1cm}
|c|cSc|c@{\hskip 1cm}
|c|cSc|c@{\hskip 1cm}
}
\cline{1-4}\cline{6-9}\cline{11-14}
$n$ & \modhead && $n$ & \modhead  && $n$ &  \modhead  \\
\cline{1-4}\cline{6-9}\cline{11-14}
1 &  & 1 \myBox & & & 35 & & 16  \myBox   & & & 69 & & 22 \myBox & \\
\cline{1-4}\cline{6-9}\cline{11-14}
2 & &  1  \myBox   & & & 36 & & 18 \Boxt & & & 70 & & 4 \myBox & \\
\cline{1-4}\cline{6-9}\cline{11-14}
3 & &  1  \myBox  & & & 37 & & 5  \myBox  & & & 71 & & 23 \myBox & \\
\cline{1-4}\cline{6-9}\cline{11-14}
4 & &  19\Boxt & & & 38 & & 10 \Boxt  & & & 72 & & 28 \myBox & \\
\cline{1-4}\cline{6-9}\cline{11-14}
5 &  & 13  \myBox  & & & 39  & & 20  \myBox  & & & 73 & & 22 \myBox & \\
\cline{1-4}\cline{6-9}\cline{11-14}
6 &  & 11 \Boxt  & & & 40 & & 28  \myBox  & & & 74 & & 24 \myBox & \\
\cline{1-4}\cline{6-9}\cline{11-14}
\end{tabular}
\caption{$B_n=4w_n^2$} \label{table:Bnwn29}
\end{table}

\end{document}


The following example uses three tables. This simplifies the construction of the table and is much easier to maintain, especially if the data changes and needs rearrangements.

The first column shows the alignment with package siunitx, the second column uses a different approach:

• The numbers are aligned by adding an invisible digit for the numbers with one digit. As shorthand the active character ~ is locally redefined.

• The boxes are set with three locally defined macros:

• \B: an empty box
• \X: box with cross
• \N: without box, but with the horizontal space of an box.

The box symbols can easily be redefined by redefining these macros. The example uses \Square and \XBox of package wasysym.

• The number of lines are much reduced and the few remaining horizontal lines are set with macros of package booktabs to get nicer rules with better vertical spacing.

Full example:

\documentclass[12pt, a4paper, twoside]{report}

\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{wasysym}

\begin{document}
\begin{table}
\centering
\newcommand*{\MyTable}[1]{%
\begin{tabular}{
S[table-format=2]
c
}%
\toprule
$n$ & $w_n\pmod{29}$\\
\midrule
#1%
\bottomrule
\end{tabular}%
}
\def\B{\Square}
\def\X{\XBox}
\def\N{\hphantom{\XBox}}
\def~{\hphantom{0}}

\MyTable{
1 & ~1 \B\\
2 & ~1 \B\\
3 & ~1 \B\\
4 & 19 \X\\
5 & 13 \B\\
6 & 11 \X\\
7 & 15 \N\\
8 & ~9 \B\\
9 & 13 \B\\
10 & 18 \N\\
11 & ~4 \B \\
}\hfill
\MyTable{
35 & 16 \B\\
36 & 18 \X\\
37 & ~5 \B\\
38 & 10 \X\\
39 & 20 \B\\
40 & 28 \B\\
41 & 10 \X\\
42 & 15 \N\\
43 & 13 \B\\
44 & 21 \N\\
45 & ~1 \B\\
}\hfill
\MyTable{
69 & 22 \B\\
70 & ~4 \B\\
71 & 23 \B\\
72 & 28 \B\\
73 & 22 \B\\
74 & 24 \B\\
75 & 10 \N\\
76 & ~9 \B\\
77 & ~5 \B\\
78 & ~1 \B\\
79 & 24 \B\\
}
\end{table}
\end{document}