多列宽度不均匀

多列宽度不均匀

我正在尝试用此代码创建一个表;

\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}


但标有“head 4”的第 4 个子列比其他子列大。我希望所有 4 列宽度相同,有什么建议吗?祝一切顺利,

答案1

使用makecelltabularx包:

\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}

在此处输入图片描述

答案2

array使用m所需宽度的列定义一个新的列类型(包)。(m将单元格垂直居中multicolumn)。

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

使用相同的包可以垂直扩展单元格

\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

答案3

使用({NiceTabular}nicematrix需要多次编译)。

 \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}

该键hvlines绘制所有规则,但由创建的块除外Block(这就是为什么我添加了一个空块\Block{2-1}{}以避免在最左上角的单元格下出现规则)。

上述代码的输出

相关内容