I always forget how to do this. If you have the same problem, here is a quick post for your bookmarks…
Basic commands
%multi-column \multicolumn{number cols}{align}{text} % align: l,c,r %multi-row \usepackage{multirow} \multirow{number rows}{width}{text}
Using *
as width in the multirow
command, the text
argument’s natural width is used (multirow package documentation).
Multiple columns
\documentclass[11pt]{article} \begin{document} \begin{table}[ht] \caption{Multi-column table} \begin{center} \begin{tabular}{cc} \hline \multicolumn{2}{c}{Multi-column}\\ X&X\\ \hline \end{tabular} \end{center} \label{tab:multicol} \end{table} \end{document}
Multiple rows
\documentclass[11pt]{article} \usepackage{multirow} \begin{document} \begin{table}[ht] \caption{Multi-row table} \begin{center} \begin{tabular}{cc} \hline \multirow{2}{*}{Multirow}&X\\ &X\\ \hline \end{tabular} \end{center} \label{tab:multicol} \end{table} \end{document}
Multiple rows and columns
\documentclass[11pt]{article} \usepackage{multirow} \begin{document} \begin{table}[ht] \caption{Multi-column and multi-row table} \begin{center} \begin{tabular}{ccc} \hline \multicolumn{2}{c}{\multirow{2}{*}{Multi-col-row}}&X\\ \multicolumn{2}{c}{}&X\\ \hline X&X&X\\ \hline \end{tabular} \end{center} \label{tab:multicol} \end{table} \end{document}
The examples provided are very basic. Drop me a comment below in case you are having problems with a more complex table. Also, the color is just for illustration purposes and for simplicity reasons not part of the minimal working examples above. See here for how to color table columns or rows.
Partial horizontal line
Use \cline{columns}
for a horizontal line spanning one or several table columns.
\documentclass[11pt]{article} \begin{document} \begin{table}[ht] \caption{Partial horizontal line} \begin{tabular}{ccc} \hline \multicolumn{2}{c}{Multi-column}&\\ \cline{1-2} X&X&X\\ X&X&X\\ \hline \end{tabular} \end{center} \label{tab:multicol} \end{table} \end{document}
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”.
tom
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.
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?
tom
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
ashish srivastava
this code in not working every time a error occured “I can’t write on file mainnew.pdf .. pls reply
tom
Hi Ashish,
Thanks for your question. I don’t think there is a problem with the code. The error sounds like there is another problem, i.e. the file cannot be replaced. Perhaps you opened
mainnew.pdf
with another program which locks the file. See here for a similar question.Hope this helps,
Tom
munmun
thanks
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
tom
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 usingp{\linewidth}
. Let me know if you have questions.Best, Tom.
Djomla
Hello,
I have one question
Is there a option to prevent page break in multicolumn? Im using tabular
So I need to second multicolumn be on separate page, if it’s to big for one page.
tom
Hi Djomla,
Thanks for the question. I suggest to use the longtable package which automatically inserts page breaks. Take a look at my post on the topic and also this trick to color every other row.
Cheers, Tom.
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: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:
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.
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.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”):
And here is an example with longer text:
tom
Hi Louisa,
In that case, I’d color the rows manually as shown in the example below. Cheers, Tom.
Louisa
Ok, I see.
Thank you very much for looking into it. 🙂
Aditya Dodda
Thanks. Super helpful.
tom
Great to hear, thanks! Tom
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.HTH,
Tom
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
Gabriel Goes
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?
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
Gabriel Goes
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.
tom
Hi Gabriel,
You can add the vertical lines with the alignment parameter of the
multicolumn
command.HTH,
Tom
Gabriel Goes
Thank you very much man! Keep up the good work.
Whenever come to Brazil, I own you a beer! o/
tom
Nice, thanks!
All the best, Tom
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:
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
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
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:
\multicol{1}{}{}
, for single-column cells just add the content.\headcol{}Cell-text...&...
insteadm{0.2\linewidth}
instead ofc
This is documented in the array package. I got the impression you used\multicolumn
to add extra column space.\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
🙂
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.. 🙁
tom
The problem is that if all rows have
multicolumn
, the column width cannot be calculated. Try using the tabu or tabularx package instead. See here for some more information.HTH, Tom.
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
tom
Great to hear you found a solution. Thanks for the feedback.
Cheers,
Tom
Neeraj Sharman
Hii Tom
I am facing problem in this code segment :
Here if at the place of 3 we use 2 it doesn’t make any issue but with 3 table alignment get changed.
Thanks for your great tutorial
tom
Hi Neeraj,
Your code works for me if I add an extra column (
ccc
instead ofcc
) totabular
. Here is the complete code:Debra Jackson
You are the latex table expert! Thank you!
Bexultan
Hey there. Such an interesting helpful blog. There is a question. I have a table; the eshould be multiple in second column, but the first one is single ones. Then 3-4 columns are single too.
Like:
\begin (table)
Xxx & multirow (2)(*)(Yyy) & pop & poop \\ \cline (0-1)(3-4)
Qqq & & you & ttt \glide
End
Don’t pay attention to grammar. I’m texting from phone
tom
Hi Bexultan,
See code below. Next time, please read the article first. Then, provide a minimal working example and try to formulate a problem or a question. With that, I’m happy to help. If you just ask me to write code for you, I may not reply.
Nadirah
Hi Tom
I would like to draw a table with 2 rows with 5 columns. In the second row, the second column till column number 5, it has 3 more subcolumns in each. I do not know how to draw that in table.
Nadirah
This is what I have been trying to do
It does not want to work
tom
Hi Nadirah,
My suggestion would be to insert a
tabular
environment in each cell with three columns. Alternatively, you can also use the multicol package as in your example. But you have to be careful to use the same number of columns in each row. In the example, you define a table with 5 columns (\begin{tabular}{|c|c|c|c|c|}
), but the first row occupies 9 columns (\multicolumn{9}{c}{...}
).Hope this helps,
Tom
Nadirah
Hi Tom
You re brilliant, thanks so much. I have modified the code according to your suggestion, it is working perfectly now. But I have another problem that is, I would want my table to be smaller. The one that I have now is taking the space of my A4. Here is my modified code:
When I mean smaller- means like could I make this sentence “The column in the matrix”
Become like this:
“The column
in the matrix”
How do I write that in the table?
Thanks Tom.
tom
Hi Nadirah,
One possibility to make it smaller is to reduce the font size (e.g. with \begin{small}…\end{small}). But what you want is to define fixed width columns. This is defined in the array package. Please see the documentation for more details. Below is a quick fix that gives every column the same width (
0.2\linewidth
). This still slightly reaches into the margin. You can define a different width for every column so that it fits nicely.Best, Tom.
Nadirah
Hi Tom
Thanks alot for your input.I have modified my code and this is how it looks like:
[…]Removed code by Tom[…]
I find writing a table is the most difficult part for me in Latex, since I am beginner to Latex.
My next question is, how do I want to draw a full vertical line for the subcolumn? I have included the vertical line in line 2 in the lLatex code, but cant seem to achieve it.
tom
In this case, I think it makes more sense to do it the hard way and write everything out (see below). If you have several rows of table content, it might be a good idea to have a full-page table on a landscape page. This way, there is more horizontal space. You might find my short post on landscape useful.
Cheers, Tom
Nadirah
Hi Tom
Thanks for this. Do you have any suggestions on how to remove white spaces between the figure and text?
tom
Hi Nadirah,
See here for some help with whitespace around figures.
HTH,
Tom
uhayatpk
Hello Tom,
Here is my code. I want to fix the width of table. But I fail to do this. Can you help me.
\setlength{\tabcolsep}{1pt}
\begin{tabular}{l*{12}{c}}
\hline\hline
\multicolumn{12}{l}{\hspace{8cm}\textbf{Panel B: Return and Profitability Descriptive Statistics}}\\
\hline
&\multicolumn{1}{c}{Non\_Dur}&\multicolumn{1}{c}{ Durable}&\multicolumn{1}{c}{Manufacturing}&\multicolumn{1}{c}{Oil \& Gas }&\multicolumn{1}{c}{Chemicals}&\multicolumn{1}{c}{Bus Eqp}&\multicolumn{1}{c}{Phone \& TV}&\multicolumn{1}{c}{Utilities}&\multicolumn{1}{c}{Whole\_Ret}&\multicolumn{1}{c}{Healthcare}&\multicolumn{1}{c}{Finance}&\multicolumn{1}{c}{Other}\\
\hline
ROE & 0.077& 0.057& 0.034& 0.031& 0.026& -0.069& -0.927& 1.096& 0.094& -0.281& 0.450& 0.001 \\
& 0.119& -0.111& 0.126& -0.755& 0.117& -0.050& 0.102& 0.116& 0.059& 0.067& 0.100& -0.000 \\
& -1.067& 6.948& -2.569& 7.551& -1.519& -0.240& -0.930& 3.766& 0.998& -8.318& 4.282& 0.053 \\
[0.5em]
\hline
CF & 0.061& 0.051& 0.060& 0.059& 0.044& 0.009& 0.029& 0.029& 0.062& -0.140& 0.003& 0.038 \\
& 0.062& 0.069& 0.058& 0.076& 0.058& 0.042& 0.054& 0.037& 0.052& -0.035& 0.021& 0.002 \\
& -1.044& -7.180& 2.385& -6.253& -6.694& -23.859& -8.631& -6.328& 11.392& -37.083& -6.982& 7.542 \\
[0.5em]
\hline
EBIT\_MAR & 0.043& -0.016& -0.066& -0.639& -0.443& -1.742& -1.598& 0.017& -0.012& -17.730& 0.018& -1.102 \\
& 0.075& 0.050& -0.050& -5.519& 0.034& -0.254& -0.163& 0.185& 0.020& -8.070& 0.095& -0.137 \\
& -2.244& -6.142& -0.519& 10.781& -4.773& -5.256& -1.514& -11.954& -2.024& -5.218& -0.509& -8.969 \\
[0.5em]
\hline
GM & 0.340& 0.262& 0.217& 0.152& 0.146& -0.847& -0.696& 0.221& 0.277& -14.841& 0.350& -0.587 \\
& 0.323& 0.285& 0.176& 0.042& 0.361& 0.285& 0.234& 0.279& 0.237& -7.230& 0.276& 0.165 \\
& 2.168& -2.748& 1.644& 0.820& -5.184& -4.208& -1.029& -9.724& 5.978& -4.262& 0.583& -7.792 \\
[0.5em]
\hline
TAX & 0.339& 0.309& 0.221& 0.264& 0.278& 0.141& 0.263& 0.392& 0.564& 0.249& 0.246& 0.138 \\
& 0.300& 0.131& 0.369& 0.276& 0.344& 0.162& 0.018& 0.329& 0.306& 0.207& 0.204& 0.216 \\
& 1.820& 5.471& -3.440& -0.289& -3.618& -1.018& 3.346& 2.985& 1.382& 0.862& 0.986& -1.205 \\
[0.5em]
\hline
RET12\_L & 1.211& 1.223& 1.212& 1.240& 1.215& 1.320& 1.261& 1.166& 1.239& 1.322& 1.144& 1.232 \\
& 1.217& 1.241& 1.208& 1.257& 1.176& 1.312& 1.293& 1.151& 1.229& 1.318& 1.212& 1.208 \\
& -1.006& -2.006& 1.176& -2.261& 5.777& 1.258& -2.815& 6.934& 1.854& 0.631& -23.983& 6.033 \\
\hline\hline
\end{tabular}
tom
Hi there,
Please don’t just ask me to fix your table. I’d appreciate if you explained the problem in a little more details. The code is below. I suggest to make it a full-page landscape table.
Best,
Tom
Tharek M
Thank you.
Tyler Jackson
Hello Tom,
Thank you for this very succinct guide. I am having problems implementing any multicolumns in my document (part of a thesis) . My document compiles without the table. I copied in your first example and i get an error:
“`
! LaTeX Error: \mathrm allowed only in math mode.
See the LaTeX manual or LaTeX Companion for explanation.
Type H for immediate help.
…
l.636 \multicolumn{2}{c}{Multi-column}
\\
You’re in trouble here. Try typing to proceed.
If that doesn’t work, type X to quit.
! Missing $ inserted.
$
l.636 \multicolumn{2}{c}{Multi-column}
\\
“`
If I comment out the row with the multicolum then it compiles. I have looked everywhere online and while I see some results saying that if the contents are math only commands then this error shows up but obviously that’s not the case here.
I’m hoping you have seen something like this before. The thesis does have a custom style file so I’m worried that the source of this problem lies in there.
Obviously I’m trying to put a more interesting table in but this is currently my minimal case reproducing the problem.
Thanks for any help you may be able to provide.
tom
Hi Tyler,
Can you please provide a minimal example. From the error I’d say that you are missing some $-signs around
\mathrm
. But I can’t be sure without seeing your code.Best, Tom
Elibeth Rugerio López
Hey, thanks for this support. How can i put a pic file into a cell?
tom
Take a look at this post. If that doesn’t answer your question, please provide a minimal working example.
Thanks,
Tom
Rim Jouini
Can you please provide me a solution: My document compiles but \multirow {the value for the cell width }or {*} appear in the text!!!
tom
Hi there,
Did you load the
multirow
package?Please provide a minimal working example to illustrate the problem and I will be happy to take a look.
Thanks,
Tom
Ian Cavalcante
Thanks for the explanation. It helped me! Greetings
Zahira
Thank you for your advice about multirow,
it helped me so much.
Ishwa
When I type on one column of my table, the row made on other columns start growing larger. The growing size comes to the equal of the text that I type. How to stop this problem, may I get somebody’s help. I have spent three days on google, but hopeless.
tom
Hi Ishwa
I describe how to control the width of a column in quite some detail in this post.
HTH, Tom
Ahmad Abusukhon
Hi Tom,
I have a problem with a long table. The table width and height can not be fit in one page. I tried “longtable” and “landscape” , but the table still appears on one page only and there is missing rows and columns (many rows and columns still out of the page boundary). I need this table to be continued on the next page since one page is not enough.
Please help.
tom
Hi Ahmad,
Take a look at this post on longtable. If you need specific help with the table, please provide a minimal working example.
Best,
Tom
Sarah M.
Hello Tom !
I’m not used to leave comments on posts I read on the Internet, especially when posts are “that old” but this post (and all your answers !) helped me so much that I could not leave without writing a thank you note. So here it goes :
Thank you VERY MUCH for your well written and crystal clear tutorial! I’m currently writting my PhD thesis and your tutorial really helped me a lot.
Best regards,
Sarah.
Arundale Ramanathan
Dear Sir, your article was very useful to implement multi column correctly in my article. Many thanks.