1

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.

1
  • Welcome to TeX.SE.
    – Mico
    Commented Aug 3, 2020 at 21:23

2 Answers 2

1

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}{>{\Centering\hspace{0pt}}X}

\begin{document}
\begin{table}
\setlength\tabcolsep{2pt} % default: 6pt
\begin{tabularx}{\textwidth}{ cCCC c CCCC }
\toprule
Leaves & Reticulations & $N$ & $N'$ & $d_{\mathrm{tail}}(N,N')$ & 
\multicolumn{4}{>{\hsize=\dimexpr4\hsize+6\tabcolsep\relax}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}
1
  • 1
    Yes, perfect! Thank you! Also thanks for welcoming me. :)
    – Ruby
    Commented Aug 3, 2020 at 21:49
2

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}{>{\centering\arraybackslash}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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .