表格嵌套的列宽度基于统一的父级宽度,而不是统一的子级宽度

表格嵌套的列宽度基于统一的父级宽度,而不是统一的子级宽度

我认为我使用了错误的搜索查询来表达这一点,但我试图制作一个表格,其中包含的单元格的宽度由其父母的一致性决定,而不是父母的宽度由其子女的一致性决定。

我的例子是我想让我的表格看起来像这样:

在此处输入图片描述

正如您所见,标题栏的col1 == col2 == col3宽度为。

子单元格x1 == x2 == x3,和y1 == z1,但是x1 != y1

但是,下面的代码甚至会生成子单元格,以及不同大小的父单元格:

\begin{table}
\setlength\tabcolsep{0pt} 
\begin{tabu} to \textwidth { | *{5}{X[c]|} | *{3}{X[c]|} | *{3}{X[c]|} }
    \toprule
    \multicolumn{5}{c}{col1} & \multicolumn{3}{c}{col2} & \multicolumn{3}{c}{col3} \\
    %% \cmidrule{1-5} \cmidrule{6-8} \cmidrule{9-11} \\
    x1 & x2 & x3 & x4 & x5 & y1 & y2 & y3 & z1 & z2 & z3 \\
    \midrule
    1 & 1 & 1 & 1 & 1 & 2 & 2 & 2 & 3 & 3 & 3 \\
    \bottomrule
\end{tabu}
\caption{mess.}
\label{tab:mess}

\end{table}

如你看到的:

它看起来像这样

我应该做些什么不同的事情?

答案1

我认为你可以通过切换到一个tabularx环境并利用环境的指定能力来实现你的格式化目标相对的列宽。

如果第一个主列有 5 个等宽子列,而第二和第三主列(基本上)没有子列,则前 5 个子列的相对宽度由 给出7/15=0.4667。为什么715?因为 (a) 实际上有 7 列,并且 (b) 可以认为整个表格环境占用了 5*3=15 列。第二和第三主列的相对宽度由 给出5*0.46667=2.3333。请注意5*0.4667+2*2.3333=7, 类型的列数X。(旁白:由于您正在执行 ,数学运算大大简化了\setlength\tabcolsep{0pt}!)

我假设第 2 列和第 3 列实际上没有子列。我之所以做出这个假设,是因为您表示计划在这两列中显示方程式。我认为最好将整个方程式置于列的中心,而不是简单地将符号置于中心=

在此处输入图片描述

\documentclass{article}
\usepackage[margin=1in,letterpaper]{geometry} % set suitable page parameters
\usepackage{tabularx,booktabs}
% define a flexible-width centered version of "X" column type:
\newcolumntype{C}[1]{>{\centering\arraybackslash\hsize=#1\hsize}X}
\begin{document}
\begin{table}
\setlength\tabcolsep{0pt}
% Total number of C-type columns: 7
% Number of (implicit) columns of equal width = 3*5 = 15
% Relative width of 5 subcolumns of col. 1: 7/15=0.4667
% Note: 5*0.4667+2*2.3333 = 7 = # of C-type columns 
\begin{tabularx}{\textwidth}{ *{5}{C{0.4667}} *{2}{C{2.3333}} }
\toprule
\multicolumn{5}{@{}c}{header 1} & header 2 & header 3 \\
\cmidrule{1-5}
x1 & x2 & x3 & x4 & x5 \\
\midrule
1 & 2 & 3 & 4 & 5 & yyyyy & zzzzz \\
\addlinespace
a & b & c & d & e &
$\displaystyle v=\int_{-\infty}^{\infty} e^{-x^2}dx$ & 
$\displaystyle w=\sum_{i=1}^{\infty}\frac{1}{i^2}$ \\
\bottomrule
\end{tabularx}
\end{table}

\end{document} 

相关内容