235

What is the easiest way to have 2 lines in some of the cells in a table?

The only way I can think right now is to actually have 2 separate rows (without the line in the middle) and use \multirow on all other cells in this row. Any easier ideas?

2
  • 1
    Can you clarify your question? Maybe post a small complete document with a table that shows what you want. If you use the p column type, you can have paragraphs within a table cell. Will that solve the problem?
    – Alan Munn
    Commented Jan 9, 2012 at 20:29
  • here is an excellent solution: texblog.org/2012/12/21/… Commented May 5, 2013 at 18:13

10 Answers 10

222

You could nest a tabular within another tabular:

enter image description here

\documentclass{article}
\begin{document}
\begin{tabular}{cccc}
  One & Two & Three & Four \\
  Een & Twee & Drie & Vier \\
  One & Two & 
    \begin{tabular}{@{}c@{}}Three \\ Drie\end{tabular}
  & Four
\end{tabular}
\end{document}

The use of @{}..@{} voids the additional space (horizontal tab separation) inserted by the nested tabular.

Also, the above example inserts the nested tabular vertically centered with respect to the row. If you want it top or bottom aligned, use the optional parameter to tabular: \begin{tabular}[t].. or \begin{tabular}[b]....

Note that this approach also works within math mode for an array.

5
  • 14
    The result is really beautiful.
    – Wok
    Commented May 21, 2015 at 12:52
  • How to center the content inside this tabular? Commented Aug 10, 2018 at 21:12
  • 2
    @PaulRBerg: I'm not sure I understand your question. All the content in the tabular is centred based on the column specification.
    – Werner
    Commented Aug 12, 2018 at 15:48
  • Oh right! Sorry, I had my own implementation and I missed the c in your answer. It's working now, thanks. Commented Aug 12, 2018 at 16:15
  • This answer actually cope quite well with whole table scaling (using \tiny, for example).
    – Olivier
    Commented Nov 10, 2023 at 6:10
173

When using a p-type column, one can set the width of a column:

By default, if the text in a column is too wide for the page, LaTeX won’t automatically wrap it. Using p{'width'} you can define a special type of column which will wrap-around the text as in a normal paragraph. You can pass the width using any unit supported by LaTeX, such as 'pt' and 'cm'...

The p column does not only allow text to be automatically broken in multiple lines depending on the size of the column as given, it also allows for the use of \newline in the tabular environment:

\begin{tabular}{l|p{15mm}}
  \hline
  foo & bar \newline rlz \\
  \hline
\end{tabular}

Which gives:
example of p-type column

7
  • Welcome to TeX.sx! Commented Mar 18, 2013 at 1:15
  • 24
    It would be good to emphasise this requires a p type column Commented Mar 18, 2013 at 7:30
  • 7
    \newline works without any issues in tabularx.
    – Rob W
    Commented Sep 29, 2013 at 17:18
  • I thought I could use p{\textwidth} to get automatic column width adjustment. Alas does not work. It then uses a column width of the text without the \newline. Commented Apr 7, 2019 at 12:57
  • 2
    I prefer Wolfone's answer. Using makecell allows to control the line break and vertically centers text in other columns. Commented Feb 11, 2020 at 15:49
96

The easiest way is to use \shortstack but it is not very flexible.

\documentclass{article}

\begin{document} 
\begin{tabular}{ccc}
    one & two & three \\
    one & two & \shortstack{aa \\ bb}\\

\end{tabular}
\end{document}

\shortstack takes an option to align content left [l], right [r], or center [c](default). Another idea is to use \parbox[t]{5cm}{aa\\bb} because it provides options to align the lines vertically.

4
  • 4
    I prefer Wolfone's answer. Using makecell vertically centers text in other columns. Commented Feb 11, 2020 at 15:47
  • (Just to complete Nagabhushan's comment), makecell gives you options to decide vertical and horizontal alignment. Commented Aug 24, 2020 at 6:50
  • This is probably the one you're after if you have a little bit of extra text that you want to wrap, rather than have it stretch out the column width
    – Bamboo
    Commented Apr 7, 2021 at 8:35
  • I believe @NagabhushanSN is correct. Otherwise, the rest of the columns in the table will align with the last line in your multirow column which doesn't quite look right in my opinion
    – Mjoseph
    Commented Feb 23 at 18:54
62

Another possible solution — my preferred one in my use-cases so far as I wanted full control to what comes in which line while being as concise as possible — is using the makecell package, which would make it possible to have multi-line cells via:

\usepackage{makecell}

...

\begin{tabular}{lll}
    & some & information & more &\\
    & info & \makecell{ line1 \\ line2 }  &  blubb &\\
\end{tabular}
3
  • 9
    Make sure to include \usepackage{makecell} to make this work.
    – ASGM
    Commented Jul 27, 2020 at 17:53
  • 1
    For alignment of text inside makecell, use \renewcommand\cellalign{cc}. The first argument(c here) is for horizontal alignment and the second argument is for vertical alignment. Commented Aug 24, 2020 at 6:47
  • 1
    You may want to use this instead to left align the content of makecell \makecell[l]{content}
    – Huhngut
    Commented Mar 5, 2023 at 8:38
9

here are some cell definitions that i've used to good effect in situations where the content of table cells was essentially text:

\newcommand{\lcell}[2][1.2in]{%
  $\vcenter{\hsize#1\baselineskip11pt\vspace*{3pt}\raggedright#2\strut\par}$}
\newcommand{\slcell}[2][1.2in]{%
  $\vcenter{\hsize#1\baselineskip9.5pt\vspace*{3pt}\raggedright#2\strut\par}$}
\newcommand{\ccell}[2][.42in]{%
  $\vcenter{\hsize#1\baselineskip11pt\vspace*{3pt}\centering#2\strut\par}$}

the job(s) involved had \usepackage{array} to get the augmented facilities.

of course, the dimensions were specific to the job, and would need to be changed depending on the circumstances; and fine tuning was definitely needed in the actual jobs involved. type was assumed to be 10pt for \lcell and \ccell or 8pt for \slcell; i also \setlength{\extrarowheight}{1pt} to keep the tops of cell content from crashing into lines above, and the \strut assures consistent clearance below.

line breaks in cells were usually manual (though they needn't be), with \break, and if a continuation line should be indented in a left-aligned cell, an \hspace* would be needed. the \par at the end ensures that the specified baseline is observed.

to me, multi-line text content of cells looks much better with "normal" text baseline settings than it does with the usual table row separation.

for table headings, vertical centering of multiple lines doesn't look so good; they look better aligned at the bottom. here's the definition i used for that:

\newcommand{\thead}[2][.75in]{%
  \vbox{\hsize#1\baselineskip11pt\centering\vspace*{3pt}#2\par}}

some of these headings ran to four or more lines (complicated headings above narrow columns of numbers). the results were actually quite respectable.

7

You can also put minipages in your cells. Its especially interesting if you have a whole text in a cell and you want it to make linebreaks by its own.

e.g.

\begin{tabular}{|l|l|} \hline
\begin{minipage}{5mm} ~\\ foo \\ bar \\ \end{minipage} & foo \\ \hline
foo & bar \\ \hline
\end{tabular}
2
5

Why Not partition your text into two rows just donot put the hline between the rows, something like this:

\begin{table}[!h]
\centering
\begin{tabular}{|c|c|c|}
\hline
A & B &C\\
D & E &F\\
\hline
G & H & I\\
\hline
\end{tabular} 
\end{table}
2
  • 2
    It's more work than a \shortstack for example. In general it will result in a lot of &. Commented Sep 11, 2014 at 13:23
  • and it is not flexible. What if you want to change the order of rows or columns after creating the table? Commented Aug 24, 2020 at 6:48
5

It’s easy peasy with tblr environment of the new LaTeX3 package tabularray:

\documentclass{article}
\usepackage{tabularray}
\begin{document}
\begin{tblr}{colspec={clr},hlines}
    one & {left\\leftttt} & three          \\
    one & two             & {right\\rightttt} \\
\end{tblr}
\end{document}

enter image description here

1

The array function works like a charm. I have extra functions in here that are not necessary for answering the above question such as the \resizebox - needed to fit all my words on the pdf page, \begin{figure} - I want the table centered with a caption and I know putting the table in a figure will do this, \bullet just makes a bullet for a list, \textrm just makes text non-italic while in math mode (i.e. while in between $ $). copy and paste in texmaker, it works.

\begin{figure}[h]
\centering

\resizebox{\textwidth}{!}{%

\begin{tabular}{|c|c|c|}
\hline
Methods & Pros & Cons\\
\hline
Anharmonic DC SQUID & $\begin{array}{l}
\bullet \textrm{ Fast readout} \\ 
\bullet \textrm{ Accessible equipment}
\end{array}$ & 
$\begin{array}{l}
\bullet \textrm{ Large currents} \\ 
\bullet \textrm{ Requires knowledge of other junction}\\
\bullet \textrm{ Not decoupled from apparatus}
\end{array}$\\
\hline
\end{tabular}
}
\end{figure}
1
  • 1
    Welcome to TeX.SX! A nested tabular with \textbullet would also work. Commented Apr 1, 2017 at 3:52
1

If you don't have a preference as to exactly what text will go on the second line or the third line, this is the best option:

\begin{tabular}{p{2cm} p{2cm}p{2cm}p{2cm}p{3cm}p{3cm}}

\end{tabular}

Only reason I am answering this is because I faced a similar problem (where I didn't have a preference) and it worked wonders.

1
  • Welcome to TeX.SE! Please explain better why this will solve the given issue ...
    – Mensch
    Commented Sep 4, 2022 at 15:34

Not the answer you're looking for? Browse other questions tagged .