The longtable package defines an environment that has most of the features of the tabular environment. In addition however, tables may be broken by TeX’s standard page breaking algorithm. Furthermore, it uses the same counter “table” as the table environment and has a \caption{...}
command. Finally, “longtables” will be listed in the list of tables produced by \listoftables
.
So let’s get started:
\usepackage{longtable}
Now, other than for single page tables, there a a few additional commands which define the headings and captions:
\endfirsthead
: Line(s) to appear as head of the table on the first page\endhead
: Line(s) to appear at top of every page (except first)\endfoot
: Last line(s) to appear at the bottom of every page (except last)\endlastfoot
: Last line(s) to appear at the end of the table
Complete code example:
\documentclass[12pt]{article} \usepackage{longtable} \begin{document} \begin{center} \begin{longtable}{|c|c|c|c|} \caption{A simple longtable example}\\ \hline \textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\ \hline \endfirsthead \multicolumn{4}{c}% {\tablename\ \thetable\ -- \textit{Continued from previous page}} \\ \hline \textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\ \hline \endhead \hline \multicolumn{4}{r}{\textit{Continued on next page}} \\ \endfoot \hline \endlastfoot 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ \end{longtable} \end{center} \end{document}
The \multicolumn
command may be used in longtable in exactly the same way as for tabular. When using the multicolumn command, you may need to run your code through LaTeX several times. The documentation shows an example that only looks right after four passes. Furthermore, both portrait and landscape tables are possible.
The complete longtable package documentation can be found here.
Alex
Thank you for your site, that’s really helpful thing!
I have a question according to using longtable package.
How can I fit table with lots of thing?
For example:
I’m looking forward to your answer!
tom
Hi Alex,
There are limits to what you can fit on a single page, obviously. But using landscape as well as a smaller font size will give you quite a bit more space than in the example you provided.
footnotesize, scriptsize
andtiny
are other options with tiny being the smalles font size.Other than that, keeping the column headings short will also be beneficial, but not always satisfying.
Cheers, Tom.
Alex
That’s not really work. (by using small or footnotesize or etc.) Text still locate on one line…
I would like to fit text in each cell in table.
The same things are available in Tabularx with “x” option for adjust; unfortunately, this option doesn’t work here…
Alex
For example, one did it here: http://ctan.mirror.rafal.ca/macros/latex/required/tools/longtable.pdf on page 3…
tom
Hi Alex,
I see. So if you want to use
longtable
, replacec
withp{length}
, e.g.:If you prefer
tabularx
, than here is an example on how to do that:Good luck!
Tom
Jose
Hello, and thanks for this great post. I have a longtable, but this is a long-horizontal table. I need to make it a multi-page table along the horizontal axis rather than the vertical. Any suggestions?
Thanks for your time.
tom
Hi Jose,
Thanks for your question. You may find this post on stackexchange useful. Cheers, Tom.
jessy
hello,
Haven’t been able to find an answer anywhere… I have a longtable that would spread out over three pages AND landscape AND the whole article is 2-column article. Simply adding \begin{longtable} and {landscape} wont’ work on the 2-column article. How can I do this?
Thanks so much!!
tom
Hi Jessy,
You may want to give the
supertabular
package a try. See an example with two columns here. There is also a link to the package documentation.Hope it works!
Best, Tom.
jessy
Dear Tom,
Thanks for the suggestion.. but am not getting through this. 🙁
This is what I have. I cut off many things for testing right now. But now it’s giving me error messages like,
This is `epsf.tex’ v2.7.3
)) (/usr/local/texlive/2009/texmf-dist/tex/latex/supertabular/supertabular.sty
/usr/local/texlive/2009/texmf-dist/tex/latex/supertabular/supertabular.sty:84:
Too many }’s.
\@stopfield -&>\color@endgroup \egroup
l.84 \tablehead{}
?
Any idea??
Thanks!!
Jessy
tom
Hi Jessy,
Here is a working example as a starting point. Make sure you have the right number of columns everywhere, in the definition (
\begin{supertabular}
),tablehead
and in the table content. You generally don’t needmulticolumn
for a single column. Furthermore,tablehead
will only be produced after the first page break.Best, Tom.
jessy
Dear Tom,
Thanks for the detail instruction… however, I am not getting it through.. I still get the same error message. The error message same as my previous posting…
Maybe something is wrong with my latex?
🙁
Thanks,
Jessy
tom
Hi Jessy,
Have you tried running the code I posted?
If that doesn’t work, you may want to update your version of LaTeX to the latest distribution and try again…
Good luck! Tom.
jessy
Hi Tom,
Yes I literally copied and pasted your example on my existing latex document and it didn’t work. So I made a test file with your instruction in it and it works. So I have narrowed it down where the trouble is.. it’s the document class that I am using. emulateapj. Heard of it? probably not.. it’s really specific document class that we have to use in Astronomy.
Well, the problem is still there, but I got to nail down where the trouble is. Thank you so much for helping me out. 🙂
Jessy
tom
Hi Jessy,
Nope, haven’t heard of that documentclass. Hope you were able to fix the issue in the meantime. Best, Tom.
Using footnote in tables « LaTeX Matters
[…] way to overcome this issue is by using longtable. The package handles footnotes really well. But it may be an overkill if your table is small. Also, […]
saifulkhan
Hi Tom,
I need longtable as the table is big which spans through multiple pages. Also I have lots of column with long text. e.g. the below code actually goes out of the boundary, as the width of the text is is big, i expect the width to reduce and length of such cell to increse, in this situation.
tom
Hi!
There is more than enough space for the cells above in
landscape
. You are using a fixed cell width, so LaTeX automatically adds linebreaks. Here is an example with a dynamic cell width. To keep the table width within the margins, reduce the font size, e.g.footnotesize
, and/or use fixed cell width as you did in your previous example.Btw, to reduce your typing effort, just repeat the column type as follows:
Best, Tom.
saifulkhan
Hi Tom,
Could you please tell me how can I achieve the following?
1. The size of the columns of the longtable is different and I want to explicitly different.
2. What is the syntax to reduce the font size of the text of the table?
Thank you very much.
tom
Hey Saifulkhan,
1. Just use the basic
l
,c
, orr
column type for dynamic column width.2. Enclose
longtable
with an environment that sets a different font size, e.g.List of available font sizes.
Best, Tom.
saifulkhan
Thank you very much Tom, appreciate your help here.
Azita
Hi. This is my first post here.
In my dissertation I have a long wide table. Table does not fit in lscape or sideways and I have to split a long table in several adjacent pages. I mean
splitting both length wise and width wise.
I was through web sites for hours and couldn’t find anything. Not sure if I have looked everywhere. So would be extremely grateful for suggestions or refers to other links.
P.S. my table needs a two line caption as well.
Many thanks
Azi
tom
Hi Azita,
What you need is not easy. One possible solution I can think of is changing the page size for your table. This obviously makes it hard to print the document.
Other than that, I found this similar question with two possible solutions. You may want to give it a try.
Good luck,
Tom.
sarvesh
Dear Sir, I want to display this as a column heading for my table:
Scene
______
Indoor(I)
Outdoor(O)
i mean Scene underline by two options Indoor(I) and Outdoor(O)
Please suggest me appropriate command for that particular type of things.
with regards
sarvesh
tom
How about using a
tabular
environment inside the table cell?Cheers, Tom.
sarvesh
Sir, Its working. Thanks a lot for your help.
with regards
sarvesh
tom
Great! Happy to help. Tom.
chandaniitd
I have a table split over three sub tables. Two of the table fit in the page, third one exceeds the page. Below is my code:
tom
You may want to try using 3 floating environments.
Best, Tom.
chandaniitd
I have done the same thing. But this gives me different table nos viz., table 1, table 2 ….. Not as subtable 1(a), 1(b), 1(c). How can this be done. Kindly help.
tom
Ok, I see. In that case, you could use the subfig package and split the table manually using the
\ContinuedFloat
command.You may also find this post on subfig helpful.
Good luck! Tom.
chandaniitd
thanks Tom.
tom
you are welcome!
Joshua B.
Wow, this is perfect for replacing supertabular for me. Thank you.
maxweberko
Hi Tom,
I am new to LaTeX and almost self-taught to learn it.
Here is my question: I would like to use the package{longtable} to make a table spanned on two pages. Trying to use the codes below did not work. Please let me know what’s wrong with the codes.
One more thing, I still don’t know where to insert the codes for the text “continued till next page” in the codes. Shall I insert the codes before {tabular}?
Thank you so much!
Best,
Maxweberko
tom
Hi there!
Thanks for the code, that made it easy to fix. You almost got it right. Just a few fixes:
center
andlongtable
environments the wrong way round.longtable
environment replacestabular
and nottable
. Table is a floating environment which only makes sense for content that spans less than a page.endfirsthead
,endhead
,endfoot
,endlastfoot
). Change it according to your needs.\begin{landscape}...\end{landscape}
to rotate the pageYou can find the updated code below.
Nice table by the way! Cheers, Tom
maxweberko
Hi Tom,
Thank you so much for the fixes! I am so happy to see that table spanned on two pages.
I will follow the codes you provided and add some columns in order to make them match the statistics. If I have further questions, I will post them here.
Have a nice day!
tom
Glad to help. Let me know if you have any further question…
Best, Tom
Jefferson Peruzzo (@jeffe_peruzzo)
Sorry about the last post, I think it’s not clear.
What I meant is that: is it possible to make something like a box around the text is such a way that it will automatically break to the next page. Just like in this example: https://www.dropbox.com/s/16anlu5thx8431n/tabela_exemplo.jpg
when you type the text and there is no more space, it automatically break to next page, splitting the table, and filling the pages.
I’d be really grateful for your help. Thanks a lot.
Jefferson
tom
Thanks for your comment, an interesting question!
I suggest using the mdframed package. Below is a minimal example that creates roughly the output in the picture.
Samaras
It worked!
kishore
Hey Tom
In longtable definition line below, what does
@{}
mean?Thanks
K
tom
Hi Kishore,
Briefly, the usage in your example would remove any space before the first and after the last column.
This is documented for example in the array package documentation and explained in more details here.
HTH, Tom
Mary
Hello Tom,
well hoestly I am not sure if my question fits in here, but you seem to know the things about big tables…
I need a table with 64 columns to fit in one text row.
(The columns don’t include s.th. – I need to color them in different colors in the end.)
I already tried with variable cells and \footnotesize’ing the table but it seems that the size never change and at most about 30 columns are shown…
What can I do?!?
tom
Hi Mary,
Thanks for your question.
I would be useful if you provided a minimal example. Could you post the code that you have for just a few columns. I will then try to propose a solution for 64 columns if possible.
Best, Tom.
tom
Would this be what you are looking for?
Mary
Woow yes that is exactly what I was looking for! Thank you very much!
This made my day =)
Francisco Pacheco
Thank you very useful!
tom
Thanks for the feedback. Glad you found this useful. Best, Tom
hayat khan
Dear Sir,
I have 7 columns , I want that Data may be wrap, is there any option in longtable. I will adjust column for complete page
tom
Hi Hayat,
You can use the array package to define cell width for every column as a fraction of
linewidth
. Please provide an example and some more details on the problem and I will be happy to look into it and make a suggestion.Best, Tom
Alfonso Saera
Hi Tom,
I am trying to fit a very long table in landscape mode in several pages. I think I need to run the code through LaTeX several times, as you and the manual say, but I have no idea how to do this.
This is an automatic report so I have a python script that creates the tex file with the required text and the compiles it like xelatex –interaction=nonstopmode report.tex”.
Any help would be very appreciated.
Thanks!
tom
Hi Alfonso,
Thanks for your question. Can you repeat the line which compiles the code in your python script multiple times or use a loop?
Best, Tom