20 Comments

  1. AlBundy

    Hi,

    in the multiple columns and multiple rows examples, are you sure pictures match with code?. I think alignment of “X” is not “c”.

    • Right, I used a more complex column definition and changed the code to keep it as simple as possible. Also, the color is not reproduced by the example.

  2. Rachael

    Hi. Thanks for the help. I am having trouble with multiple multirows in a table. It doesn’t seem to recognise that the second multirow exists. Any ideas?

    • Hi Rachael,
      Here is an example with multiple multirows in a table. If this doesn’t help solving your issue, please provide a minimal example.

      Thanks, Tom

      \documentclass[11pt]{article}
      \usepackage{multirow}
      \begin{document}
      \begin{table}[ht]
      \caption{Multi-row table}
      \centering
      \begin{tabular}{ccc}
          \hline
          \multirow{2}{*}{Multirow}&X&\multirow{2}{*}{Multirow}\\
          &X&\\
          X&\multirow{2}{*}{Multirow}&\multirow{2}{*}{Multirow}\\
          X&&\\
          \hline
      \end{tabular}
      \label{tab:multirow}
      \end{table}
      \end{document}
  3. Renan

    Hi Tom,

    Thanks for the post. It helped me a lot.

    I wonder if it is possible to centralize the cell named “Sunday and Holiday” in the following example.

    Thanks, Renan

    \begin{table}[!htb]
    \caption{Schedule of electricity load level}
    \begin{center}
    \begin{tabular}{c|c|c|c|c}
    \hline
    \multirow{3}{12mm}{Load Level}  & \multicolumn{2}{|c|}{No daylight saving time} & \multicolumn{2}{|c}{In Daylight saving time} \\ [0.5ex] % inserts table
    %heading
     & \multirow{2}{22mm}{From Monday to Saturday}  & \multirow{2}{22mm}{Sunday and holidays} & \multirow{2}{22mm}{From Monday to Saturday} & \multirow{2}{22mm}{Sunday and holidays} \\
     & & & & \\
    \hline
    \multirow{2}{12mm}{Light} & \multirow{2}{22mm}{00:00 to 06:59} & \multirow{2}{22mm}{00:00 to 16:59 22:00 to 23:59}
     & \multirow{2}{22mm}{00:00 to 06:59} & \multirow{2}{22mm}{00:00 to 17:59 23:00 to 23:59} \\
     & & & & \\
    \multirow{2}{12mm}{Medium} & \multirow{2}{22mm}{07:00 to 17:59 21:00 to 23:59}
     & \multirow{2}{22mm}{17:00 to 21:59} & \multirow{2}{22mm}{07:00 to 18:59 22:00 to 23:59}
      & \multirow{2}{22mm}{18:00 to 22:59} \\
      & & & & \\
    \multirow{2}{12mm}{Heavy} & \multirow{2}{22mm}{18:00 to 20:59} & & \multirow{2}{22mm}{19:00 to 21:59} & \\
     & & & & \\
    \hline
    \end{tabular}
    \label{load level}
    \end{center}
    \end{table}
    • Hey Renan,

      Thanks, interesting question!

      I assume you were asking about centering horizontally. You can achieve it by placing \centering in front of the text within \multirow.

      I also took the liberty to clean up your table a bit. The main change is to define column width relative to \linewidth in order to optimally make use of space using p{\linewidth}. Let me know if you have questions.

      Best, Tom.

      \documentclass[11pt]{article}
      \usepackage{multirow}
      \usepackage{array}
      \begin{document}
      
      \begin{table}[!ht]
      \caption{Schedule of electricity load level}
      \begin{center}
      \renewcommand{\arraystretch}{1.5} % adds more space between rows
      \begin{tabular}{p{0.12\linewidth}|*{3}{p{0.20\linewidth}|}p{0.20\linewidth}}
      \hline
      \multirow{3}{\linewidth}{Load Level}  & \multicolumn{2}{c|}{No daylight saving time} & \multicolumn{2}{c}{In Daylight saving time} \\  % inserts table
      %heading
      & \multirow{2}{\linewidth}{\centering From Monday to Saturday}  & \multirow{2}{\linewidth}{\centering Sunday and holidays} & \multirow{2}{\linewidth}{\centering From Monday to Saturday} & \multirow{2}{\linewidth}{\centering Sunday and holidays} \\
      & & & & \\
      \hline
      \multirow{2}{\linewidth}{Light} & \multirow{2}{\linewidth}{00:00 to 06:59} & \multirow{2}{\linewidth}{00:00 to 16:59\hfill 22:00 to 23:59}
      & \multirow{2}{\linewidth}{00:00 to 06:59} & \multirow{2}{\linewidth}{00:00 to 17:59\hfill 23:00 to 23:59} \\
      & & & & \\
      \multirow{2}{\linewidth}{Medium} & \multirow{2}{\linewidth}{07:00 to 17:59 21:00 to 23:59}
      & \multirow{2}{\linewidth}{17:00 to 21:59} & \multirow{2}{\linewidth}{07:00 to 18:59 22:00 to 23:59}
      & \multirow{2}{\linewidth}{18:00 to 22:59} \\
      & & & & \\
      Heavy & 18:00 to 20:59 & & 19:00 to 21:59 & \\
      \hline
      \end{tabular}
      \label{load level}
      \end{center}
      \end{table}
      
      \end{document}
  4. Djomla

    Hello,
    I have one question
    Is there a option to prevent page break in multicolumn? Im using tabular

    \begin{xtabular}{|c|c|c|c|c|c|c|}
    \multicolumn{2}{l}{}\\
    \multicolumn{2}{l}{\bfseries\large Mixed Masters Walking}\\
    \hline
    \rowcolor[gray]{.9}
    1 & TEAM \\
    \hline
    2 & TEAM \\
    \hline
    \rowcolor[gray]{.9}
    3 & TEAM \\
    \hline
    4 & TEAM \
    \hline
    \rowcolor[gray]{.9}
    5 & TEAM \\
    \hline
    6 & \\
    \hline

    So I need to second multicolumn be on separate page, if it’s to big for one page.

  5. Louisa

    Do you know if it’s possible to combine multirows with alternating rowcolors?
    I want all the rows within one multirow to be the same color, but the “main” rows to change color every second row.
    When just specifying \rowcolors{2}{white}{grey}, the rows within one multirow will also alternate colors, which I don’t want. And preferably I don’t want the multirows to have visible lines between them.
    Found something similar here, but with multicolumns:
    http://tex.stackexchange.com/questions/40880/multicolumn-overrules-rowcolors.

    • tom

      Hi Louisa,

      Interesting question, thanks! In the example on tex.SX, usage of \cline doesn’t change the color for the subsequent row. You might want to try that instead of \multirow. To make the small lines between rows with the same color disappear, give \cline the same color:

      \arrayrulecolor{gray!20}\cline{1-6}\arrayrulecolor{black}

      If this doesn’t answer your question, please provide a minimal working example.

      Cheers, Tom

      • Louisa

        Thank you for looking into this issue. Your comments helped me one step in the right direction: I’ve managed to hide the cline by coloring it the rowcolor.
        Here is a stripped example of my table, which kind of works.
        As you can see there are still some issues:
        1: The right vertical table border is not shown (is it affected by the cline-color perhaps?)
        2: The “very long text” in my third “main” row is put within one cell (which I understand, due to multirow not being used). However, I would like it to span over the next three rows AND keep its color.

        And regarding the tabular column alignment:

        \begin{tabular}{|p{3cm}| p{3cm}*{1}{p{5cm}} | }
        

        Could you please explain what the p{3cm}*{1} part means (the star (*) symbol). I stole that from the example on tex.SX, but can’t really figure out how it works.

        \begin{table}
        \centering
        \rowcolors{2}{white}{gray!20}
        \newcommand{\myrule}{\ifodd\rownum\arrayrulecolor{gray!20}\else\arrayrulecolor{white}\fi}
        \begin{tabular}{|p{3cm}| p{3cm}*{1}{p{5cm}} | }
        \hline
        \rowcolor{blue} \bfseries \textcolor{white}{My text} & \multicolumn{2}{c}{\bfseries \textcolor{white}{My multirow}}\\
        \hline
        text	& First row: & color1 \\ \myrule\cline{2-3}\arrayrulecolor{black}
        & Second row: & color1 \\ \myrule\cline{2-3}\arrayrulecolor{black}
        & Third row: & color1 \\
        text	& First row: & color2 \\ \myrule\cline{2-3}\arrayrulecolor{black}
        & Second row: & color2 \\ \myrule\cline{2-3}\arrayrulecolor{black}
        & Third row: & color2 \\
        this is a very long text which should span multiple rows	& First row: & color1 \\ \myrule\cline{2-3}\arrayrulecolor{black}
        & Second row: & color1 \\ \myrule\cline{2-3}\arrayrulecolor{black}
        & Third row: & color1 	\\
        \hline
        \end{tabular}
        \end{table}
        
      • tom

        Thanks for the example. You might have to use another trick for the last set of rows. Try the modified example below. I cannot reproduce your issue 1, the right border appears properly.

        Concerning your ‘star’ question: The star belongs to the column definition on the right. You can define column-repetitions this way and save some typing especially for more complex columns. Say you have 5 left-aligned columns, so instead of lllll you could use *{5}{l} . In your example, it doesn’t make sense, because you only have one column :-). See here for some more information.

        \documentclass[11pt]{article}
        \usepackage{multirow}
        \usepackage[table]{xcolor}
        \begin{document}
        
        \newcommand{\specialcell}[2][c]{%
          \begin{tabular}[#1]{@{}l@{}}#2\end{tabular}}
        
        \begin{table}
        \centering
        \rowcolors{2}{white}{gray!20}
        \newcommand{\myrule}{\ifodd\rownum\arrayrulecolor{gray!20}\else\arrayrulecolor{white}\fi}
        \begin{tabular}{|m{4cm}| *{2}{p{3cm}} |}
        \hline
        \rowcolor{blue} \bfseries \textcolor{white}{My text} & \multicolumn{2}{c}{\bfseries \textcolor{white}{My multirow}}\\
        \hline
        text    & First row: & color1 \\ \myrule\cline{2-3}\arrayrulecolor{black}
        & Second row: & color1 \\ \myrule\cline{2-3}\arrayrulecolor{black}
        & Third row: & color1 \\
        text    & First row: & color2 \\ \myrule\cline{2-3}\arrayrulecolor{black}
        & Second row: & color2 \\ \myrule\cline{2-3}\arrayrulecolor{black}
        & Third row: & color2 \\
        this is a very long text which should span multiple rows & \specialcell{First row:\\Second row:\\Third row:} & \specialcell{color1\\color1\\color1} \\
        \hline
        \end{tabular}
        \end{table}
        
        \end{document}
      • Louisa

        Thank you! Works perfectly for the provided example. However, as the example was a bit stripped, it’s still an issue: The text which I provided as “color1″ in my example is actually a little longer in my real table (so it forces some line breaks in the cell, due to the “p” horizontal alignment). By using \specialcell, the texts in the two “specialcell” columns, are not properly aligned, as they would have been using multirow.

        I modified the \specialcell command slightly (to use “p” instead of “l”):

        \newcommand{\specialcell}[2][c]{%
          \begin{tabular}[#1]{@{}p{3cm}@{}}#2\end{tabular}}
        

        And here is an example with longer text:

        \begin{table}
        \centering
        \rowcolors{2}{white}{gray!20}
        \newcommand{\myrule}{\ifodd\rownum\arrayrulecolor{gray!20}\else\arrayrulecolor{white}\fi}
        \begin{tabular}{|m{4cm}| *{2}{p{3cm}} |}
        \hline
        \rowcolor{blue} \bfseries \textcolor{white}{My text} & \multicolumn{2}{c}{\bfseries \textcolor{white}{My multirow}}\\
        \hline
        text & First row: & color1 \\ \myrule\cline{2-3}\arrayrulecolor{black}
        		 & Second row: & color1 \\ \myrule\cline{2-3}\arrayrulecolor{black}
        		 & Third row: & color1 \\
        text & First row: & color2 \\ \myrule\cline{2-3}\arrayrulecolor{black}
        		 & Second row: & color2 \\ \myrule\cline{2-3}\arrayrulecolor{black}
        		 & Third row: & color2 \\
        this is a very long text which should span multiple rows & \specialcell{First row:\\Second row:\\Third row:} & \specialcell{I'm the explanation of the First row.\\I'm the explanation of Second row, and want to be aligned with second row.\\And I belong to Third row!} \\
        \hline
        \end{tabular}
        \end{table}
        
      • tom

        Hi Louisa,

        In that case, I’d color the rows manually as shown in the example below. Cheers, Tom.

        \documentclass[11pt]{article}
        \usepackage{multirow}
        \usepackage[table]{xcolor}
        \begin{document}
        \begin{table}
        \centering
        \begin{tabular}{|m{4cm}|*{2}{p{3cm}|}}
        \hline
        \rowcolor{blue} \bfseries \textcolor{white}{My text} & \multicolumn{2}{c}{\bfseries \textcolor{white}{My multirow}}\\
        \hline
        \rowcolor{gray!20}
        text & First row: & color1 \\
        \rowcolor{gray!20}
                & Second row: & color1 \\
        \rowcolor{gray!20}
                & Third row: & color1 \\
        text & First row: & color2 \\
                & Second row: & color2 \\
                & Third row: & color2 \\
        \rowcolor{gray!20}
        	&First row:& I'm the explanation of the First row. \\
        \rowcolor{gray!20}
        	\multirow{3}{*}{\begin{minipage}{\linewidth}this is a very long text which should span multiple rows\end{minipage}} & Second row: & I'm the explanation of Second row, and want to be aligned with second row.\\
        \rowcolor{gray!20}
        	& Third row: & And I belong to Third row!\\
        \hline
        \end{tabular}
        \end{table}
        \end{document}
  6. Anders Hovgaard

    Can anyone help with getting every second row colored in gray.. Really cant figure it out

    • tom

      Hi Anders,

      Thanks for getting in touch. I removed your table, it was quite bulky. Here is an example of how to color every alternate row in a table. The problem in your case will be that you are using \multirow. These can be coloured with either this trick and/or manually.

      \documentclass{article}
      \usepackage[table]{xcolor}
      \usepackage{multirow} 
      \begin{document}
      \begin{table}[ht]
      \rowcolors{1}{}{gray}
      \begin{tabular}{cc}
      \hline
      Single-row&Single-row\\
      &Single-row\\
      %\rowcolor{gray} % Color all 3 multi-row rows
      &Single-row\\
      \multirow{-3}{*}{Multi-row (3)}&Single-row\\
      Single-row&Single-row\\
      \hline
      \end{tabular}
      \end{table}%
      \end{document}

      HTH,
      Tom

Leave a Reply