# How to align the numbers horizontally?

Here are my examples and the output.

\begin{center}
\begin{tabular}{lp{.8\linewidth}}
Group ID: & $1 \quad 2 \quad 3 \quad 4 \quad 5 \quad 6 \quad 7 \quad 8 \quad 9 \quad 10 \quad 11 \quad 12 \quad 13 \quad 14 \quad 15$\\
$\underbrace{\text{Group size}}_\text{\# of overlaps}$: & $\underbrace{3 \quad 3}_1 \quad 3 \quad \underbrace{6 \quad 6}_2 \quad 6 \quad \underbrace{9 \quad 9}_3 \quad 9 \quad \underbrace{15 \quad 15}_5 \quad 15 \quad \underbrace{24 \quad 24}_8 \quad 24$\\
\end{tabular}
\end{center}


What I wish to achieve is to align each of the group ID numbers with its size below it. The two rows cannot align perfectly now is due to the under braces.

Moreover, I wish to indicate number of overlapping variables between groups in the second row. I did it by using underbrace, as shown in the second row. And the notation for this is done by \underbrace{\text{Group size}}_\text{\# of overlaps}. The minor question is, does this notation look professional? Or is there a better way to convey this information. I am working on a research paper so wish to be a little professional.

Update:

A new issue occurred. If Group size is all 10, the alignment issue came up again, and none of current solutions can handle that. How could this new issued be fixed? Thanks.

Use an outer array for ensuring constant spacing and inner arrays for the groups:

\documentclass{article}
\usepackage{amsmath}

\newcommand{\dbl}[5]{%
\underbrace{\begin{array}[b]{@{}cc@{}}
#1 & #2 \\
#3 & #4
\end{array}}_{#5}%
}
\newcommand{\sgl}[2]{%
\begin{array}[b]{@{}c@{}}
#1 \\ #2
\end{array}%
}
\newcommand{\hdr}[3]{%
\underbrace{\begin{array}[b]{@{}l@{}}
\text{#1} \\ \text{#2}
\end{array}}_{\text{#3}}%
}

\begin{document}

\begin{equation*}
\begin{array}{@{}*{9}{c}@{}}
\hdr{Group ID:}{Group size:}{\# of overlaps} &
\dbl{1}{2}{10}{10}{3} &
\sgl{3}{10} &
\dbl{4}{5}{10}{10}{3} &
\sgl{6}{10} &
\dbl{7}{8}{10}{10}{3} &
\sgl{9}{10} &
\dbl{10}{11}{10}{10}{3} &
\sgl{12}{10}
\end{array}
\end{equation*}

\end{document}


To accommodate for any minute shifts, use separate (centred) columns:

\documentclass{article}
\usepackage{amsmath}
\begin{document}

\begin{center}
Group ID: & $1 \quad 2$ & $3$ & $4 \quad 5$ & $6$ & $7 \quad 8$ & $9$ & $10 \quad 11$ & $12$ & $13 \quad 14$ & $15$ \\
Group size: & $\underbrace{3 \quad 3}_1$ & $3$ & $\underbrace{6 \quad 6}_2$ & $6$ &
$\underbrace{9 \quad 9}_3$ & $9$ & $\underbrace{15 \quad 15}_5$ & $15$ & $\underbrace{24 \quad 24}_8$ & $24$
\end{tabular}
\end{center}

\end{document}


I would forego the use of \# of overlaps for the Group size, as this can be referenced in the text explaining the tabulated data.

• BTW, what does {l *{10} option after \begin{tabular} do? – SixSigma Jul 2 '15 at 17:06
• @AaronZeng: *{n}{<col spec>} will repeat <col spec> a total of n times. – Werner Jul 2 '15 at 18:53

another way of looking at this: in the second row, there is a repeating pattern. take advantage of it.

\documentclass{article}
\usepackage{amsmath}

\newcommand{\myubrace}[2]{%
\rlap{$\underbrace{#1 \quad #1}_{#2}$}\phantom{#1\quad#1}

\begin{document}
\begin{center}
\begin{tabular}{lp{.8\linewidth}}
Group ID: & $1 \quad 2 \quad 3 \quad 4 \quad 5 \quad 6 \quad 7 \quad 8 \quad 9 \quad 10 \quad 11 \quad 12 \quad 13 \quad 14 \quad 15$\\
$\underbrace{\text{Group size}}_\text{\# of overlaps}$:
&$\myubrace{3}{1} \myubrace{6}{2} \myubrace{9}{3} \myubrace{15}{5} \myubrace{24}{8}$
\end{tabular}
\end{center}
\end{document}


Slightly simplified @Werner's code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{array}

\begin{document}

\begin{center}
\begin{tabular}{l *{10}{@{\quad}>{$}c<{$}}}
Group ID: & 1 \quad 2 & 3 & 4 \quad 5 & 6 & 7 \quad 8 & 9 & 10 \quad 11 & $12$ & 13 \quad 14 & 15 \\
Group size: & \underbrace{3 \quad 3}_1 & 3 & \underbrace{6 \quad 6}_2 & 6 &
\underbrace{9 \quad 9}_3 & 9 & \underbrace{15 \quad 15}_5 & 15 & \underbrace{24 \quad 24}_8 & 24
\end{tabular}
\end{center}

\end{document}

• BTW, what does {l *{10} option after \begin{tabular} do? – SixSigma Jul 2 '15 at 17:18
• l is for left align cell, *{10}{...} means ten times columns as determined in .... In my answer this is >{$}c<{$}, where >{$} and <{$} says, that content in this column is in math mode. – Zarko Jul 2 '15 at 21:44