88

Possible Duplicate:
How to add a forced line break inside a table cell

In a table I would like to have a line break in the text inside a cell. Is there an easy way to do so, or do I have to create a new line without borders? The same holds true for other situations such as breaking a line in a caption for instance.

damusnet
  • 1,317
  • 2
  • 12
  • 11

5 Answers5

72

Would a \parbox work?

\begin{tabular}{ll}
one line& \parbox[t]{5cm}{another\\column}\\
second line here& and here
\end{tabular}
David Carlisle
  • 757,742
Yossi Farjoun
  • 13,274
  • 10
  • 74
  • 96
  • 2
    Is it possible to make parbox tight and omitting the width parameter somehow? And how to make it centered? – nimcap Dec 20 '11 at 15:31
  • @nimcap: better to start a new question than put it here...What do you mean by tight? a table is flexible and its width depends on the width of its contents? center by centering the column of the table (using c) and centering the parbox, using \begin{center}.... – Yossi Farjoun Dec 22 '11 at 02:23
  • 1
    @YossiFarjoun: I started a new question here: http://tex.stackexchange.com/questions/38924/newline-in-a-table-cell-which-is-centered – nimcap Dec 22 '11 at 08:44
  • 1
    @nimcap: and arn't you glad you did? ;-) – Yossi Farjoun Dec 22 '11 at 17:27
51

\newline works in paragraph columns

Example:

\begin{tabular}{|p{3cm}|p{3cm}|}
first line
\newline second line & still on first line \\
\end{tabular}
David Carlisle
  • 757,742
Tomasz
  • 641
15

Is this for a single cell, or an entire column of cells? If the later, try:

\documentclass{article}
\begin{document}
    \begin{tabular}{lp{.2\textwidth}l}
        42 & A paragraph of text & 42\\
        42 & Another paragraph & 42
    \end{tabular}
\end{document}

Even if it is for a single cell, I suspect that this is still the way to go.

David Carlisle
  • 757,742
vanden
  • 30,891
  • 23
  • 67
  • 87
5
\usepackage{pbox}

\begin{tabular}{|l|l|} \hline
    \pbox{20cm}{This is the first \\ cell} & second \\ \hline
    3rd & and the last cell \\ \hline
\end{tabular}
5

Regarding line breaks in captions: if you use the caption package \\ works. In the optional argument you should protect it. Here's a demonstration example:

\documentclass{article}
\usepackage[font=small,labelfont=bf]{caption}
\begin{document}
\listoffigures
\bigskip
\begin{figure}[hb]
\centering Test
\caption[Short\protect\\text]{Test\\example}
\end{figure}
\end{document}

Because the caption package provides a lot of features regarding justification and customization I would not work without it. But if you really wish to avoid this package, it can also be done using \parbox. The optional positioning parameter t might be useful:

\documentclass{article}
\begin{document}
\listoffigures
\bigskip
\begin{figure}[hb]
\centering Test
\caption[Short\protect\\text]{\parbox[t]{0.2\textwidth}{Just a Test\\example}}
\end{figure}
\end{document}
Stefan Kottwitz
  • 231,401
  • 1
    Thanks for your fine code! I had an awful error in the subfigure environment, but your idea of using \protect\\ instead of \\ has fixed it. (I had given \\ as a macro parameter and it did not work). – loved.by.Jesus Oct 15 '15 at 14:19