1

I would like to create a table like this:

         +------------+----------+
         |      M1    |     M2   |
+--------+------+-----+------+---+
|  Prog  |   E  |  S  |  E   | S | 
+--------+------+-----+------+---+
| P1     |  1   |  5  | 0.6  | 10|
| P2     |  2   |  7  | 2    | 6 |
+--------+------+-----+------+---+

Based on the examples, I have written this

\begin{document}
    \begin{table}
        \centering
        \caption{Multiprogram sets}
        \label{multiprogram}
        \begin{tabular}{c|c|c|c|c|}
            \cline{2-5}
             & \multicolumn{2}{|c|c|}{M1}  & \multicolumn{2}{|c|c|}{M2} \\
            \cline{2-5}
             & E & S & E & S\\
            \hline
            \multicolumn{1}{|c|}{P1} & 1 & 5 & 0.6 & 10\\
            \hline
            \multicolumn{1}{|c|}{P2} & 2 & 7 & 2 & 6 \\
        \end{tabular}
    \end{table}
\end{document}

But it is not working and has many errors. How can I fix that?

2
  • 2
    You just need to change \multicolumn{2}{|c|c|}{M1} to \multicolumn{2}{|c|}{M1} (and the same for the other cell with M2). At least, this should solve the errors. Commented Jul 12, 2022 at 13:38
  • Should the four data columns have equal widths? Please advise.
    – Mico
    Commented Jul 12, 2022 at 13:38

2 Answers 2

1

Like this:

enter image description here

\documentclass[twoside, a4paper]{article}
\usepackage{array}

\begin{document}
    \begin{table}
        \centering
        \caption{Multiprogram sets}
        \label{multiprogram}
        \begin{tabular}{c|c|c|c|c|}
            \cline{2-5}
    & \multicolumn{2}{c|}{M1}  & \multicolumn{2}{c|}{M2} \\
    \cline{2-5}
    & E & S & E & S\\
    \hline
\multicolumn{1}{|c|}{P1} & 1 & 5 & 0.6 & 10\\
    \hline
\multicolumn{1}{|c|}{P2} & 2 & 7 & 2 & 6 \\
    \hline
        \end{tabular}
    \end{table}
\end{document}

or with equal width of columns, using tabularray packageČ

\documentclass[twoside, a4paper]{article}
\usepackage[skip=1ex]{caption}
\usepackage{tabularray}

\begin{document}
    \begin{table}
    \centering
\caption{Multiprogram sets}
\label{multiprogram}
    \begin{tblr}{hline{1}={2-Z}{solid}, hline{2-Z}=solid, 
                 vline{1}={3-Z}{solid}, vline{2-Z}=solid,
                 colspec= {*{5}{Q[c, wd=3em]}},
                 }
\SetCell[r=2]{c}
    &   \SetCell[c=2]{c}  M1
        &       &   \SetCell[c=2]{c}  M2    
                    &       \\
    & E & S & E     & S     \\
P1  & 1 & 5 & 0.6   & 10    \\
P2  & 2 & 7 & 2     & 6     \\
    \end{tblr}
    \end{table}
\end{document}

enter image description here

0

If all you wish to achieve is to get the code to compile, simply replace both instances of \multicolumn{2}{|c|c|} with \multicolumn{2}{c|}. This measure has already been suggested by @JensHabricht in a comment and in Zarko's answer.

If, in addition, you would like your table to have better visual appeal, do also consider (a) giving equal widths to the four data columns and (b) aligning the numbers in the final two data columns on their explicit or implict decimal markers. The result of the making these adjustments is shown in the second table, labelled "improved version", in the following screenshot.

enter image description here

\documentclass{article}

%% setup code for second version of table:
\usepackage{siunitx,array,calc}
\newcolumntype{T}[1]{S[table-format=#1]}
\newcommand\mc[1]{\multicolumn{1}{c|}{#1}}
\newcommand\mw[1]{\multicolumn{1}{wc{\widthof{0.6}}|}{#1}}

\begin{document}
    \begin{table}
        \centering
        \caption{Initial version}
        \label{multiprogram}
        \begin{tabular}{c|c|c|c|c|}
            \cline{2-5}
             & \multicolumn{2}{c|}{M1}  & \multicolumn{2}{c|}{M2} \\
            \cline{2-5}
             & E & S & E & S\\
            \hline
            \multicolumn{1}{|c|}{P1} & 1 & 5 & 0.6 & 10\\
            \hline
            \multicolumn{1}{|c|}{P2} & 2 & 7 & 2 & 6 \\
        \end{tabular}        
        
        \bigskip\bigskip
        \setlength\extrarowheight{2pt}
        \caption{Improved version\strut}
        \label{tab:multi}
        \begin{tabular}{| c | c | c | T{1.1}| T{2.0} |}
            \cline{2-5}
            \mc{} & \multicolumn{2}{c|}{M1}  & \multicolumn{2}{c|}{M2} \\
            \cline{2-5}
            \mc{} & \mw{E} & \mw{S} & \mw{E} & \mw{S} \\
            \hline
            P1 & 1 & 5 & 0.6 & 10 \\
            \hline
            P2 & 2 & 7 & 2   & 6  \\
            \hline
        \end{tabular}
    \end{table}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .