创建一个每行具有不同列数的表格,但多列似乎不起作用

创建一个每行具有不同列数的表格,但多列似乎不起作用

我在 Word 中创建了这个表格来说明一些数据。首先我想说这不是一个示例,这是我使用的确切表格,但我清除了数据,因为我不允许共享这些数据。

在此处输入图片描述

我读过这个网站上关于表格中每行不同列大小的问题,但我找不到可以实现这一点的方法。我认为唯一可行的选择是创建一个包含许多列的表格(我仍在考虑我需要多少列),然后为每一行创建复杂的多列。

现在我截取了表格的屏幕截图并将其作为图像放在我的文件中,但这是我想避免的事情。除此之外,我最后一个选择是从表格中删除一些数据点以使列数合适,但我不敢相信这是我必须采取的解决方案。

我被要求制作一个最小的工作示例,因此我选择了表格的最后三行,因为它们很好地说明了我的问题。我创建了一个有 13 列的表格(左侧标题为三列,后面的值为 10 列)。由于 4、9 和 10 没有共同的乘数,我不知道如何才能获得与上面发布的屏幕截图类似的结果。

\documentclass{article}
\usepackage[utf8]{inputenc}

\begin{document}

\begin{table}[h]
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|c|c|}
\hline
\multicolumn{3}{|c|}{} & \multicolumn{10}{|c|}{Section} \\
\hline
\multicolumn{3}{|c|}{\textbf{Some name}} & \multicolumn{2}{|c|}{2} & \multicolumn{2}{|c|}{3} & \multicolumn{2}{|c|}{4} & 
\multicolumn{2}{|c|}{5} & & \\
\hline
\multicolumn{3}{|c|}{Some metric} & \multicolumn{2}{|c|}{--} & \multicolumn{2}{|c|}{--} & \multicolumn{2}{|c|}{--} & 
\multicolumn{2}{|c|}{--} & & \\
\hline

\multicolumn{3}{|c|}{\textbf{Some name}} & 1 & 2 & 3 & 4 & 5 & 8 & 12 & 16 & 20 & \\
\hline
\multicolumn{3}{|c|}{Some metric} & -- & -- & -- & -- & -- & -- & -- & -- & -- & \\
\hline

\multicolumn{3}{|c|}{\textbf{Some name}} & 1 & 2 & 3 & 4 & 5 & 8 & 12 & 16 & 20 & 24\\
\hline
\multicolumn{3}{|c|}{Some metric} & -- & -- & -- & -- & -- & -- & -- & -- & -- & -- \\
\hline
\end{tabular}
\end{table}

\end{document}

结果如下:在此处输入图片描述

我想知道,有没有办法可以简单地按“每行”或类似的方式指定我想要的列数?

答案1

要获得这样的布局,\multicolumn您需要制作一个有 90 (!) 列的表格(该表格每行列数之间的最小公倍数),然后\multicolumn适当使用来实现该布局。

一个更简单的解决方案是将表格堆叠在一起,每个表格具有不同数量的列,但所有表格的宽度相同。下面的文档就是这么做的;代码可以变得更加自动化,以减少使用时的痛苦,但这留作练习吧 :)

在此处输入图片描述

\documentclass{article}
\usepackage{tabularx}
\usepackage[table]{xcolor}
\definecolor{tbg}{HTML}{E6E6E6}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\begin{document}
\begin{table}
  \begin{tabularx}{\textwidth}{|p{3cm}|*6{C|}} \hline
    \cellcolor{tbg}    & \multicolumn{6}{c|}{\cellcolor{tbg} \textbf{Section 1}} \\ \hline
    \textbf{Some Name} & 1 & 2 & 3 & 4 & 5 & 6 \\ \hline
    Some metric        &   &   &   &   &   &   \\ \hline
  \end{tabularx}\par\nointerlineskip
  \begin{tabularx}{\textwidth}{|p{3cm}|*2{C|}}
    \textbf{Some Name} & 2 & 3 \\ \hline
    Some metric        &   &   \\ \hline
  \end{tabularx}\par\nointerlineskip
  \begin{tabularx}{\textwidth}{|p{3cm}|*3{C|}}
    \textbf{Some Name} & 3 & 6 & 8 \\ \hline
    Some metric        &   &   &   \\ \hline
  \end{tabularx}\par\nointerlineskip
  \begin{tabularx}{\textwidth}{|p{3cm}|*6{C|}}
    \textbf{Some Name} & 1 & 2 & 5 & 10 & 15 & 19 \\ \hline
    Some metric        &   &   &   &    &    &    \\ \hline
  \end{tabularx}\par\nointerlineskip
  \begin{tabularx}{\textwidth}{|p{3cm}|*2{C|}}
    \textbf{Some Name} & 1 & 125 \\ \hline
    Some metric        &   &     \\ \hline
  \end{tabularx}\par\nointerlineskip
  \begin{tabularx}{\textwidth}{|p{3cm}|*5{C|}}
    \cellcolor{tbg}    & \multicolumn{5}{c|}{\cellcolor{tbg} \textbf{Section 2}} \\ \hline
    \textbf{Some Name} & 1 & 2 & 3 & 4 & 5 \\ \hline
    Some metric        &   &   &   &   &   \\ \hline
  \end{tabularx}\par\nointerlineskip
  \begin{tabularx}{\textwidth}{|p{3cm}|*4{C|}}
    \textbf{Some Name} & 1 & 2 & 3 & 4 \\ \hline
    Some metric        &   &   &   &   \\ \hline
  \end{tabularx}\par\nointerlineskip
  \begin{tabularx}{\textwidth}{|p{3cm}|*4{C|}}
    \textbf{Some Name} & 2 & 3 & 4 & 5 \\ \hline
    Some metric        &   &   &   &   \\ \hline
  \end{tabularx}\par\nointerlineskip
  \begin{tabularx}{\textwidth}{|p{3cm}|*9{C|}}
    \textbf{Some Name} & 1 & 2 & 3 & 4 & 5 & 8 & 12 & 16 & 20 \\ \hline
    Some metric        &   &   &   &   &   &   &    &    &    \\ \hline
  \end{tabularx}\par\nointerlineskip
  \begin{tabularx}{\textwidth}{|p{3cm}|*{10}{C|}}
    \textbf{Some Name} & 1 & 2 & 3 & 4 & 5 & 8 & 12 & 16 & 20 & 24 \\ \hline
    Some metric        &   &   &   &   &   &   &    &    &    &    \\ \hline
  \end{tabularx}\par\nointerlineskip
\end{table}
\end{document}

答案2

这是一种实现此目的的肮脏方法。

在此处输入图片描述

\documentclass{article}

\usepackage{ninecolors}
\usepackage{tabularray}

\newcommand{\ds}{\hfill\rule[-2mm]{0.5pt}{6mm}\hfill} %dummy column separator 

\begin{document}
\begin{table}[htbp]
    \begin{tblr}{
    hlines, vlines,
    width=\linewidth,
    colspec = {Q[l,m] X[c,m]},
    rows = {rowsep=0pt},
    columns={preto=\hfill, appto=\hfill},
    cell{even}{1}={font=\bfseries},
    hspan = even
    }
    \SetRow{gray9}& \rule[-2mm]{0pt}{6mm}Section 1\\
    Some name   & 1 \ds 2 \ds 3 \ds 4 \ds 5 \ds 6 \\
    Some metric & 1 \ds 2 \ds 3 \ds 4 \ds 5 \ds 6 \\
    Some name   &  2 \ds 3 \\
    Some matric & 2 \ds 3\\
    Some name   & 3 \ds 6 \ds 8\\
    Some matric & 3 \ds 6 \ds 8\\
    Some name   & 1 \ds 2 \ds 5 \ds 10 \ds 15 \ds 19\\
    Some matric & 1 \ds 2 \ds 5 \ds 10 \ds 15 \ds 19\\
    Some name   & 1 \ds 125\\
    Some metric & 1 \ds \hphantom{12}5\\
    \SetRow{gray9} & \rule[-2mm]{0pt}{6mm}Section 2\\
    Some name   & 1 \ds 2 \ds 3 \ds 4 \ds 5 \\
    Some metric & 1 \ds 2 \ds 3 \ds 4 \ds 5 \\
    Some name   & 1 \ds 2 \ds 3 \ds 4 \\
    Some metric & 1 \ds 2 \ds 3 \ds 4\\
    Some name   & 1 \ds 2 \ds 3 \ds 4 \\
    Some metric & 1 \ds 2 \ds 3 \ds 4\\
    Some name   & 1 \ds 2 \ds 3 \ds 4 \ds 5 \ds 8 \ds 12 \ds 16 \ds 20\\
    Some metric & 1 \ds 2 \ds 3 \ds 4 \ds 5 \ds 8 \ds 12 \ds 16 \ds 20\\
    Some name   & 1 \ds 2 \ds 3 \ds 4 \ds 5 \ds 8 \ds 12 \ds 16 \ds 20 \ds 24\\
    Some metric & 1 \ds 2 \ds 3 \ds 4 \ds 5 \ds 8 \ds 12 \ds 16 \ds 20 \ds \hphantom{2}4\\
    \end{tblr}
\end{table}
\end{document}

免责声明:它有几个问题,例如,如果连续的“某些名称”和“某些指标”行在相似的列中没有等宽的内容,那么这些列将不会对齐。纠正此问题的一种方法是使用命令,\hphantom但很快就会变得乏味。此外,如果水平线的高度不是绝对的,那就更好了。

相关内容