How to add a line break inside a table cell in LaTeX?

In this tutorial, you will learn how to add a line break inside a table cell in LaTeX.

Generally, we use thisĀ  \\ to add a line break in LaTeX. But with the tabular environment, you can’t do line breaks inside a cell.

In order to do this, you can use the tabularx environment provided by the tabularx package. This environment gives you a special type of column “X“, with this you can set a fixed width of the column. Like this

\begin{tabularx}{<column-width>}{X|X|X}

This process unlocks the auto line breaks inside a table cell.

\documentclass{article}
\usepackage{tabularx}

\begin{document}

  \begin{tabularx}{9cm}{|X|X|X|}
  \hline
  3cm & 3cm & 3cm \\
  \hline
  This table & each column got the same & width: 3cm  \\
  \hline
  \end{tabularx}
  
\end{document}

Output:

auto line break inside table cell.

Line break manually inside a table cell

To line break manually inside a table cell you have to use the makecell package. This package provides \thead{} and \makecell{} command, inside these commands you can use this \\ to add a line break inside a cell.

\documentclass{article}
\usepackage{makecell}

\begin{document}

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

Output:

line break inside a table cell.

 

Leave a Reply

Your email address will not be published. Required fields are marked *