Monday, 9 April 2007

Tables on multiple pages with latex

It is Easter break so I have finally some time to write my Master's Thesis ("Delivering accessible information from soical semantic information sources through adaptive hypermedia" - yeah, I don't understand it either :D ). Anyway, I'm using latex with TeXclipse. Yesterday I tried to create my first ever table using latex.

There is a latex directive "tabular", which I was trying to use. Problem with my table was that it was quite long and had to be spread on multiple pages. Tabular does not support this kind of long tables and it simply cuts the table at the end of the page.

I've found example using "longtable" directive for creating tables on multiple pages but I did not have appropriate package installed. I followed this instruction and installed necessary packages. Now it is enough to declaire use of this package \usepackage{longtable} and create a longtable.

Package installation precedure:
  1. Download set of additional packages tools.zip from www.ctan.org.
  2. Extract package's content
  3. Enter folder with extracted files and run command
    latex tools.ins
    to create *.sty files
  4. Copy *.sty to folder with latex plugins. In my case it's
    /usr/share/texmf-tetex/tex/latex.
  5. Refresh latex files db by running command texhash
You use the "longtable" directive very similar to tabular. Here is simple example:

\begin{longtable}{c|l|p{5cm}}
\hline
content of cell 1 (centered) &
content of cell 2 (aligned left) &
content of cell 3 (multiplerows, 5cm width) \\
\hline
\end{longtable}

PS. Longtable directive helped me with my problem but it still doesn't work as I expected. If a single cell contains a lot of text in multiple rows, so it takes more than one page to render it, it is also cut at the end of the page. I don't know how to fix it (yet :>).

18 comments:

Jarosław Dobrzański said...

You forgot to mention one has to add

\usepackage{longtable}

to include the proper package :)
Anyway thanks ;)

Filip Czaja said...

You obvisly did not read the post careful enough. Please try again :)

Jarosław Dobrzański said...

upss.. sorry :)

loop-bell said...

Nice post -- now I know how to do these for my Physics homework :)

yue said...

Thanks for your instruction. I've managed to fix my problem with my long table.

Jaap said...

Thanks! This post helped me out! The package was standard included in my TexLive distribution.

Pelerin-Voyageur said...

Thanks for the post! :-)

Vlad said...

Thanks for longtable, useful... but how to duplicate (automatically) the first row on each new page?

Renee said...

Vlad, it's quite easy to do. I can't remember where I first learn't it, but this is taken from my honours thesis:


\begin{longtable}{l|p{1.5in}|p{2in}}

\caption{Validity Risk Analysis \label{table:riskAnalysis}}\\

%This is the header for the first page of the table...

\hline \hline
{\textbf{Classification}} &
{\textbf{Risk}} &
{\textbf{Precautionary Action}}\\
\hline
\endfirsthead

%This is the header for the remaining page(s) of the table...

\multicolumn{3}{c}{{\tablename} \thetable{} -- Continued} \\[0.5ex]
\hline \hline
{\textbf{Classification}} &
{\textbf{Risk}} &
{\textbf{Precautionary Action}}\\
\hline
\endhead

%This is the footer for all pages except the last page of the table...

\multicolumn{3}{l}{{Continued on Next Page\ldots}} \\
\endfoot

%This is the footer for the last page of the table...

\hline \hline
\endlastfoot

%Now the data...

{History} & {Student could receive private tutoring} & {Survey addressed this question. Students who fell into this category were omitted from the results.}\\
\hline

\end{longtable}

Rock Hyrax said...

Thanks to both Filip and Renee for this useful post. :)

Mike said...

If you want to be able to use "justified" columns (as in tabularx and tabulary) on several pages, you may use ltxtable (http://tug.ctan.org/tex-archive/help/Catalogue/entries/ltxtable.html)

Note, though (re-Renee's comment), that "The \multicolumn command is suppressed in environments included in this way" (according to the ctan page).

Mike said...

Addendum: in spite of what the ctan page says, \multicolumn seems to work ok with ltxtable.

mundhra said...

@Reene: thanks a lot, you saved my day

sarah said...

@renee Thank you for the info. You've saved me a lot of time!

Anonymous said...

Like it, Reene. Thanks much.

Gustavo Siles said...

A very useful PDF about this issue: http://www.tug.org/pracjourn/2007-1/mori/mori.pdf

Leandro Meyer said...

I don´t know why, but I can´t use this package. I´ve instaled the package correctly (at least I think so), but when I write '\begin{longtable}{ll}' it´s like the LEd doesn´t recognize the comand. Does anyone know what could possibly be wrong?

Swagatika said...

Thanks. This is very helpful. I am trying to implement a table of images with many rows which cross a page. However, this still cuts the table at page end and does not show other images in next page... Is there anything I need to do for table of images?