13

I use longtable package to produce a table covers two pages, but if there are too many words in one column, the sentence can not switch to the next line automatically, see picture, the 4th column.enter image description here Below is the "code"

\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 & 4dddddddddddd asdfdasfasdf asödflkjasdf asdfasdfasdf \\ 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}

I change

\begin{longtable}{|c|c|c|c|} 

to

\begin{longtable}{|c|c|c|p{6cm}|}

The fourth column can swith line, but the lines of columns have problem, see below enter image description here How to produce the table correctly?

5
  • 2
    This is exacty the same as tabular and array and any other latex alignment {|c|c|c|c|} specifies one-line cells if you need fixed width cells with line breaking use p{3cm} instead of c Jul 31, 2014 at 13:29
  • If I change the 4th c to p{6cm} in your example I do not see the image you get: the table all lines up (but it is 39.68738pt too wide as reported in the log) Jul 31, 2014 at 13:33
  • When you change something in a longtable you should compile at least twice so that the widths can be recalculated correctly. Jul 31, 2014 at 13:38
  • After i consecutively run it several times, it shows okei after the second time, all lines up. I just remember someone said for longtable package, "table will not line up correctly until this document has been run through LATEX several times." Is it true? This is terrible! Users must check the result and wait several times to get the correct result when they print PDF??
    – postit
    Jul 31, 2014 at 13:44
  • Yes. And users must also wait several times to get correct references, tableofcontents, citations, tikz overlays and other things. Get used to it. A correct document needs a number of compilation until everything is in place. Jul 31, 2014 at 13:56

1 Answer 1

12

You need p{5cm} the same as in tabular to specify a cell that works like \parbox. 6cm and the default inter-column space made a table wider than the page, so I reduced both. Also don't put longtable in a center environment.

If you edit a longtable (including changing c to p) it can take a few runs for all parts of the table to line up, which may account for your image.

enter image description here

\documentclass[12pt]{article}
\usepackage{longtable}
\begin{document}

\setlength\tabcolsep{4pt}
\begin{longtable}{|c|c|c|p{5cm}|}
\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 & 4dddddddddddd asdfdasfasdf asödflkjasdf asdfasdfasdf \\ 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{document}
4
  • postit obviously didn't compile twice and so the wrong width from the previous run got him/her wrong lines. Jul 31, 2014 at 13:39
  • @UlrikeFischer yes I wrote that originally then I persuaded myself that it wouldn't have looked quite like that but I think you're right, I'll edit. Jul 31, 2014 at 13:40
  • Yes, my problem is i didnot compile it several times. If all users must compile twice to get a PDF file, i doubt if our customer still want to buy our software... No better solutions?
    – postit
    Jul 31, 2014 at 13:54
  • 1
    @postit latex almost always needs multiple runs to resolve cross references etc, if your software is calling it as a black box to generate pdf just make it run latex again if the log file says rerun latex Jul 31, 2014 at 13:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .