TransWikia.com

Tabular column width evenly distributed

TeX - LaTeX Asked by Violet on January 29, 2021

Consider this table I made. It’s actually much longer but for the purpose of illustration I’ve removed most rows.

begin{table}[h]
begin{tabular}{ccccccccc}
toprule
                         &                                 &                       &                        &                                    & multicolumn{4}{l}{Distance as determined by Algorithm}  cmidrule(l){6-9} 
multirow{-2}{*}{Leaves} & multirow{-2}{*}{Reticulations} & multirow{-2}{*}{$N$} & multirow{-2}{*}{$N'$} & multirow{-2}{*}{$d_{tail}(N,N')$} & 2.1          & 2.2          & 2.3         & 2.4          midrule
5                        & 1                               & 7                     & 8                      & 3                                  & 5            & 5            & 5           & 5           
rowcolor[HTML]{EFEFEF} 
5                        & 1                               & 8                     & 9                      & 2                                  & 2            & 2            & 2           & 2            bottomrule
end{tabular}
end{table}

enter image description here

I don’t know how to evenly distribute the cells underneath "distance as determined by Algorithm" in terms of width. I’ve tried using tabularx, but I couldn’t get it right.

2 Answers

I suggest you employ a tabularx environment, set its width to textwidth, and assign a centered version of the X column type to 7 of the 9 columns. This setup will create an appearance of roughly equally wide columns.

enter image description here

documentclass{article}
usepackage[table]{xcolor}
usepackage{booktabs,ragged2e,amsmath,tabularx}
newcolumntype{C}{>{Centeringhspace{0pt}}X}

begin{document}
begin{table}
setlengthtabcolsep{2pt} % default: 6pt
begin{tabularx}{textwidth}{ cCCC c CCCC }
toprule
Leaves & Reticulations & $N$ & $N'$ & $d_{mathrm{tail}}(N,N')$ & 
multicolumn{4}{>{hsize=dimexpr4hsize+6tabcolseprelax}C}{%
     Distance as determined by algorithm} 
cmidrule(l){6-9}
&&&&& 2.1 & 2.2 & 2.3 & 2.4  
midrule
5 & 1 & 7 & 8 & 3 & 5 & 5 & 5 & 5 
rowcolor[HTML]{EFEFEF} 
5 & 1 & 8 & 9 & 2 & 2 & 2 & 2 & 2  
bottomrule
end{tabularx}
end{table}
end{document}

Answered by Mico on January 29, 2021

Here are two solutions, one with the new w column type, and the other with tabularx, and the help of the makecell package, which allows for line breaks in standard column types:

documentclass{article}

usepackage[table]{xcolor}
usepackage{booktabs}
usepackage{tabularx, multirow}
usepackage{makecell}

begin{document}

begin{table}[h]
centering
begin{tabular}{*{5}{c}*{4}{wc{6mm}}}
toprule
                         & & & & & multicolumn{4}{c}{makecell{Distance as determined by Algorithm}}  cmidrule(l){6-9}
multirow{-2}{*}{Leaves} & multirow{-2}{*}{Reticulations} & multirow{-2}{*}{$N$} & multirow{-2}{*}{$N'$} & multirow{-2}{*}{$d_{mbox{tail}}(N,N')$} & 2.1 & 2.2 & 2.3 & 2.4  midrule
5 & 1 & 7 & 8 & 3 & 5 & 5 & 5 & 5 
rowcolor[HTML]{EFEFEF}
5 & 1 & 8 & 9 & 2 & 2 & 2 & 2 & 2  bottomrule
end{tabular}
vskip1cm

begin{tabularx}{linewidth}{*{5}{c}*{4}{>{centeringarraybackslash}X}}
toprule
                         & & & & & multicolumn{4}{c}{makecell{Distance as determined by Algorithm}}  cmidrule(l){6-9}
multirow{-2}{*}{Leaves} & multirow{-2}{*}{Reticulations} & multirow{-2}{*}{$N$} & multirow{-2}{*}{$N'$} & multirow{-2}{*}{$d_{mbox{tail}}(N,N')$} & 2.1 & 2.2 & 2.3 & 2.4  midrule
5 & 1 & 7 & 8 & 3 & 5 & 5 & 5 & 5 
rowcolor[HTML]{EFEFEF}
5 & 1 & 8 & 9 & 2 & 2 & 2 & 2 & 2  bottomrule
end{tabularx}
end{table}

end{document} 

enter image description here

Answered by Bernard on January 29, 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