How to wrap text in LaTeX tables?

694,306

Solution 1

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}

EDIT: (based on the comments)

\begin{table}[ht]
    \centering
    \begin{tabular}{p{0.35\linewidth} | p{0.6\linewidth}}
      Column 1  & Column2 \\ \hline
      This text will be wrapped & Some more text \\
      Some text here & This text maybe wrapped here if its tooooo long \\
    \end{tabular}
    \caption{Caption}
    \label{tab:my_label}
\end{table}

we get:

enter image description here

Solution 2

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).

Solution 3

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}

Solution 4

I like the simplicity of tabulary package:

\usepackage{tabulary}
...
\begin{tabulary}{\linewidth}{LCL}
    \hline
    Short sentences      & \#  & Long sentences                                                 \\
    \hline
    This is short.       & 173 & This is much loooooooonger, because there are many more words.  \\
    This is not shorter. & 317 & This is still loooooooonger, because there are many more words. \\
    \hline
\end{tabulary} 

In the example, you arrange the whole width of the table with respect to \textwidth. E.g 0.4 of it. Then the rest is automatically done by the package.

Most of the example is taken from http://en.wikibooks.org/wiki/LaTeX/Tables .

Solution 5

Simple like a piece of CAKE!

You can define a new column type like (L in this case) while maintaining the current alignment (c, r or l):

\documentclass{article}
\usepackage{array}
\newcolumntype{L}{>{\centering\arraybackslash}m{3cm}}

\begin{document}

\begin{table}
    \begin{tabular}{|c|L|L|}
        \hline
        Title 1 & Title 2 & Title 3 \\
        \hline 
        one-liner & multi-line and centered & \multicolumn{1}{m{3cm}|}{multi-line piece of text to show case a multi-line and justified cell}   \\
        \hline
        apple & orange & banana \\
        \hline
        apple & orange & banana \\
        \hline
    \end{tabular}
\end{table}
\end{document}

enter image description here

Share:
694,306
Arnkrishn
Author by

Arnkrishn

Updated on January 30, 2022

Comments

  • Arnkrishn
    Arnkrishn over 2 years

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

    Is it somehow related to the table's width? But as it's overshooting the page's width, will it make a difference?

  • moinudin
    moinudin about 15 years
    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?
  • Damien Pollet
    Damien Pollet about 15 years
    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 :)
  • Quentin Pradet
    Quentin Pradet over 12 years
    Thanks, this allowed me to place itemize lists in my cells.
  • Andrejas
    Andrejas over 12 years
    Good solution, but lose the '|' if you don't want a border around the table. It would then become \begin{tabular}{p{1cm}p{3cm}}
  • Dylan Knowles
    Dylan Knowles about 11 years
    Is there any way to do this while still specifying alignment in each cell?
  • Veridian
    Veridian almost 11 years
    I found a way to specify alignment in each cell! Create a macro! \newcolumntype{L}{>{\centering\arraybackslash}m{.8cm}} \begin{table*}[t] %\small \caption{Comparison} \centering %\begin{tabular}{|L|L|L|L|L|L|L|L|L|L|L|L|L|}
  • Sander
    Sander about 10 years
    What if I have two columns where the first should be as long as needed to fit it's contents and the other should fill to the line width, while wrapping? So basically, I need begin{tabular}{lp{<whatever is left to fill the line width>}}
  • jgyou
    jgyou over 9 years
    Great solution. However, I'd recommend using relative values instead of arbitrary dimension, e.g. p{0.2\linewidth}p{0.6\linewidth}}
  • LondonRob
    LondonRob almost 9 years
    Specifying the width manually is clearly a major flaw with this method (who really wants to specify the width, just because they want the text to wrap?) I'd suggest using tabularx as suggested by @DamienPollet
  • Nanashi No Gombe
    Nanashi No Gombe almost 6 years
    @Veridian I get error just by ditto copying and pasting your macro before the beginning of the document. I am sure you meant otherwise. Could you kindly elaborate on which part needs to be copied where?
  • U. Windl
    U. Windl over 4 years
    I think the answer should explain the meaning of \columnwidth: When I tried it, it seemed to be more table-width rather than column-width, so I had to set a manual proportion like 0.2\columnwidth to get a reasonable width.
  • starbeamrainbowlabs
    starbeamrainbowlabs over 4 years
    Downvote. This is a code-only answer, with no explanation at all.
  • Ahmed salah
    Ahmed salah over 4 years
    Furthermore, it is wrong anyway since the second row contains 4 cells where the table is designed to accommodate only two columns. And it is not related to wrapping cell content anyhow.
  • Jung
    Jung almost 4 years
    how do you make the text in the 'multi-line piece ...' center align
  • Shayan Amani
    Shayan Amani almost 4 years
    @Jung notice having L in the corresponding column \begin{tabular}{|c|L|L|}
  • ashman
    ashman over 3 years
    What does p stand for?
  • user3582228
    user3582228 over 3 years
    It doesn't seem to work if the table has multi-columns:( Am I right? In my case, the table has lengthy column headings but it doesn't wrap. Please guide me.
  • Tasneem
    Tasneem over 3 years
    I am getting error "! Missing number, treated as zero. <to be read again> } l.197 \end{tabular} }"
  • PS Nayak
    PS Nayak about 3 years
    Good solution. But it is not working in case of multirow{}. What would be the solution then?
  • Basilique
    Basilique over 2 years
    it is a really useful package, thank you!
  • Deadly Pointer
    Deadly Pointer about 2 years
    This makes the content centered by default, you can define alignment by \makecell[l]{A \\ B} (l = left, r=right)
  • MattAllegro
    MattAllegro about 2 years
    @ashman p is for paragraph :)