2

I am trying to create a table with this code;

\begin{table}[ht]
\centering\begin{tabular}{|c|c|c|c|c|}
\hline
Mean Partnership Duration & \multicolumn  {4}{c|}{\centering Number of simulations resulting in an epidemic}\\

\cline{2-5}
No. & head 1 & head 2& head 3 & head 4\\
\hline
1 & data1 & data2 & data 3 & data4\\
\hline
2 & data1 & data2 & data 3 & 4\\
\hline
3 & data1 & data2 & data 3& 4\\
\hline
4 & data1 & data2 & data 3& 4\\
\hline
\end{tabular}
\caption{This is the ideal table}
\label{My table}
\end{table}


But the 4th subcolumn labelled 'head 4' is larger than the other subcolumns. I want all 4 columns to be equal in width, Any advice? All the best,

3
  • Welcome. Does this (tex.stackexchange.com/questions/5017/…) answer your question? – Excelsior Mar 29 at 20:14
  • @DavidCarlisle apologies I meant Multicolumn – Charlie Cameron Mar 29 at 21:07
  • Hi! Now you have three answers :-). You may consider to up-vote each of them you liked ( by clicking on up-triangle at top left side of answers) and accept one of the, which solve your problem on the best way ( by clicking on check mark at top left side of selected answer). – Zarko Mar 30 at 10:44
1

With use of the makecell and tabularx packages:

\documentclass[12pt]{article}
\usepackage{makecell, tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}
    \begin{table}[ht]
    \centering
    \setcellgapes{3pt}
    \makegapedcells
\begin{tabularx}{\linewidth}{|*{5}{C|} }
    \hline
\makecell{Mean\\ Partnership\\ Duration}
    & \multicolumn{4}{c|}{Number of simulations resulting in an epidemic}\\
\cline{2-5}
No. & head 1 & head 2 & head 3 & head 4\\
\hline
1   & data 1 & data 2 & data 3 & data4\\
\hline
2   & data 1 & data 2 & data 3 & 4\\
\hline
3   & data 1 & data 2 & data 3 & 4\\
\hline
4   & data 1 & data 2 & data 3 & 4\\
\hline
\end{tabularx}
\caption{This is the ideal table}
\label{My table}
    \end{table}
\end{document}

enter image description here

1
1

Define a new column type (package array) using an m column of the desired width. (m will center vertically the multicolumn cell).

\newcolumntype{C}{>{\centering \arraybackslash}m{0.2\textwidth}}

Using the same package it is possible expand the cells vertically

\renewcommand{\arraystretch}{<a number>}

x

\documentclass[12pt,a4paper]{article}

\usepackage{showframe} % show margins

\usepackage{calc}%<<<<<<<<<<<<< added
\usepackage{array} %<<<<<<<<<<<<< added
\newcolumntype{C}{>{\centering \arraybackslash}m{(\textwidth-(6\arrayrulewidth)-(10\tabcolsep))/5}}

\renewcommand{\arraystretch}{1.8} %<<<<<<<<<<<<<<< optional
    
\begin{document}
    
    \begin{table}[ht]
        \centering
        \begin{tabular}{|C|C|C|C|C|}
            \hline
            Mean Partnership Duration & \multicolumn{4}{c|}{Number of simulations resulting in an epidemic}\\           
            \cline{2-5}
            No. & head 1 & head 2& head 3 & head 4\\
            \hline
            1 & data1 & data2 & data 3 & data4\\
            \hline
            2 & data1 & data2 & data 3 & 4\\
            \hline
            3 & data1 & data2 & data 3& 4\\
            \hline
            4 & data1 & data2 & data 3& 4\\
            \hline
        \end{tabular}
        \caption{This is the ideal table}
        \label{My table}
    \end{table} 
    
\end{document
1
  • Your tabular is too wide. You should replace 0.2\textwidth by (\textwidth-(6\arrayrulewidth)-(10\tabcolsep))/5. – F. Pantigny Mar 30 at 9:06
0

With {NiceTabular} of nicematrix (you need several compilations).

 \documentclass[12pt,a4paper]{article}
 \usepackage{nicematrix}

 \begin{document}

 \renewcommand{\arraystretch}{1.8}
     
 \begin{table}[ht]
 \centering
 \begin{NiceTabular}{ccccc}[hvlines,columns-width=auto]
 \Block{2-1}{}
 \Block{}{Mean\\ Partnership\\ Duration} & \Block{1-4}{Number of simulations resulting in an epidemic}\\ 
 No. & head 1 & head 2& head 3 & head 4\\
 1 & data1 & data2 & data 3 & data4\\
 2 & data1 & data2 & data 3 & 4\\
 3 & data1 & data2 & data 3& 4\\
 4 & data1 & data2 & data 3& 4\\
 \end{NiceTabular}
 \caption{This is the ideal table}
 \label{My table}
 \end{table} 
     
 \end{document}

The key hvlines draws all the rules, excepted in the blocks created by Block (that's why I have added an empty block \Block{2-1}{} to avoid a rule under the left-uppermost cell.

Output of the above code

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.