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 \\
–
frabjousAug 28 '10 at 2:59

1

@frabjous: Could you clarify the status of this answer? Does it work, and if so, how? (A complete MWE would be good.)
–
doncherryJun 9 '12 at 15:06

12

@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.
–
FritzAug 28 '12 at 13:11

+1 This solution (unlike some others I've tried) also works well with \rowcolors
–
DanaMar 12 '14 at 17:26

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.

How does this work? I'm not getting the desired output.
–
CroCoMar 28 at 1:56

@CroCo, add the \usepackage... line in your main.tex and the rest of the code shown works perfectly. +1 for this answer, which does not only work for \newline but also allows many other commands and even environments \begin{...} ... \end{...}, which was all very forbidden in the normal tabular environment.
–
SteevenMay 1 at 14:35

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 SitzAug 28 '10 at 15:33

3

Replace \vbox with \vtop to get alignment at the top.
–
TH.Aug 29 '10 at 0:49

1

Great answer. How do you center horizontally?
–
denilwOct 21 '10 at 20:47

1

@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

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:

This is great, it works without specifying a width and does not need confusing hacks. Maybe worth to mention that the example here contains much more than is actually necessary to answer the question: just adding \usepackage{makecell} and using \makecell{...\\...} is sufficient to get a linebreak in the cell.
–
luatorMay 11 at 9:48

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 MunnSep 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?
–
user2822693May 14 '14 at 16:41

The nice thing about this answer is that \vtop{\hbox{\strut top line}\hbox{\strut botline}} can be used within Pandoc Markdown tables.
–
Serge StroobandtOct 21 '14 at 11:11

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.

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..
–
sdaauJul 10 '14 at 1:08

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 SitzAug 28 '10 at 4:38

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.

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).

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).