1

I tried using the following code, with an unexpected outcome shown below (I've also attached the outcome I am expecting):

\documentclass{article}

\usepackage{siunitx} \usepackage{array,booktabs}

\begin{document} \begin{table}

\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lSS@{}} \\\toprule
&{\textit{Score}}\\\midrule
    \textbf{class 1}  & 0 \\
    \midrule
    &\multicolumn{2}{cc}{\textbf{Model A}\textbf{Model B}}\\
    \midrule
    \textbf{class A}   0  &  0\\ 
    \textbf{class B}  0  &  0\\
    \textbf{class C}  0  &  0\\
    \textbf{class D}  0  &  0\\
    \midrule
    \textbf{class X}  0  &  0\\
    \textbf{class Y}  0  &  0\\
    \textbf{class Z}  0  &  0\\
    \bottomrule
\end{tabular*}


\caption{caption here}
\label{table:results}

\end{table}

\end{document}

This has three major issues shown below: (1) the alignment of "Score" should be centered, (2) Model A Model B are repeating, and (3) the 0 values are shifted towards the left

enter image description here

I am trying to get an output which looks like:

enter image description here

Simon Dispa
  • 39,141
  • Did you notice the error message "Package array Error: Only one column-spec. allowed.." that you get upon compiling the code? Please never ever ignore error messages! Even if you get something that on first glance resembles a pdf file, there can still be issues with it. After an error, TeX only tries to recover enough to syntax check more of the file, it does not try to make sensible output after an error. To summarize: fix all error messages before even looking at the output. – leandriis Feb 21 '21 at 20:12
  • Is there a reason for stretching the table to textwidth? Since the contents of all columns are quite narrow, stretching the table just makes it harder to read. Wouldn't a regular \begin{tabular}{@{}lSS@{}} be sufficient? – leandriis Feb 21 '21 at 20:14

3 Answers3

1

The nicematrix package is the only one needed to reproduce the drawn table.

The package offer the \Block command that:

(1) replaces both multirow and multicolumn;

(2) allow the use of \\ (new lines) inside the cell;

(3) the content is centered horizontally and vertically;

\Block{1-2}{..} will generate a cell 1 rows high x 2 columns wide.

Vertical lines are generally discouraged in technical publications, but they are of course useful in many situations. Some packages have difficulty joining vertical and horizontal lines. This is not the case with nicematrix.

As you can appreciate the final code is much simpler, so it is easy to understand and change.

Two compilations are required the first time or if the table layout is changed.

See also https://tex.stackexchange.com/a/584492/161015

a

\documentclass{article}

\usepackage{nicematrix}

\begin{document}

\NiceMatrixOptions{cell-space-top-limit = 5pt,cell-space-bottom-limit = 5pt} %expand the cells vertically and horizontally

\begin{table} \centering \begin{NiceTabular}{|>\bfseries{c}|c|c|} % first column in bf \hline & \Block{1-2}{\textit{Score}} \ \hline class 1 & \Block{1-2}{0} \ \hline &\textbf{Model A} & \textbf{Model B}\ \hline class A & 0 & 0 \ class B & 0 & 0 \ class C & 0 & 0 \ \hline class X & 0 & 0 \ class Y & 0 & 0 \ class Z & 0 & 0 \ \hline \end{NiceTabular} \caption{Score of classes} \label{tbl:scoreii} \end{table}

\end{document}

Simon Dispa
  • 39,141
0

The following should serve as a place to start from:

enter image description here

\documentclass{article}

\usepackage{siunitx} \usepackage{array,booktabs}

\begin{document} \begin{table} \begin{tabular}{\textwidth}{@{\extracolsep{\fill}}lSS@{}} \toprule &\multicolumn{2}{c}{\textit{Score}}\ \midrule \textbf{class 1} & \multicolumn{2}{c}{0} \ \midrule &\textbf{Model A} & \textbf{Model B}\ \midrule \textbf{class A} & 0 & 0\ \textbf{class B} & 0 & 0\ \textbf{class C} & 0 & 0\ \textbf{class D} & 0 & 0\ \midrule \textbf{class X} & 0 & 0\ \textbf{class Y} & 0 & 0\ \textbf{class Z} & 0 & 0\ \bottomrule \end{tabular} \caption{caption here} \label{table:results} \end{table}

\end{document}

leandriis
  • 62,593
0

Using array and booktabs packages is sufficient for write your table ... (it is not clear, why you use ``S` columns, especially since you not specify any its formatting possibilities):

\documentclass{article}
\usepackage{array, makecell}

\begin{document} \begin{table} \setcellgapes{4pt} \makegapedcells \centering \begin{tabular}{|>{\bfseries}l | c | c | } \Xhline{1.2pt} & \multicolumn{2}{c|}{\textit{Score}} \ class 1 & \multicolumn{2}{c|}{0} \ \Xhline{0.8pt} & \textbf{Model A} & \textbf{Model B} \ \hline class A & 0 & 0 \ class B & 0 & 0 \ class C & 0 & 0 \ class D & 0 & 0 \ \hline class X & 0 & 0 \ class Y & 0 & 0 \ class Z & 0 & 0 \ \Xhline{1.2pt} \end{tabular} \caption{caption here} \label{table:results} \end{table} \end{document}

After one compilation the rezult is:

enter image description here

Zarko
  • 296,517