625

I am creating a report using LaTeX, however, the cell data in the table is exceeding the width of the page.

How can I wrap the text so that it falls into the next line within the same cell of the table?

Is the solution somehow related to specifying the table's width? As the table is exceeding the page's width, would specifying the table's width make a difference?

Kyle F Hartzenberg
  • 2,567
  • 3
  • 6
  • 24
Arnkrishn
  • 29,828
  • 40
  • 114
  • 128
  • I was looking for a way to wrap long word, the other solution better suits my needs tex.stackexchange.com/questions/198325/wrap-word-in-table-cell – Mzq Feb 11 '19 at 06:54

9 Answers9

755

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

Thamme Gowda
  • 11,249
  • 5
  • 50
  • 57
moinudin
  • 134,091
  • 45
  • 190
  • 216
  • 49
    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}}` – Andrejas Jan 05 '12 at 09:34
  • 110
    Is there any way to do this while still specifying alignment in each cell? – Dylan Knowles Apr 30 '13 at 18:40
  • 11
    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|} – Veridian Aug 09 '13 at 03:03
  • 16
    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{}}` – Sander Jun 23 '14 at 13:45
  • 163
    Great solution. However, I'd recommend using relative values instead of arbitrary dimension, e.g. `p{0.2\linewidth}p{0.6\linewidth}}` – jgyou Nov 04 '14 at 20:35
  • 7
    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 – LondonRob Jul 29 '15 at 16:38
  • 1
    @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? – Nanashi No Gombe Sep 25 '18 at 13:11
  • 4
    What does p stand for? – ashman Nov 02 '20 at 18:06
  • 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. – user3582228 Jan 23 '21 at 09:00
  • I am getting error "! Missing number, treated as zero. } l.197 \end{tabular} }" – Tasneem Mar 11 '21 at 07:59
  • Good solution. But it is not working in case of multirow{}. What would be the solution then? – PS Nayak Jun 02 '21 at 05:56
  • 2
    @ashman **p** is for _paragraph_ :) – MattAllegro Jun 02 '22 at 14:39
165

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
  • 6,488
  • 3
  • 27
  • 28
  • 5
    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? – moinudin Apr 26 '09 at 14:49
  • 4
    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 Apr 26 '09 at 15:13
63

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
  • 731
  • 6
  • 4
  • 9
    Thanks, this allowed me to place `itemize` lists in my cells. – Quentin Pradet Dec 04 '11 at 09:19
  • 1
    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. – U. Windl Nov 20 '19 at 00:22
52

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 .

ozi
  • 1,651
  • 2
  • 17
  • 17
46

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

Shayan Amani
  • 5,787
  • 1
  • 39
  • 40
7

If you want to wrap your text but maintain alignment then you can wrap that cell in a minipage or varwidth environment (varwidth comes from the varwidth package). Varwidth will be "as wide as it's contents but no wider than X". You can create a custom column type which acts like "p{xx}" but shrinks to fit by using

\newcolumntype{M}[1]{>{\begin{varwidth}[t]{#1}}l<{\end{varwidth}}}

which may require the array package. Then when you use something like \begin{tabular}{llM{2in}} the first two columns we be normal left-aligned and the third column will be normal left aligned but if it gets wider than 2in then the text will be wrapped.

cheshirekow
  • 4,797
  • 6
  • 43
  • 47
7

To change the text AB into A \r B in a table cell, put this into the cell position: \makecell{A \\ B}.

Before doing that, you also need to include package makecell.

FlowerBeach
  • 71
  • 1
  • 3
3

The new tabularray makes wrapping text in cells easier then ever before.

The package supports all the traditional used column names like c, l, r, etc., but also has its own Q column which accepts various keys to control the width and vertical and horizontal alignment. It also provides an X column, as known from tabularx` which will automatically calculate the width of the column to fit the table into the available text width.

Another nice feature is that all the settings can also be done for individual cells.

\documentclass{article}
\usepackage{tabularray}

\begin{document}

\begin{table}
    \begin{tblr}{|c|Q[2cm,valign=m]|X[j,valign=m]|}
        \hline
        Title 1 & Title 2 & Title 3 \\
        \hline 
        one-liner & multi-line text & multi-line piece of text to show case a multi-line and justified cell   \\
        \hline
        apple & orange & banana \\
        \hline
        \SetCell{h,2cm} wrapping text only in a single cell & orange & banana \\
        \hline
    \end{tblr}
\end{table}
\end{document}

enter image description here

(thanks to Shayan Amani for providing a MWE in their answer!)

-17
\begin{table}
 \caption{ Example of force text wrap}
 \begin{center}
  \begin{tabular}{|c|c|}
   \hline
   cell 1       &   cell 2 \\   \hline
   cell 3                &       cell 4 & & very big line that needs to be wrap. \\ \hline
   cell 5       &   cell 6 \\   \hline
  \end{tabular}
  \label{table:example}
 \end{center}
\end{table}
Md Monjur Ul Hasan
  • 1,705
  • 1
  • 13
  • 36