![如何制作一个具有多个子标题列且跨越整个表格宽度的表格?](https://linux22.com/image/429014/%E5%A6%82%E4%BD%95%E5%88%B6%E4%BD%9C%E4%B8%80%E4%B8%AA%E5%85%B7%E6%9C%89%E5%A4%9A%E4%B8%AA%E5%AD%90%E6%A0%87%E9%A2%98%E5%88%97%E4%B8%94%E8%B7%A8%E8%B6%8A%E6%95%B4%E4%B8%AA%E8%A1%A8%E6%A0%BC%E5%AE%BD%E5%BA%A6%E7%9A%84%E8%A1%A8%E6%A0%BC%EF%BC%9F.png)
我尝试在表格环境中使用多个 tabularx 环境,但这会导致整个文本向左对齐且与列标题不匹配的问题,在下面的例子中是V1, V2, V3
:
我收到以下代码的错误(我尝试删除 \hline,但没有帮助):
Misplaced \noalign.: You have used a \hline command in the wrong place, probably outside a table. If the \hline command is written inside a table, try including \\ before it.
\begin{table}[]
\begin{tabular*}{\textwidth}{SSSS} \hline
{$ $} & {$metric$} & {$metric$} & {$metric$} \\ \hline
\textbf{class 1} & 0 & 0 & 0 \\
\midrule
\textbf{HEADING}
\midrule
\textbf{model (1)} & 0 & 0 & 0\\
\textbf{model (1)} & 0 & 0 & 0 \\
\textbf{model (1)} & 0 & 0 & 0 \\
\textbf{model (1)} & 0 & 0 & 0 \\
\midrule
\textbf{model (1)} & 0 & 0 & 0\\
\textbf{model (1)} & 0 & 0 & 0 \\
\textbf{model (1)} & 0 & 0 & 0 \\
\textbf{model (1)} & 0 & 0 & 0 \\
\midrule
\textbf{HEADING}
\midrule
\textbf{model (1)} & 0 & 0 & 0 \\
\textbf{model (1)} & 0 & 0 & 0 \\
\textbf{model (1)} & 0 & 0 & 0 \\
\textbf{model (1)} & 0 & 0 & 0 \\
\midrule
\textbf{model (1)} & 0 & 0 & 0\\
\textbf{model (1)} & 0 & 0 & 0 \\
\textbf{model (1)} & 0 & 0 & 0 \\
\textbf{model (1)} & 0 & 0 & 0 \\
\bottomrule
\end{tabular*}
\caption{caption here}
\label{table:results}
\end{table}
答案1
我相信您有意这样做(请注意,您tabularx
根本没有使用。就我个人而言,我会tabular
在这里使用正常方式:将列拉伸到满,只会使表格更难阅读,但这会显示它的全宽。
\documentclass{article}
\usepackage{siunitx}
\usepackage{array,booktabs}
\begin{document}
\begin{table}% don't use an empty argument []
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lSSS@{}} \\\toprule
% don't use math mode to get italics!!!!
% {$ $} & {$metric$} & {$metric$} & {$metric$} \\ \hline
& {\textit{metric}}&{\textit{metric}}&{\textit{metric}}\\\midrule
\textbf{class 1} & 0 & 0 & 0 \\
\midrule
&\multicolumn{3}{c}{\textbf{HEADING}}\\
\midrule
\textbf{model (1)} & 0 & 0 & 0\\
\textbf{model (1)} & 0 & 0 & 0 \\
\textbf{model (1)} & 0 & 0 & 0 \\
\textbf{model (1)} & 0 & 0 & 0 \\
\midrule
\textbf{model (1)} & 0 & 0 & 0\\
\textbf{model (1)} & 0 & 0 & 0 \\
\textbf{model (1)} & 0 & 0 & 0 \\
\textbf{model (1)} & 0 & 0 & 0 \\
\midrule
&\multicolumn{3}{c}{\textbf{HEADING}}\\
\midrule
\textbf{model (1)} & 0 & 0 & 0 \\
\textbf{model (1)} & 0 & 0 & 0 \\
\textbf{model (1)} & 0 & 0 & 0 \\
\textbf{model (1)} & 0 & 0 & 0 \\
\midrule
\textbf{model (1)} & 0 & 0 & 0\\
\textbf{model (1)} & 0 & 0 & 0 \\
\textbf{model (1)} & 0 & 0 & 0 \\
\textbf{model (1)} & 0 & 0 & 0 \\
\bottomrule
\end{tabular*}
\caption{caption here}
\label{table:results}
\end{table}
\end{document}
如果你使用
\centering
\begin{tabular}{lSSS} \\\toprule
然后它看起来像
答案2
此替代方案使用包nicematrix
。我遵循了绘图的总体设计,并绘制了一条垂直线。
技术出版物中通常不鼓励使用垂直线,但在许多情况下它们当然很有用。有些软件包很难连接垂直线和水平线(见第二张图)。但 并非如此nicematrix
。
我添加了一个新的列类型,以允许将最后 3 列扩展为文本宽度的百分比。
最后,几个元素被强调,但没有明显的后果。还请注意“度量“。
笔记 要在 S 列下划线,请使用 包\uline
中的下划线ulem
。(加载为\usepackage[normalem]{ulem}
)。请参阅https://tex.stackexchange.com/a/159634/161015
连接线路时出现问题
\documentclass{article}
\usepackage{siunitx}
\usepackage{array,booktabs}
\usepackage{nicematrix}
\begin{document}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1\textwidth}} % size of the column as a percent of textwidth
\NiceMatrixOptions{cell-space-top-limit = 5pt,cell-space-bottom-limit = 5pt} %expand the cells vertically and horizontally
\begin{table}
\centering
\begin{NiceTabular}{>\bfseries{c} | P{0.2} P{0.2} P{0.2}} % first column in bf
& \textit{metric}&\textit{metric}&\textit{\underline{metric}}\\
\midrule
\textbf{class 1} & 0 & 0 & 0 \\
\midrule
&\Block{1-3}{\textbf{\underline{HEADING}}}\\
\midrule
model (1) & 0 & 0 & 0 \\
\underline{model (1)} & 0 & 0 & 0 \\
model (1) & 0 & 0 & 0 \\
model (1) & 0 & 0 & 0 \\
\midrule
model (1) & 0 & 0 & 0 \\
model (1) & 0 & 0 & 0 \\
model (1) & 0 & 0 & 0 \\
model (1) & \underline{0} & 0 & 0 \\
\midrule
&\Block{1-3}{\textbf{HEADING}}\\
\midrule
model (1) & 0 & 0 & 0 \\
model (1) & 0 & 0 & 0 \\
model (1) & 0 & 0 & 0 \\
model (1) & 0 & 0 & 0 \\
\midrule
model (1) & 0 & 0 & 0 \\
model (1) & 0 & 0 & 0 \\
model (1) & 0 & \underline{0} & 0 \\
model (1) & 0 & 0 & 0 \\
\bottomrule
\end{NiceTabular}
\caption{caption here}
\label{table:results}
\end{table}
\end{document}