6

I am having problems in centering the text in a table using multicolumn. This is the table I have created:

\begin{table}[H]\centering
\setlength\extrarowheight{3pt}
\begin{tabular}{|c|c|c|c|c|}
\hline
\multirow{2}{*}{Metric}&
\multicolumn{2}{c}{FFNN}\vline&
\multicolumn{2}{c}{CNN}\vline\\
& In-Sample & Out-of-Sample & In-Sample & Out-of-Sample\\\hline
RMSE & 1 & 2 & 3 & 4\\\hline MAE & 1 & 2 & 3 & 4\\\hline
MDA & 1 & 2 & 3 & 4\\\hline $\text{R}^2$ & 1 & 2 & 3 & 4\\\hline
\end{tabular}
\end{table}

and this is what I get after compiling:

enter image description here

The problem is that the two column titles, "FFNN" and "CNN" are not centered with respect to the 'In-Sample' and 'Out-of-Sample' columns below. Is there a way to center them without changing the layout of the table?

2
  • Welcome to TeX.SE!
    – Mensch
    Nov 14, 2021 at 23:13
  • @Mensch Thank you!
    – T. Willis
    Nov 15, 2021 at 14:26

4 Answers 4

4

If you want the FFNN and CNN header strings to be centered between columns 2/3 and 4/5, respectively, the four data columns need to have equal widths. One way to achieve this is with the help of the w column type, which is provided by the array package.

You may also want to rethink why it's even necessary to center the FFNN and CNN strings on the vertical lines that separate columns 2/3 and 4/5. If you get rid of all vertical lines and use fewer but well-spaced horizontal lines, it's not really necessary to pay attention to the unequal column widths.

The first table in the following screenshot reproduces the OP's screenshot. The second table employs the w column type to center the FFNN and CNN headers; observe that despite the fact that these headers are now centered over the pairs of columns, the "look" still isn't balanced since the strings in the sub-headers -- "In-Sample" and "Out-of-Sample" -- have rather different widths. The third table does away with the vertical lines, getting rid of the need for equal-width columns in the process.

enter image description here

\documentclass{article}
\usepackage{amsmath,array,multirow,booktabs}
\newlength\mylen
\settowidth\mylen{Out-of-Sample}
\begin{document}
\begin{table}[ht]
\centering
\setlength\extrarowheight{3pt}
\begin{tabular}{|c|*{4}{c|}}
\hline
\multirow{2}{*}{Metric}&
\multicolumn{2}{c}{FFNN}\vline&
\multicolumn{2}{c}{CNN}\vline\\
& In-Sample & Out-of-Sample & In-Sample & Out-of-Sample\\\hline
RMSE & 1 & 2 & 3 & 4\\\hline MAE & 1 & 2 & 3 & 4\\\hline
MDA & 1 & 2 & 3 & 4\\\hline R\textsuperscript{2} & 1 & 2 & 3 & 4\\\hline
\end{tabular}
\end{table}

\begin{table}[h]
\centering
\setlength\extrarowheight{3pt}
\begin{tabular}{|c|*{4}{wc{\mylen}|}}
\hline
\multirow{2}{*}{Metric}&
\multicolumn{2}{c}{FFNN}\vline&
\multicolumn{2}{c}{CNN}\vline\\
& In-Sample & Out-of-Sample & In-Sample & Out-of-Sample\\\hline
RMSE & 1 & 2 & 3 & 4\\\hline MAE & 1 & 2 & 3 & 4\\\hline
MDA & 1 & 2 & 3 & 4\\\hline R\textsuperscript{2} & 1 & 2 & 3 & 4\\\hline
\end{tabular}
\end{table}

\begin{table}[h]
\centering
\begin{tabular}{@{} l cccc @{}}
\toprule
Metric & \multicolumn{2}{c}{FFNN} & \multicolumn{2}{c@{}}{CNN} \\
\cmidrule(lr){2-3} \cmidrule(l){4-5}
& In-Sample & Out-of-Sample & In-Sample & Out-of-Sample\\
\midrule
RMSE & 1 & 2 & 3 & 4\\
MAE & 1 & 2 & 3 & 4\\
MDA & 1 & 2 & 3 & 4\\
R\textsuperscript{2} & 1 & 2 & 3 & 4\\
\bottomrule
\end{tabular}
\end{table}
\end{document}
1
  • 1
    That is a very clean and elegant solution! Many thanks!
    – T. Willis
    Nov 15, 2021 at 14:25
3

The "FFNN" and "CNN" are indeed centered, the problem lies in the widths of the "In-Sample" and "Out-of-Sample" columns. Using \widthof from the calc package we can make "In-Sample" be in a box of the same width as "Out-of-Sample" which will result in proper centering.

\documentclass{article}
\usepackage{float}
\usepackage{array}
\usepackage{multirow}
\usepackage{amsmath}
\usepackage{calc} %<--
\begin{document}
\begin{table}[H]\centering
\setlength\extrarowheight{3pt}
\begin{tabular}{|c|c|c|c|c|}
\hline
\multirow{2}{*}{Metric}&
\multicolumn{2}{c}{FFNN}\vline&
\multicolumn{2}{c}{CNN}\vline\\
&\makebox[\widthof{Out-of-Sample}][c]{In-Sample} & Out-of-Sample &\makebox[\widthof{Out-of-Sample}][c]{In-Sample} & Out-of-Sample\\\hline%<--
RMSE & 1 & 2 & 3 & 4\\\hline MAE & 1 & 2 & 3 & 4\\\hline
MDA & 1 & 2 & 3 & 4\\\hline $\text{R}^2$ & 1 & 2 & 3 & 4\\\hline
\end{tabular}
\end{table}
\end{document}

enter image description here

2

Two version of your table written by use of the tabularray package:

enter image description here

\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\newlength\mycolwidth

\begin{document}
    \begin{table}[ht]
    \centering
    \settowidth\mycolwidth{Out-of-Sample}
\begin{tblr}{hline{1,3-Z}, vlines,
             colspec={c *{4}{Q[c, wd=\mycolwidth]} },
             column{2-Z} = {colsep=4pt}
             }
\SetCell[r=2]{c}    Metric
        &   \SetCell[c=2]{c} FFNN
            &   &   \SetCell[c=2]{c}    CNN
                    &                       \\
        & In-Sample
            & Out-of-Sample
                & In-Sample
                    & Out-of-Sample         \\
RMSE    & 1 & 2 & 3 & 4                     \\
MAE     & 1 & 2 & 3 & 4                     \\
MDA     & 1 & 2 & 3 & 4                     \\
R\textsuperscript{2} 
        & 1 & 2 & 3 & 4                     \\
\end{tblr}
    \end{table}

    \begin{table}[ht]
    \centering
    \settowidth\mycolwidth{Out-of-Sample}
\begin{tblr}{column{2-Z} = {colsep=3pt},
             colspec={@{} l *{4}{Q[c, wd=\mycolwidth]} @{}},
             }
    \toprule
\SetCell[r=2]{c}    Metric
        &   \SetCell[c=2]{c} FFNN
            &   &   \SetCell[c=2]{c}    CNN
                    &                       \\
    \cmidrule[r] {2-3}  
    \cmidrule[l] {4-5}
        & In-Sample
            & Out-of-Sample
                & In-Sample
                    & Out-of-Sample         \\
    \midrule
RMSE    & 1 & 2 & 3 & 4                     \\
MAE     & 1 & 2 & 3 & 4                     \\
MDA     & 1 & 2 & 3 & 4                     \\
R\textsuperscript{2}
        & 1 & 2 & 3 & 4                     \\
    \bottomrule
\end{tblr}
    \end{table}
\end{document}
2

If you actually want the columns 2 and 3 with different widths (idem for 4 and 5) and, despite that difference, FFNN centered according the rule between the columns 2 and 3 (idem for CNN above the rule between the columns 4 and 5), it's possible to program a command \BiBlock in {NiceTabular} of nicematrix which does the job automatically.

\documentclass{article}
\usepackage{float}
\usepackage{nicematrix,tikz}

\ExplSyntaxOn
\NewDocumentCommand \BiBlock { m }
  {
    \Block{1-2}{}
    \tl_gput_right:Nx \g_nicematrix_code_after_tl 
      { 
        \__willis_myblock:nnn 
          { \int_use:c { c@iRow } } 
          { \int_eval:n { \int_use:c { c@jCol } + 1 } } 
          { #1 } 
      }
  }
\cs_new_protected:Nn \__willis_myblock:nnn
  { \tikz \node [baseline=base] at ( #1.5 -| #2 ) { #3 } ; }
\ExplSyntaxOff

\begin{document}

\begin{table}[H]\centering
\setlength\extrarowheight{3pt}
\begin{NiceTabular}{ccccc}[vlines]
\hline
\Block{2-1}{Metric}&
\BiBlock{FFNN}& &
\BiBlock{CNN}\\
& In-Sample & Out-of-Sample & In-Sample & Out-of-Sample\\\hline
RMSE & 1 & 2 & 3 & 4\\\hline MAE & 1 & 2 & 3 & 4\\\hline
MDA & 1 & 2 & 3 & 4\\\hline $\text{R}^2$ & 1 & 2 & 3 & 4\\\hline
\end{NiceTabular}
\end{table}

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

The same with the horizontal rules of booktabs.

\documentclass{article}
\usepackage{float}
\usepackage{nicematrix,tikz,booktabs}

\ExplSyntaxOn
\NewDocumentCommand \BiBlock { m }
  {
    \Block{1-2}{}
    \tl_gput_right:Nx \g_nicematrix_code_after_tl 
      { 
        \__willis_myblock:nnn 
          { \int_use:c { c@iRow } } 
          { \int_eval:n { \int_use:c { c@jCol } + 1 } } 
          { #1 } 
      }
  }
\cs_new_protected:Nn \__willis_myblock:nnn
  { \tikz \node [baseline=base] at ( #1.5 -| #2 ) { #3 } ; }
\ExplSyntaxOff

\begin{document}

\begin{table}[H]\centering
\setlength\extrarowheight{3pt}
\begin{NiceTabular}{ccccc}
\toprule
\Block{2-1}{Metric}&
\BiBlock{FFNN}& &
\BiBlock{CNN}\\
\cmidrule(r){2-3}  
\cmidrule(l){4-5}
& In-Sample & Out-of-Sample & In-Sample & Out-of-Sample\\
\midrule
RMSE & 1 & 2 & 3 & 4\\ MAE & 1 & 2 & 3 & 4\\\hline
MDA & 1 & 2 & 3 & 4\\ $\text{R}^2$ & 1 & 2 & 3 & 4\\
\bottomrule
\end{NiceTabular}
\end{table}

\end{document}

Output of the second code

You must log in to answer this question.

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