[Tex/LaTex] How to add a forced line break inside a table cell

line-breakingtables

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

Best Answer

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 inside these cells. The horizontal and vertical alignments can chosen independently from those of the table they're included in. The default is cc, but you can change it globally in the preamble with

\renewcommand{\cellalign}{vh}
\renewcommand{\theadalign}{vh}

where v is one of t,c,b and h one of l,c,r. Alternatively, for a single cell, you can use the \makecell or \thead commands with the optional argument [vh].

So here is a demo:

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

\renewcommand\theadalign{bc}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadgape{\Gape[4pt]}
\renewcommand\cellgape{\Gape[4pt]}

\begin{document}

  \begin{center}
    \begin{tabular}{ | c | c | c |}
      \hline
      \thead{A Head} & \thead{A Second \\ Head} & \thead{A Third \\ Head} \\
      \hline
      Some text &  \makecell{Some really \\ longer text}  & Text text text  \\
      \hline
    \end{tabular}
  \end{center}

\end{document} 

Compiled MWE