Search code examples
latexmultirow

Problem with multirow and multicolumn in LaTeX


I have a problem with the creation of a table on LaTex while using multicolumn and multirow. Here the code that I'm using

\documentclass[12 pt, letterpaper, twoside, openright]{book}

\usepackage{amsmath} 

\usepackage{multirow} 

\usepackage{enumitem}
\usepackage{booktabs, makecell, longtable}
\usepackage{stackengine}
\usepackage{tabularx}
\usepackage{caption}
\usepackage{subcaption}

\usepackage[super]{nth} 

\usepackage{array}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\usepackage[table]{xcolor}
%http://latexcolor.com/
\definecolor{amber}{rgb}{1.0, 0.75, 0.0} %giallo ocra

\begin{document}

\begin{table}[!b]
\centering
  \begin{tabular}{l|ccc|ccc|ccc}
    \multirow{2}{*}{Periods and Hours} &
      \multicolumn{3}{|c|}{\cellcolor{amber}Total} &
      \multicolumn{3}{|c|}{\cellcolor{amber}First period} &
      \multicolumn{3}{|c}{\cellcolor{amber}Second Period} \\[0.5ex]
      & \cellcolor{amber}hypo & \cellcolor{amber}\cellcolor{amber}normo & \cellcolor{amber}hyper& \cellcolor{amber}hypo & \cellcolor{amber}normo & \cellcolor{amber}hyper& \cellcolor{amber}hypo & \cellcolor{amber}normo & \cellcolor{amber}hyper\\[0.5ex]
    \hline
     \multirow{2}{3em} TIR (\%) PP1 & 54,02 & 14,59 & 31,38 &48,7 &9,48&41,77&59,9&20,29&19,8\\
      Hour(h) & 57 & 15 & 33 &27 &5&23&30&10 &10\\
      \hline
  \end{tabular}
\end{table}

\end{document}

and this is the output

enter image description here

I don't know why the TIR and Hours is so mess up. Please help me :(


Solution

  • The syntax \multirow{2}{3em} TIR (\%) PP1 is wrong. \multirow has 3 mandatory arguments, the last one the content of the cell. So if you actually want to merge two cells, you need \multirow{2}{3em}{ TIR (\%) PP1} (notice the {...} around the cell content)

    ... that being said, the cell below this is already filled with Hour(h). If you want the cell above to be merged with this one, you have to leave it empty.

    \documentclass[12 pt, letterpaper, twoside, openright]{book}
    
    \usepackage{amsmath} 
    
    \usepackage{multirow} 
    
    \usepackage{enumitem}
    \usepackage{booktabs, makecell, longtable}
    \usepackage{stackengine}
    \usepackage{tabularx}
    \usepackage{caption}
    \usepackage{subcaption}
    
    \usepackage[super]{nth} 
    
    \usepackage{array}
    \newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
    \usepackage[table]{xcolor}
    %http://latexcolor.com/
    \definecolor{amber}{rgb}{1.0, 0.75, 0.0} %giallo ocra
    
    \begin{document}
    
    \begin{table}[!b]
    \centering
      \begin{tabular}{l|ccc|ccc|ccc}
        \multirow{2}{*}{Periods and Hours} &
          \multicolumn{3}{|c|}{\cellcolor{amber}Total} &
          \multicolumn{3}{|c|}{\cellcolor{amber}First period} &
          \multicolumn{3}{|c}{\cellcolor{amber}Second Period} \\[0.5ex]
          & \cellcolor{amber}hypo & \cellcolor{amber}\cellcolor{amber}normo & \cellcolor{amber}hyper& \cellcolor{amber}hypo & \cellcolor{amber}normo & \cellcolor{amber}hyper& \cellcolor{amber}hypo & \cellcolor{amber}normo & \cellcolor{amber}hyper\\[0.5ex]
        \hline
    %     \multirow{2}{3em}
         TIR (\%) PP1 & 54,02 & 14,59 & 31,38 &48,7 &9,48&41,77&59,9&20,29&19,8\\
          Hour(h) & 57 & 15 & 33 &27 &5&23&30&10 &10\\
          \hline
      \end{tabular}
    \end{table}
    
    \end{document}
    

    Personally, I would use the tabularray package, this makes merging cells so much easier:

    \documentclass{book}
    
    \usepackage{xcolor}
    \definecolor{amber}{rgb}{1.0, 0.75, 0.0} %giallo ocra
    \usepackage{tabularray}
    
    \begin{document}
    
    \begin{table}[htbp]
      \begin{tblr}{
        colspec={l|ccc|ccc|ccc},
        cell{1-2}{2-10} = {amber}
      }
        \SetCell[r=2]{} Periods and Hours &
        \SetCell[c=3]{} Total & & & 
        \SetCell[c=3]{} First period & & &
        \SetCell[c=3]{} Second Period & &\\
        & hypo & normo & hyper& hypo & normo & hyper& hypo & normo & hyper\\
        \hline
         TIR (\%) PP1 & 54,02 & 14,59 & 31,38 &48,7 &9,48&41,77&59,9&20,29&19,8\\
          Hour(h) & 57 & 15 & 33 &27 &5&23&30&10 &10\\
          \hline
      \end{tblr}
    \end{table}
    
    \end{document}