63

I am trying to write a short paper with LaTeX and need to add a table with 3 columns.

+-------------+-----------------+--------------------------------------+
| AAAAAAAAAA  | BBBBBBBBBBBBBBB | Betty Botter Bought a Bit of Butter  |
|             |                 | but the Butter's Bitter              |
+-------------+-----------------+--------------------------------------+
| CCCCCCCC    | DDDD            | Betty Botter Thought:                |
|             |                 | If I Put This Bitter Butter in My    |
|             |                 | Batter it Will Make My Batter Bitter |
+-------------+-----------------+--------------------------------------+

Unfortunately I can't seem to find the correct idiom to do it.


I tried:

\begin{tabular}{lll} 
    AAAAAAAAAA  & BBBBBBBBBBBBBBB & Betty Botter Bought a Bit of Butter but 
    the Butter's Bitter  \\
    CCCCCCCC  & DDDD & Betty Botter Thought: \newline If I Put This Bitter Butter in My Batter it Will Make My Batter Bitter
 \end{tabular}

But LaTeX doesn't do any linebreaks or formatting within the cell. I assume I need to tell it to do so.. But how?

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
lexu
  • 8,766
  • 5
  • 45
  • 63
  • 1
    Duplicate question: http://stackoverflow.com/questions/2687033/multiple-lines-in-a-cell-of-a-table (that question is a little more general in scope). – Charles Stewart May 24 '10 at 11:52

4 Answers4

87

Use the p column descriptor:

Change

\begin{tabular}{lll} 

to

\begin{tabular}{llp{5cm}}

To explicitly insert line-breaks:

CCCCCCCC  & DDDD & \parbox{5cm}{Betty Botter Thought: \\ If I Put This Bitter Butter in My Batter it Will Make My Batter Bitter}
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 6
    Would there be any way to combine p{} with one of the other column descriptors? Meaning, a way to say that the column that is p{5cm} should be left, right or center aligned? – jja Aug 12 '13 at 01:05
  • @jja as workaround for align-right inside p{}, i am using \hfill on the beginning of each line. – Frizi Oct 23 '13 at 18:13
  • 1
    To explicitly insert line-breaks ```\newline``` can be used. – Aryadegari Sep 20 '18 at 10:28
  • For anyone else asking about the centered version: https://tex.stackexchange.com/a/157400/108304 – andrew Jul 06 '21 at 22:28
10

This is the answer I found so far for my needs: Link here.

It creates a new command that will make a table inside a table in a more proper way:

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

So, if want to do a forced line break inside a cell like here:

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

You will end up using a code like this:

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

Horizontal alignment can be controlled in the declaration of the new command by changing c@ to l@ or r@

All credit goes to egreg from the Tex forum. Do upvote his answer !

3nrique0
  • 349
  • 6
  • 14
2

As @aioobe wrote in his answer, in this case one can switch from the left alignment

\begin{tabular}{lll}

to the paragraph alignment, at least in the third column where the custom linebreak must be inserted manually:

\begin{tabular}{llp{.5\textwidth}}

After this edit, one can use the command \par (instead of \newline) to implement the linebreak within the cell.

This code:

\documentclass{article}

\begin{document}

\begin{tabular}{llp{.5\textwidth}}
AAAAAAAAAA & BBBBBBBBBBBBBBB & Betty Botter Bought a Bit of Butter \par but the Butter's Bitter\\
CCCCCCCC & DDDD & Betty Botter Thought: \par If I Put This Bitter Butter in My \par Batter it Will Make My Batter Bitter\\
\end{tabular}

\end{document}

produces the output requested:

screenshot of output

MattAllegro
  • 6,455
  • 5
  • 45
  • 52
-1

Here is an answer with no fancy coding. Write your rows in separate lines. Omit the \hline for all but the last row(line) Its quick and dirty but, hey, it works and gives me exactly what I want, for simple tables anyway. I was making advertising to go on automobile windscreens. I have 3 centered rows in each cell

iTutor Grahamstown
Mathematics Tutor
0793296211

I wanted this repetitively in my table. I just left out the \hline for the first two rows. The multiple \hlines and '|' are to make cutting up the printout easier.

\begin{tabular}{||c||c||c||c||}
\hline\hline

iTutor Grahamstown &iTutor Grahamstown&iTutor Grahamstown &iTutor Grahamstown \\ %No \hline

Mathematics Tutor & Mathematics Tutor & Mathematics Tutor&Mathematics Tutor \\  %No \hline

0793296211 & 0793296211 & 0793296211 & 0793296211\\ \hline\hline\hline %\hline now


\end{tabular}  

I hope that this helps.

m00am
  • 5,910
  • 11
  • 53
  • 69