如何才能同时创建多列多行的表格?

如何才能同时创建多列多行的表格?

我正在尝试使用 LaTeX 绘制此表,但看起来效果不太好。有什么指导吗?

在此处输入图片描述


我试过:前两行

\begin{tabular}{|c|c|c|c|c|c|c|c|c|}
\centering
\hline
\multicolumn{2}{c}{\multirow{2}{*}{Test Percentage}}&\multicolumn{5}{c|}{sleep stage} & %
    \multicolumn{2}{c}{\multirow{2}{*}{Accuracy}}\\
\cline{2-7}
& N1 & N2 & N3 & Wake & REM  \\
&training & testing\\
\hline

\end{tabular}

答案1

像这样吗?

在此处输入图片描述

\documentclass{article}
\usepackage{booktabs,multirow}

\begin{document}
\begingroup % localize scope of next instruction
\setlength\tabcolsep{0pt}
\noindent
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} *{8}{c} }
\toprule 
\multicolumn{2}{l}{\multirow{2.2}{*}{Test Percentage}} &
\multicolumn{5}{c}{Sleep stage} & 
\multirow{2.2}{*}{$A_{cc}$} \\
\cmidrule{3-7}
& & S1 & S2 & S3+S4 & Wake & REM \\ 
\midrule
\multirow{2.2}{*}{20} & $S_e$ & 92.53 & 96.34 & 97.82 & 97.88 & 67.23 & \multirow{2.2}{*}{95.46} \\
                      & $S_p$ & 98.71 & 98.33 \\
\addlinespace
\multirow{2.2}{*}{30} & $S_e$ & 91.18 & \\
                      & $S_p$ & 98.87 & \\
\addlinespace
\multirow{2.2}{*}{50} & $S_e$ & 91.40 & \\
                      & $S_p$ & 98.81 & \\

\bottomrule
\end{tabular*}
\endgroup
\end{document}

答案2

这是您代码的更正版本。我添加了一些注释,希望能够解释您原始代码中的问题以及我所做的更改。我还添加了第二个版本,使用更少的行(来自包booktabs)和 siunitx` 来帮助对齐数字:

在此处输入图片描述

\documentclass{article}
\usepackage{multirow}

%%% Only used in the second example:
\usepackage{siunitx}
\usepackage{booktabs}
\begin{document}


\begin{center} % Instead of the wrong centering command, assuming you want to horizontally center the whole tabular with respect ot the textwidth.
\begin{tabular}{|c|c|c|c|c|c|c|c|} % removed one column specifier.
%\centering  % Do not use `\centering` inside of a tabluar.
\hline 
\multicolumn{2}{|c|}{\multirow{2}{*}{Test Percentage}} % second argument of \multirow can't be empty. I suggest using * here. Added | to get a continuous vertical line.
&\multicolumn{5}{c|}{sleep stage} & % 
\multirow{2}{*}{Accuracy}% second argument of \multirow can't be empty. I suggest using * here. Removed \multicolumn as this seems superfluous  here.
\\ \cline{3-7} % canged 2-7 to 3-7 to prevent overlap with "percentage"
\multicolumn{2}{|c|}{} & N1 & N2 & N3 & Wake & REM & \\ % added in some missing &s and an empty \multicolumn.
\hline 
\end{tabular}
\end{center}

\begin{tabular}{cc*{5}{S[table-format=2.2]}c}
\toprule
\multicolumn{2}{c}{\multirow{2.4}{*}{Test Percentage}} 
  & \multicolumn{5}{c}{Sleep EEG Classes} 
    & \multirow{2.4}{*}{Accuracy} \\
\cmidrule{3-7}
                    &   & {N1}  & {N2}  & {N3} & {Wake} & {REM} & \\ 
\midrule
\multirow{2}{*}{20} & S & 92.53 & 92.53 & 92.53   & 92.53  & 92.53 & \multirow{2}{*}{92.53} \\
                    & S & 92.53 & 92.53 & 92.53   & 92.53  & 92.53 &                        \\
\midrule
\end{tabular}


\end{document}

答案3

以下是您可以使用nicematrix和执行的操作booktabs

\documentclass{article}
\usepackage{nicematrix}
\usepackage{booktabs}

\begin{document}

\begin{center} 
\begin{NiceTabular}{wc{1cm}wc{1cm}*{6}{c}}
\toprule
\Block{2-2}{Test Percentage} & & \Block{1-5}{sleep stage} &&&&& \Block{2-1}{Accuracy} \\
\cmidrule{3-7}  
 & & N1 & N2 & N3 & Wake & REM & \\ 
\midrule
\Block{2-1}{20} & S & 92.53 & 92.53 & 92.53   & 92.53  & 92.53 & \Block{2-1}{92.53} \\
                    & S & 92.53 & 92.53 & 92.53   & 92.53  & 92.53 &                        \\
\bottomrule
\end{NiceTabular}
\end{center}

\end{document}

您需要多次编译(因为nicematrix使用 PGF/Tikz 节点)。

在此处输入图片描述

相关内容