TransWikia.com

How to align the numbers horizontally?

TeX - LaTeX Asked by SixSigma on May 21, 2021

Here are my examples and the output.

begin{center}
begin{tabular}{lp{.8linewidth}}
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}

enter image description here

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.

enter image description here

4 Answers

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}

enter image description here

Correct answer by egreg on May 21, 2021

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

enter image description here

documentclass{article}
usepackage{amsmath}
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}

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

Answered by Werner on May 21, 2021

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}

Answered by Zarko on May 21, 2021

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{#1quad#1}
        quad #1 quad}

begin{document}
begin{center}
begin{tabular}{lp{.8linewidth}}
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}

output of example code

Answered by barbara beeton on May 21, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP