39 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

  7. Sofja

    Thank you for your tutorial! I wonder how can I spann the rows on the right side of the table? I would appreciate your helf a lot!

    • tom

      Hi Sofja,

      Below is an example of multirow-row spanning on the right.

      HTH, Tom

      \documentclass[border=10pt]{standalone}
      \usepackage{multirow}
      \begin{document}
      \begin{tabular}{cc}
          \hline
          X&\multirow{3}{*}{Multirow}\\
          X&\\
          X&\\
          \hline
      \end{tabular}
      \end{document}
  8. Hi Tom,
    Thanks for the post, it helped me hugely!
    Can you tell about how to put that vertical bar “|c|” in the multirow-line too?

    \begin{table}[!h]
        \selectlanguage{brazil}
        \caption{Tabela de erro para polarização C.A. (Em porcentagem), comparando-se as Tabelas \ref{TabelaCAcalculados} e \ref{TabelaCAmedidoscalculados}.}
        \label{TabelaErroCA}
        \begin{center}
            \begin{tabular}{|c|c|c|c|}
                \hline
                \multirow{2}{*}{Erro}   &$A_{V_{NL}}$   &$A_{V_{L}}$    &$A_{V_{S}}$    \\ \cline{2-4}
                                        &2,0            &2,0            &5,5            \\ \hline
            \end{tabular}
        \end{center}
    \end{table}
    • tom

      Hi Gabriel,

      Thanks for the example. In the code you provided, all vertical lines are produced. Would you mind to clarify the question, please?

      Tom

      • I am so sorry! Provided wrong example! This one here, it doesn’t show the vertical lines at the sides of the multicolumn. All others have.

        \begin{table}[!h]
                \caption{Caption}
                \label{Table1}
                \begin{tabular}{|c|c|c|c|c|}
                  \hline
                  \multicolumn{5}{c}{Title}\\\hline
                  X&X&X&X&X\\\hline
                  10,88&5,07&8,34&3,03&-3,24\\\hline
                  X&X&X&X&X\\\hline
                  12,96&4,62&2,53&2,53&0,108\\\hline
                \end{tabular}
        \end{table}
      • tom

        Hi Gabriel,

        You can add the vertical lines with the alignment parameter of the multicolumn command.

        \multicolumn{5}{|c|}{Title}\\\hline

        HTH,
        Tom

  9. Caspar

    Hello Tom,
    thank you very much for this very helpfull blog!
    I’ve got a problem with multirow which i’m unable to solve. Any help would be much appreciated.
    The text in the multirow is not centerd but goes over the lower cell-boundary:

    \begin{tabular}{| C | C{3cm} | C{3cm}  | C{3cm} | C{3cm} |}   
        \hline
         \textbf{category} & \textbf{absolute donations} & \textbf{donations relative to wealth} & \textbf{donations relative to income} & \textbf{Bekkers \&  Wiepking} \\ \hline
        job & 1 & 6 & 4 & 5 \\  \hline
        wealth & rich > poor & poor > rich; exception: 7th decile; thoose who dont give have the least & rich > poor & \multirow{2}{*}{\parbox[c][/c]{3cm}{positive, but inelastic(growth in donations  low income & unclear pattern & unclear &   \\ \hline
        real estate & rich > poor & poor > rich; exception: 7th decile; thoose who dont give have the least  & rich > poor & 5\\ \hline
       social engagement & 5 & 6 & 4 & 5\\ \hline
      \end{tabular}
    • tom

      Hi Caspar,

      Thanks for your question. Next time, please provide a complete minimal example. Otherwise, you risk that I won’t be able to help because I can’t reproduce the problem. For example the column-type C is not defined by default. I assume you copied the style from here. Hope you can understand.

      Anyway, below is an example that works. If this doesn’t solve your problem, please provide a minimal working example.

      Thanks, Tom

      \documentclass[10pt]{article}
      \usepackage[T1]{fontenc}
      \usepackage{multirow}
      \usepackage{array}
      \newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
      \begin{document}
      \begin{tabular}{| *{4}{C{2cm}|} C{3cm} |}   
      	\hline
      	\textbf{category} & \textbf{absolute donations} & \textbf{donations relative to wealth} & \textbf{donations relative to income} & \textbf{Bekkers \&  Wiepking} \\ \hline
      	    job & 1 & 6 & 4 & 5 \\  \hline
           	social engagement & 5 & 6 & 4 &\multirow{2}{*}{\parbox{\linewidth}{positive, but inelastic growth in donations  low income}}\\ \cline{1-4}
      	social engagement & 5 & 6 & 4 & \\ \hline
      	social engagement & 5 & 6 & 4 & 5\\ \hline
      	\end{tabular}
      \end{document}
  10. Jessica

    Hello Tom

    I am a LaTeX idiot so I’m really struggling. I’m trying to recreate your example “multiple rows and columns”. If I do it as you did, the text carries over to the next line, but the very start of the next line. It does not carry on below the line I want it to in the last column

    \begin{table}[htb!]
    	\small
    	\centering
    	\begin{tabular}{cccccccc}
    		\topline    
    		
    		\headcol\multicolumn{1}{c}{}&\multicolumn{4}{c}{ILW-silo}&\multicolumn{3}{c}{LLW-silo}\\ \midline
    		
    		\headcol\multicolumn{1}{c}{}&\multicolumn{1}{c}{Bituminised}&\multicolumn{3}{c}{Filter rods and metal scrap}&\multicolumn{3}{c}{\multirow{2}{*}{Maintenance waste, filter rods, metal}} \\ 
    	                \bottomline
    	\end{tabular}
    \end{table}

    So basically “scrap and solidified liquids” goes all the way to the left of the line and I need it to be on the right.

    • tom

      Hi Jessica,

      From the example, I’m having a hard time to see how you’d like to structure your table. Here are a few suggestions that may be useful:

      • You don’t need to specify \multicol{1}{}{}, for single-column cells just add the content.
      • It seems you added an extra column to set the row color. Use \headcol{}Cell-text...&... instead
      • You can specify a fixed column width, globally and in multicolumn, using for example m{0.2\linewidth} instead of c This is documented in the array package. I got the impression you used \multicolumn to add extra column space.
      • I wasn’t able to find \topline, \midline, \bottomline. Did you define these commands or should they rather be \toprule, etc. as defined in booktabs?

      If you need more help, please provide a minimal working example, which includes a preamble and 1-2 lines of table content.

      Thanks,
      Tom

      • Jessica

        Hi Tom,

        Thanks so much for getting back to me.

        Regarding the first 3 points: thank you I will do that!

        Regarding the last point: you are correct, it should be \toprule

        🙂

  11. Iza

    Hey Tom,

    i am having troubles with the multicol… actually it works just fine most of the time but in the row ‘concentration of nanoparticles’ the multicol does not seem to work and i cannot figure out why.. 🙁

    \begin{table}
    \caption{Key points of the applied samples and experimental configuration}
    \begin{tabular}{lcccc}
    \hline
    Type of Cell & \multicolumn{4}{c}{HeLa}\\
    \hline
    Type of Nanoparticles & \multicolumn{2}{c}{Cl$_2$ + FeCl$_3$ + Citrate} & \multicolumn{2}{c}{Cl$_2$ + FeCl$_3$ + Mannitol}\\
    Concentration of Nanoparticles & 2,4 \mu l  & \multicolumn{2}{c}{4,8 \mu l} & 7,2 \mu l\\
    Incubation time & \multicolumn{2}{c}{24 h} & \multicolumn{2}{c}{48 h}\\
    Position of the Magnet & \multicolumn{2}{c}{underneath} & \multicolumn{2}{c}{at the side}\\
    Time of Excitation & \multicolumn{4}{c}{1-6 minutes}\\
    Applied force & \multicolumn{4}{c}{400 N}\\
    \hline
    \end{tabular}
    \label{KeyPoints}
    \end{table}
  12. Carlos Santos

    Hello Tom. Thanks for your helpful blog. I have a question about this multicolumn and multirow problem. What about if I want to do this not for header row but within some data cells of my csv imported table? Is this possible?

    • tom

      Hi Carlos,

      That’s an interesting question. Depending on the package you use to import your CSV file, it may or may not be possible. My suggestion would be to use an online service such as this to convert your CSV file to a LaTeX table.

      Hope this helps.

      Best wishes, Tom

      • Carlos Santos

        Hello again Tom.

        Thanks for your answer. In fact, yesterday I decided to try the excel add-in excel2latex and inputed the resulting file in my .tex project. With some inner code adjustments I’m being able to do several things I was intending but not achieving before… Like multi column/row.

        Your LaTeX Table Generator suggestion looks much alike what I’m doing with the mentioned converted file and it seems a very interesting option. Thank you very much!

        Best regards,
        Carlos

Leave a Reply