# How can we create a multi-column multi-row table at the same time?

I'm trying to plot this table using LaTeX, but it looks like I'm not doing a good job. Any guidance, please?

I tried this: for the first two lines

\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}

• Since I assume, this question is about "Test Percentage" spanning both two columns and two rows: \multicolumn{2}{c}{\multirow{2}{*}{Test Percentage}} should help. – leandriis Jan 18 at 21:54
• can you please guide me how can we draw the first two lines (test percentage + 20 ) I tried so many times im a beginner in latex thank you – wassimdiai Jan 18 at 21:59
• Please add a minimal working example (MWE) to your question that show what you tried so far. – leandriis Jan 18 at 22:01
• of course , I tried this for the first 2 lines :\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 \\ \hline \end{tabular} – wassimdiai Jan 18 at 22:03

Something like this?

\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 \\
\multirow{2.2}{*}{30} & $S_e$ & 91.18 & \\
& $S_p$ & 98.87 & \\
\multirow{2.2}{*}{50} & $S_e$ & 91.40 & \\
& $S_p$ & 98.81 & \\

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

• thank you for your answer but when im trying it im getting the table bellow – wassimdiai Jan 18 at 23:23
• @wassimdiai - Did you load the booktabs and multirow packages properly? – Mico Jan 19 at 9:33

Here is a corrected version of your code. I added some annotations hoping to explain the issues with your original code and the changes I made. I also added a second version using less lines (from the booktabs package) and siunitx to help with the alignment of the numbers:

\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}

• Thank you so much – wassimdiai Jan 18 at 22:29

Here is what you can do with nicematrix and 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}


You need several compilations (because nicematrix` uses PGF/Tikz nodes).

• I took liberty to insert correct image of table produced by your MWE. +1 – Zarko Jan 19 at 22:29