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?
asked Jan 9, 2012 at 20:14 9,575 12 12 gold badges 36 36 silver badges 35 35 bronze badgesCan 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?
Commented Jan 9, 2012 at 20:29 here is an excellent solution: texblog.org/2012/12/21/… Commented May 5, 2013 at 18:13You could nest a tabular within another tabular :
\documentclass \begin \begin One & Two & Three & Four \\ Een & Twee & Drie & Vier \\ One & Two & \beginc@<>>Three \\ Drie\end & Four \end \end
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[t].. or \begin[b]. .
Note that this approach also works within math mode for an array .
answered Jan 9, 2012 at 20:22 610k 141 141 gold badges 1.5k 1.5k silver badges 2.3k 2.3k bronze badges The result is really beautiful. Commented May 21, 2015 at 12:52 How to center the content inside this tabular? Commented Aug 10, 2018 at 21:12@PaulRBerg: I'm not sure I understand your question. All the content in the tabular is c entred based on the column specification.
Commented Aug 12, 2018 at 15:48Oh 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). Commented Nov 10, 2023 at 6:10When 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 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> \hline foo & bar \newline rlz \\ \hline \end
Which gives:
I thought I could use p to get automatic column width adjustment. Alas does not work. It then uses a column width of the text without the \newline .\textwidth>
Commented Apr 7, 2019 at 12:57I 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:49The easiest way is to use \shortstack but it is not very flexible.
\documentclass \begin \begin one & two & three \\ one & two & \shortstack\\ \end \end
\shortstack takes an option to align content left [l] , right [r] , or center [c] (default). Another idea is to use \parbox[t] because it provides options to align the lines vertically.
777k 70 70 gold badges 1.6k 1.6k silver badges 2.5k 2.5k bronze badges answered Jan 9, 2012 at 21:29 Christian Lindig Christian Lindig 14.7k 5 5 gold badges 53 53 silver badges 74 74 bronze badges 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:50This 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
Commented Apr 7, 2021 at 8:35I 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
Commented Feb 23 at 18:54Another 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 . \begin & some & information & more &\\ & info & \makecell < line1 \\ line2 >& blubb &\\ \end458 4 4 silver badges 16 16 bronze badges answered Jul 26, 2019 at 7:40 721 5 5 silver badges 6 6 bronze badges Make sure to include \usepackage
For alignment of text inside makecell, use \renewcommand\cellalign
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]\raggedright#2\strut\par>$> \newcommand[2][1.2in]\raggedright#2\strut\par>$> \newcommand[2][.42in]\centering#2\strut\par>$>
the job(s) involved had \usepackage 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 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[2][.75in]#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.