Table with multiple lines in some cells
You could nest a tabular
within another tabular
:
\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 t
op or b
ottom 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
.
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:
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.