# How to add a forced line break inside a table cell

I have some text in a table and I want to add a forced line break. I want to insert a forced line break without having to specify the column width, i.e. something like the following:

\begin{tabular}{|c|c|c|}
\hline
Foo bar & Foo <forced line break here> bar & Foo bar \\
\hline
\end{tabular}


I know that \\ inserts a line break in most cases, but here it starts a new table row instead.

A similar question was asked before: How to break a line in a table

-

It's a quite old question, but I'll add my answer anyway, as the method I suggest didn't appear in the others

\begin{tabular}{|c|c|c|}
\hline
Foo bar & \begin{tabular}[x]{@{}c@{}}Foo\\bar\end{tabular} & Foo bar \\
\hline
\end{tabular}


where x is either t, c, or b to force the desired vertical alignment.

In case this is needed in more than a couple of places, it's better to define a command

\newcommand{\specialcell}[2][c]{%
\begin{tabular}[#1]{@{}c@{}}#2\end{tabular}}


so the table line before can be one of

Foo bar & \specialcell{Foo\\bar} & Foo bar \\    % vertically centered
Foo bar & \specialcell[t]{Foo\\bar} & Foo bar \\ % aligned with top rule
Foo bar & \specialcell[b]{Foo\\bar} & Foo bar \\ % aligned with bottom rule


More variations are possible, for instance specifying also the horizontal alignment in the special cell.

Notice the @{} to suppress added space before and after the cell text.

-
For those wanting to control the horizontal alignment, change c@ to l@ or r@ (or make it another parameter like the vertical alignment?). Thanks egreg –  Dolan Antenucci Apr 19 '12 at 13:31
@dolan I'd go with another parameter: #1 vertical alignment (optional), #2 horizontal alignment, #3 text. –  egreg Apr 19 '12 at 14:46
\parbox will do the same, but you have set a width: \parbox[t]{5cm}{foo\\bar} –  Born2Smile Dec 8 '12 at 14:11
@Born2Smile There's the varwidth environment (package of the same name), but it's overkill. –  egreg Dec 8 '12 at 14:15
This wins the prize for the most incomprehensible answer. –  Ricky Robinson Jan 13 at 13:00

It really is no wonder why LaTeX is said to be complicated! Just look at your answers to such an easy question! How about an easy solution to an every day problem?

\usepackage{pbox}

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


which looks like:

Note that the width supplied to \pbox is a maximum width. If the content is shorter the length of the longest line is taken.

-
Once again the simplest solution is the best. Thanks! –  Connor Doyle Mar 15 '12 at 20:39
+1 Nice and easy...love it! –  WAF Aug 26 '13 at 10:40
This may be an obvious question. What is the 20cm for, and is there a way to remove it? (automatically choose the correct value) –  Annan Dec 2 '13 at 4:38
The \pbox solution is great. Alignment might be a bit tricky. If you run into trouble using this solution, see here tex.stackexchange.com/a/55861/13450 –  Christian Apr 8 at 17:23

You can switch your cell layout to paragraph to use the \newline command.

\begin{tabular}{|p{2cm}|p{2cm}|}
\hline
Test & foo \newline bar \\
...

-
I did. I tried both \newline and \linebreak, but they only work in paragraph mode. cims.nyu.edu/cgi-comment/info2html?(latex)%5Cnewline –  Denilson Sá Aug 28 '10 at 0:56
Hmm. I thought it did. I could have sworn I've done this before. The only thing I can think of right now is to put a single column tabular within the other tabular. E.g., Foo bar & \begin{tabular}{c} Foo \\ bar \end{tabular} & foo bar \\ –  frabjous Aug 28 '10 at 2:59
@frabjous: Could you clarify the status of this answer? Does it work, and if so, how? (A complete MWE would be good.) –  doncherry Jun 9 '12 at 15:06
@doncherry: This answer works, iff your column type is p{width} or X (in a tabularx environment). It does not work with c, l or r columns, as requested by the asker, though. –  Fritz Aug 28 '12 at 13:11
+1 This solution (unlike some others I've tried) also works well with \rowcolors –  Dana Mar 12 at 17:26

Use the tabularx environment instead of tabular, and then use \newline where you want line breaks within a cell.

\usepackage{tabularx}
\begin{tabularx}{\textwidth}{lX}
Section:   &  This is my     \newline
long paragraph \\
\end{tabularx}


The tabularx environment has a special column type, X, in addition to the usual ones, and its first argument is the desired width of the table. The X column will have the necessary width in order to make the whole table the desired width.

Details on tabularx can be found here.

-
Nice one, I hadn't realised tabularx supported this, thanks for mentioning it. –  sharky Feb 23 '12 at 4:02
By far the easiest solution! –  mtsz Oct 9 '12 at 21:33
\begin{tabular}{lll}
a&\vbox{\hbox{\strut ASDF}\hbox{\strut ASDF}\hbox{\strut This is my
really long line}}&c
\end{tabular}


The \strut is essential for spacing.

-
This looks like good method. Maybe too much to ask, but is there any easy way to adjust vertical justification in the cells? E.g., so single line cells are even with top of multi-line cell? Or so all are vertically centered? –  Herbert Sitz Aug 28 '10 at 15:33
Replace \vbox with \vtop to get alignment at the top. –  TH. Aug 29 '10 at 0:49
Great answer. How do you center horizontally? –  denilw Oct 21 '10 at 20:47
@denilw: Oops. I misread and wrote a previous comment (now deleted). If you want to center the lines, the easiest way is to compute the width of the widest \hbox and use that as the width of the other \hboxes. Something like \vbox{\setbox0\hbox{\strut This is the widest one.}\hbox to\wd0{\hss\strut ASDF\hss}\copy0\hbox to\wd0{\hss\strut asdf\hss}} –  TH. Oct 21 '10 at 21:05
@TH, works well thanks –  denilw Oct 21 '10 at 21:20

Here's a very simple way to do it, using Plain TeX commands within the tabular environment:

\begin{tabular}{|c|c|c|}
\hline
here&\vtop{\hbox{\strut top line}\hbox{\strut botline}}&more\\
\hline
x&y&z\\
\hline
\end{tabular}


By using hboxes within the vtop we've stayed in vertical mode and therefore the width of the text in the hboxes determines the width of the vtop. This way we don't need to know the width of the text in advance. \strut will maintain the right space above and below the text in the hbox.

-
Hi Amy, and welcome to TeX.sx. You might want to read Welcome to TeX.SX! and note that signing your posts is not necessary since your name appears at the bottom automatically. See also Rules about linking to external pages. With respect to this particular answer, how is it different from TH's answer? –  Alan Munn Sep 10 '13 at 20:08
Thank you! This works when you are in a multicell but they are left aligned (even through I defined it to be in the centre). Is there a way around this? –  user2822693 May 14 at 16:41

You can also just fake it:

\begin{tabular}{|c|c|c|}
\hline
Foo bar & Foo & Foo bar \\
~       & bar &  ~      \\
\hline
\end{tabular}

-

How about using \parbox in a custom command

\documentclass{article}
\newsavebox\mybox
\newlength\mylength
\newcommand\boxup[2]{%
\savebox\mybox{#1}%
\setlength\mylength{\wd\mybox}%
\parbox{\mylength}{#1 \\ #2}%
}
\begin{document}
\begin{tabular}{|c|c|c|}
Foo bar & \boxup{Foo}{bar} & Foo bar \\
\end{tabular}
\end{document}


This takes two arguments with the assumption that the first line is longer than the second. It would be possible to refine the code to work through an arbitrary number of lines and find the longest. If that's of interest I'll write something, probably using expl3 for the looping.

-
Gives me issues with too little vertical spacing. \vspace only helps for the bottom margin. –  Aleksandr Levchuk Oct 23 '11 at 22:09

This is a really old question, but since this was linked from a recent question on separating content and presentation, I'll add a ConTeXt answer for comparision.

In ConTeXt, \crlf adds a forced line-break, so achieving a forced line-break in a table is as simple as just adding \crlf in the appropriate place. Here is the complete example:

% Setup for rules at the top and bottom
\startsetups table:rules
\setupTABLE[frame=off]
\setupTABLE[row][first][topframe=on]
\setupTABLE[row][last][bottomframe=on]
\stopsetups

% Setups for middle alignment
\startsetups table:middle
\setupTABLE[align=middle]
\stopsetups

\starttext

\startTABLE[setups={table:rules, table:middle}]
\NC Foo bar \NC Foo \crlf Bar \NC Foo bar \NC \NR
\NC Foo bar \NC Foo \crlf Bar \NC Foo bar \NC \NR
\stopTABLE

\stoptext


which gives:

-
your source code does not match what the illustration shows. –  Kurt Pfeifle Nov 17 '13 at 13:28
@KurtPfeifle: It does for me! Which version of context are you using? –  Aditya Nov 17 '13 at 20:34
Sorry, didn't see your question earlier. My context is v. 0.60 (2013.04.20 01:15). However, now re-testing your code, I can confirm that your code works. I have to withdraw my previous statement. Can't remember now what caused me to make it. –  Kurt Pfeifle Feb 11 at 19:30

I believe I have the simplest answer here:

If you are using a paragraph column in a table, you can put text in an alignment environment and the table does not pick up the \\ as a new table row, so you can use it normally. For example:

\begin{tabular}{p{2in}p{2in}}
\begin{flushleft}
Some text over here \\
newline! \\
\bigskip all the way down
\end{flushleft}
&
\begin{flushleft}
Lorem ipsum dolor sit amet, consectetur
tempor incididunt ut labore et dolore
magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo\\ \bigskip
consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.\\
\bigskip \bigskip lorem ipsum\\
\end{flushleft}\\
\end{tabular}\\


Will give you a table with line breaks using the \\ command.

-
I think the explanation is wrong - I just tried {flushleft} in a l column, and it fails; I think the reason why this example works is because it has p{2in} column type, which sets the width in advance.. –  sdaau Jul 10 at 1:08

I think multirow is a simple elegant solution, at least for simple tables.

For example, I tried a few of the suggestions above but I found this worked the best:

\usepackage{multirow}
\begin{table}[ht]
\caption{RESTful Resources}
\centering
\begin{tabular}{l l l}
\hline
Resource & Methods & Description \\
\hline
\multirow{2}{*}{Resource1} & \multirow{2}{*}{POST, PUT}
& This resource contains ...\\
& & \emph{Media types: text/plain, application/json}\\
\multirow{2}{*}{Resource2} & \multirow{2}{*}{POST, PUT}
& This resource contains ...\\
& & \emph{Media types: text/plain, application/json} \\
\hline
\end{tabular}
\label{table:resources}
\end{table}

-

Strangely, no answer (unless I've misread them) mentions a package that is dedicated to this precise question: makecell, which allows for common formatting of certain cells, thanks to its \thead and \makecell commands, and for line breaks in those cells. The horizontal and vertical alignments can chosen independently from those of the table they're included in. So here is a demo:

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{fourier}
\usepackage{array}
\usepackage{makecell}

\renewcommand\cellgape{\Gape[4pt]}

\begin{document}

\begin{center}
\begin{tabular}{ | c | c | c |}
\hline
\hline
Some text &  \makecell{Some really \\ longer text}  & Text text text  \\
\hline
\end{tabular}
\end{center}

\end{document}


-

I don't have direct help on how to add a linebreak, but using the multirow package may provide different route to same desired end. You can read a tutorial here:

http://andrewjpage.com/index.php?/archives/43-Multirow-and-multicolumn-spanning-with-latex-tables.html

But I have a feeling the \parbox answer in the similar question you linked is what you're looking for. I think using a box in the cell is going to be simplest and best way, was there something about that that wasn't working for you?

If you want to use parbox without having to hardcode in a width, here's one way that works for me. Not perfect, since it requires you to know beforehand the text of the longest line in the multi-line column, but it does work:

% define new length and set to length of longest line
\newlength{\longline}
\settowidth{\longline}{longest line in table}\\

\begin{tabular}{lll}

one line & \parbox[t]{\longline}{longest line in table \\ and another line} & more text \\
second line here& and what & more text
\end{tabular}

-
\parbox works, but it requires me to type the desired column width. Thus, I lose the auto-column-size that happens otherwise. I bet \multirow will work, but the source-code will be a lot messy. –  Denilson Sá Aug 28 '10 at 1:39
Gotcha. Added code sample to my question that uses column autosizing with parbox and is probably cleaner than using multirow package. I think there must be a better way, but at least it works. . . –  Herbert Sitz Aug 28 '10 at 4:38

You can use minipage:

\begin{minipage}[t]{0.2\columnwidth}%
This is the first line
\newline
second line
\newline
...
\end{minipage}

-
You have to specify width in advance, though... –  sdaau Jul 10 at 1:06

This approach isn't so different from some of the others already given, except that it "externalises" all of the design decisions from the table definition itself (that is, leaving it cleaner to write and read each table should you want to include multiple tables like this throughout your doc or docs).

\usepackage{array} % needed if you're going to use \\ together with centering,
% raggedright, raggedleft in your column specifier (see
% manuals on \arraybackslash)
\newlength{\mywidestcolwidth}
\newcommand*\mycellformat{\protect\footnotesize}
\newcommand*\mytableparskip{\setlength\parskip{4pt}}
\newcommand*\setmywidestcolwidth[1]{
\settowidth{\mywidestcolwidth}{\mycellformat #1}
}


Usage:

\setmywidestcolwidth{foo baz bar}
\begin{tabular}{|>{\mycellformat}c
|>{\mytableparskip\mycellformat\centering\arraybackslash}p{\mywidestcolwidth}
|>{\mycellformat}c|}
\hline
foo bar baz&
foo

foo baz bar

baz&
foo

foo baz bar

baz\\ \hline
\end{tabular}


Skip the \mycellformat and \parskip if you have no need for them (although not necessary in your case, I included \parskip since it's useful to visually distinguish paragraphs from one another since paragraph indentation is automatically switched off in tables, and added \mycellformat to allow for uniform document-wide table design settings).

-

changed suggestion above to accomplish different problem with second line being longer...

\mybox\usepackage{ifthen}

\newsavebox{\mybox}%
\newsavebox{\myboxx}%
\newlength{\mylengthA}%
\newlength{\mylengthB}%
\newlength{\mylength}%
\newcommand{\boxup}[2]{%
\savebox{\mybox}{#1}%
\setlength\mylengthA{\wd\mybox}%
\savebox{\myboxx}{#2}%
\setlength\mylengthB{\wd\myboxx}%
\ifthenelse{\lengthtest{\mylengthA>\mylengthB}}
{\setlength{\mylength}{\mylengthA}}
{\setlength{\mylength}{\mylengthB}}
\parbox{\mylength}{\centering #1 \\ #2}%
}

-

Good afternoon, when I was younger I was typesetting a tabular environment inside a tabular environment. Since we have TikZ I use tikzpicture environment inside tabulars, or even tikzpicture inside tikzpicture. I prefer putting simple TikZ nodes next and below each other, but it depends on actual task. I enclose an MWE with fast text height correction.

\documentclass{article}
\usepackage{tikz}\pagestyle{empty}
\begin{document}
\tikzset{inner sep=0pt}
\begin{tabular}{|l|l|} \hline
\begin{tikzpicture}
\node[align=left, text height=4.5ex]{This is the\\[3pt]first cell};
\end{tikzpicture}
& second \\ \hline
3rd & and the last cell \\ \hline
\end{tabular}
\end{document}


-

## protected by toheczMar 17 at 11:24

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.