tabularx 列宽不符合预期

tabularx 列宽不符合预期

我正在尝试尽可能接近地重现以下表格设计:

在此处输入图片描述

这就是我现在所拥有的:

\documentclass[12pt,a4paper]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}
\newcolumntype{Y}{>{\centering\arraybackslash}X}

\begin{document}
    \begin{tabularx}{\textwidth}{X X X X X X}
        \hline
        \multicolumn{3}{|Y|}{Width 1/2} & \multicolumn{3}{Y|}{Width 1/2} \\
        \hline
        \multicolumn{3}{|X|}{
            \begin{itemize}
                \item Item 1
                \item Item 2
                \item Item 3
            \end{itemize}
        } & \multicolumn{3}{X|}{
            \begin{itemize}
                \item Item 1
                \item Item 2
                \item Item 3
            \end{itemize}
        } \\
        \hline
        \multicolumn{6}{|Y|}{Width 1} \\
        \hline
        \multicolumn{6}{|X|}{
            \begin{itemize}
                \item Item 1
                \item Item 2
                \item Item 3
            \end{itemize}
        } \\
        \hline
        \multicolumn{2}{|Y|}{Width 1/3} & \multicolumn{2}{Y|}{Width 1/3} & \multicolumn{2}{Y|}{Width 1/3} \\
        \hline
        \multicolumn{2}{|Y|}{
            \begin{itemize}
                \item Item 1
                \item Item 2
                \item Item 3
            \end{itemize}
        } & \multicolumn{2}{Y|}{
            \begin{itemize}
                \item Item 1
                \item Item 2
                \item Item 3
            \end{itemize}
        } & \multicolumn{2}{Y|}{
            \begin{itemize}
                \item Item 1
                \item Item 2
                \item Item 3
            \end{itemize}
        } \\
        \hline
    \end{tabularx}
\end{document}

然而我最终得到了这个:

  • 列宽不起作用
  • 文本未垂直居中
  • 我不确定单元格中的文本是否会换行

我怎样才能改善结果?我打算使用 Word/Excel 来实现这一点……

结果

答案1

对单元格内容和标题的垂直居中进行了轻微的改进:

\documentclass[12pt,x11names, a4paper]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{array}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\setlength\extrarowheight{3pt}
\usepackage{ragged2e}
\usepackage{xcolor, colortbl}
\usepackage{etoolbox}

\usepackage{enumitem}
\AtBeginEnvironment{tabular}{%
\setlist[itemize, 1,2]{label=\color{LightSteelBlue3}\textbullet, leftmargin = 0.8em,nosep, before =\leavevmode\vspace{-\baselineskip}\RaggedRight, after=\leavevmode\vspace{\dimexpr-\baselineskip + \extrarowheight\relax},}%itemsep=1pt, , topsep=0pt,
}
\begin{document}
\noindent
    \begin{tabular}{cccccc}
        \hline
        \multicolumn{6}{|c|}{\cellcolor{LightSteelBlue3}\sffamily\bfseries\color{white}Title}\\
        \hline
        \multicolumn{3}{|p{\dimexpr(\textwidth-3\arrayrulewidth-4\tabcolsep)/2\relax}|}{
            \begin{itemize}
                \item Item 1
                \item Item 2
                \item Item 3
            \end{itemize}
        } & \multicolumn{3}{|p{\dimexpr(\textwidth-3\arrayrulewidth-4\tabcolsep)/2\relax}|}{
            \begin{itemize}
                \item Item 1
                \item Item 2
                \item Item 3
            \end{itemize}
        } \\
        \hline
        \multicolumn{6}{|c|}{Width 1} \\
        \hline
        \multicolumn{6}{|p{\dimexpr\textwidth-2\arrayrulewidth-2\tabcolsep\relax}|}{
            \begin{itemize}
                \item Item 1
                \item Item 2
                \item Item 3
            \end{itemize}
        } \\
        \hline
        \multicolumn{2}{|c|}{Width 1/3} & \multicolumn{2}{c|}{Width 1/3} & \multicolumn{2}{c|}{Width 1/3} \\
        \hline
        \multicolumn{2}{|p{\dimexpr(\textwidth-4\arrayrulewidth-6\tabcolsep)/3\relax}|}{
            \begin{itemize}
                \item Item 1
                \item Item 2
                \item Item 3
            \end{itemize}
        } & \multicolumn{2}{p{\dimexpr(\textwidth-4\arrayrulewidth-6\tabcolsep)/3\relax}|}{
            \begin{itemize}
                \item Item 1
                \item Item 2
                \item Item 3
            \end{itemize}
        } & \multicolumn{2}{p{\dimexpr(\textwidth-4\arrayrulewidth-6\tabcolsep)/3\relax}|}{
            \begin{itemize}
                \item Item 1
                \item Item 2
                \item Item 3
            \end{itemize}
        } \\
        \hline
    \end{tabular}
\end{document} 

在此处输入图片描述

答案2

您确实不想tabularx在这里这样做,因为您事先知道您希望列有多宽。您的尝试失败了,因为tabularx本质上进行了一些计算,然后用某个宽度 x(每种情况下的值都相同)替换每个X单元格p{x},但您想要的是宽度为一半、全部或三分之一的单元格。

在此处输入图片描述

\documentclass[12pt,a4paper]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{array}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\setlength\extrarowheight{3pt}
\begin{document}
\noindent
    \begin{tabular}{cccccc}
        \hline
        \multicolumn{6}{|c|}{Title}\\
        \hline
        \multicolumn{3}{|p{\dimexpr(\textwidth-3\arrayrulewidth-4\tabcolsep)/2\relax}|}{
            \begin{itemize}
                \item Item 1
                \item Item 2
                \item Item 3
            \end{itemize}
        } & \multicolumn{3}{|p{\dimexpr(\textwidth-3\arrayrulewidth-4\tabcolsep)/2\relax}|}{
            \begin{itemize}
                \item Item 1
                \item Item 2
                \item Item 3
            \end{itemize}
        } \\
        \hline
        \multicolumn{6}{|c|}{Width 1} \\
        \hline
        \multicolumn{6}{|p{\dimexpr\textwidth-2\arrayrulewidth-2\tabcolsep\relax}|}{
            \begin{itemize}
                \item Item 1
                \item Item 2
                \item Item 3
            \end{itemize}
        } \\
        \hline
        \multicolumn{2}{|c|}{Width 1/3} & \multicolumn{2}{c|}{Width 1/3} & \multicolumn{2}{c|}{Width 1/3} \\
        \hline
        \multicolumn{2}{|p{\dimexpr(\textwidth-4\arrayrulewidth-6\tabcolsep)/3\relax}|}{
            \begin{itemize}
                \item Item 1
                \item Item 2
                \item Item 3
            \end{itemize}
        } & \multicolumn{2}{p{\dimexpr(\textwidth-4\arrayrulewidth-6\tabcolsep)/3\relax}|}{
            \begin{itemize}
                \item Item 1
                \item Item 2
                \item Item 3
            \end{itemize}
        } & \multicolumn{2}{p{\dimexpr(\textwidth-4\arrayrulewidth-6\tabcolsep)/3\relax}|}{
            \begin{itemize}
                \item Item 1
                \item Item 2
                \item Item 3
            \end{itemize}
        } \\
        \hline
    \end{tabular}
\end{document}

相关内容