tags:

views:

10877

answers:

3

Hi

I am creating a report in LaTeX which invole a few tables. I'm stuck on that as my cell data in the table is exceeding the width of the page. Can I some how wrap the text so that it falls in to the next line in the same cell of the table?

Is it some how related to the table's width but as its over shooting the page's width it won't make a difference, isn't it?

cheers

+14  A: 

Use p{width} for your column specifiers instead of l/r/c.

\begin{tabular}{|p{1cm}|p{3cm}|}
  This text will be wrapped & Some more text \\
\end{tabular}
marcog
+13  A: 

With the regular tabular environment, you want to use the p{width} column type, as marcog indicates. But that forces you to give explicit widths.

Another solution is the tabularx environment:

\usepackage{tabularx}
...
\begin{tabularx}{\linewidth}{ r X }
    right-aligned foo & long long line of blah blah that will wrap when the table fills the column width\\
\end{tabularx}


All X columns get the same width. You can influence this by setting \hsize in the format declaration:

>{\setlength\hsize{.5\hsize}} X >{\setlength\hsize{1.5\hsize}} X

but then all the factors have to sum up to 1, I suppose (I took this from the LaTeX companion). There is also the package tabulary which will adjust column widths to balance row heights. For the details, you can get the documentation for each package with texdoc tabulary (in TeXlive).

Damien Pollet
Interesting, that looks really useful. How intelligent is it when it comes to selecting column widths? For example, if you have two columns that need to be wrapped but one with much longer text than the other, does it still asign them equal width?
marcog
I edited my answer. But actually in practice I try simplify my tables so that I only need X for a single column. I just discovered tabulary :)
Damien Pollet
+1  A: 

Another option is to insert a minipage in each cell where text wrapping is desired, e.g.:

\begin{table}[H]
\begin{tabular}{l}
\begin{minipage}[t]{0.8\columnwidth}%
a very long line a very long line a very long line a very long line
a very long line a very long line a very long line a very long line
a very long line a very long line a very long line %
\end{minipage}\tabularnewline
\end{tabular}
\end{table}
Neil Rubens