906

I have some text in a table and I want to add a forced line break. I want to insert a forced line break without having to specify the column width, i.e. something like the following:

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

I know that \\ inserts a line break in most cases, but here it starts a new table row instead.


A similar question was asked before: How to break a line in a table

23 Answers23

726

Use the makecell package. This package provides commands for formatting cells, including \thead (for header cells) and \makecell (for regular cells). For instance, to create a cell containing line breaks:

\makecell{Some really \\ longer text}

Additionally, the horizontal and vertical alignments can be chosen independently from those of the table they're included in. The default is cc, but you can change it globally in the preamble with

\renewcommand{\cellalign}{vh}
\renewcommand{\theadalign}{vh}

where v is one of t, c, or b and h one of l, c, or r. Alternatively, for a single cell, you can use the \makecell or \thead commands with the optional argument [vh].


Demo:

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{fourier} 
\usepackage{array}
\usepackage{makecell}

\renewcommand\theadalign{bc} \renewcommand\theadfont{\bfseries} \renewcommand\theadgape{\Gape[4pt]} \renewcommand\cellgape{\Gape[4pt]}

\begin{document}

\begin{center} \begin{tabular}{ | c | c | c |} \hline \thead{A Head} & \thead{A Second \ Head} & \thead{A Third \ Head} \ \hline Some text & \makecell{Some really \ longer text} & Text text text \ \hline \end{tabular} \end{center}

\end{document}

Compiled MWE

Bernard
  • 271,350
  • 98
    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. – luator May 11 '15 at 09:48
  • In contrast to pbox, this also works with adjustbox (\adjustbox{valign=t}{\makecell{content}}) – koppor Dec 14 '15 at 01:05
  • I get the following error: Missing number, treated as zero. Illegal unit of measure (pt inserted). pdfTeX error: pdflatex.EXE (file futr8r): Font futr8r at 604 not found – maja Jun 14 '17 at 19:31
  • @maja: This kind of message is related with the font. It means the compiler does not find the type 1 font and tries to make a type 3 version, but doesn't know how to do it. Do you have the font (it is a commercial font, as far as I know)? Also, do you happen to have MiKTeX or TeX Live? – Bernard Jun 14 '17 at 19:50
  • @Bernard I use MikTex only for the initial toolchain installation and for the package management. The font problem might be caused by my universities custom LaTeX template, but I don't know enough about the language to resolve it. However, egreg's answer worked for me, so I'm using that now. – maja Jun 15 '17 at 12:21
  • 3
    Is it me or the \thead command actually decreases the font size of the cell? – Dr_Zaszuś Aug 02 '17 at 14:03
  • 3
    @Dr_Zaszuś, no it is not you. makecell defaults thead to footnotesize. If you want something else, you can redefine it globally or locally with, e.g. \renewcommand\theadfont{\normalsize}. – gusbrs Dec 21 '17 at 16:43
  • 1
    How do you make the text left/right aligned? Thanks. – tommy.carstensen Feb 26 '18 at 13:00
  • 25
    As explained, if it's for all you document, \renewcommand{\cellalign}{l} (it will remain vertically centred), or, for some specific cells, makecell[l]{ ... } or \thead[l]{ ... }. – Bernard Feb 26 '18 at 13:04
  • I am afraid \makecel{ ... \\ ... } does not preserve vertical alignment – deps_stats Nov 04 '18 at 01:04
  • 4
    As explained, it is by default vertically and horizontally centred. But you can change that either redefining \cellalign in the preamble, so it's valid for the whole document, or using an optional argument, such as \makecell[l] { ... \\ ... } to have all lines left-aligned, for instance. – Bernard Nov 04 '18 at 02:32
  • After searching for 30mins straight, finally an answer which doesn't add ugly hyphenation or spacing, has proper vertical space (less than between rows), doesn't mess up neighbour cells and allows proper alignment. Thank you!!! – smoothware Jan 29 '20 at 15:24
  • 1
    If the text is too long, the table gets out of the document. Is it possible to use \makecell with the default automatic line break and custom line breaks to prevent the table having a greater width than the page? – mhellmeier Jan 12 '21 at 14:02
  • @mhellmeier: I don't think so. The aim of the package is to allow for manual line breaks in standard column specifiers (l,r or c). For automatic line breaks, you can use a nested tabular with a fixed width column. – Bernard Jan 12 '21 at 14:16
  • makecell is old and not supported. \thead sometimes produces unexpected result. Nowadays just use \parbox, \pbox, or \Block{1-1} from nicematrix. – facetus Feb 28 '21 at 04:31
  • I have no special problems with makecell, except used with colortbl. Your solution requires determining at least an approximate cells width. I prefer \Centerstack,or \Vectorstack, from stackengine, when makecell doesn't work smoothly. – Bernard Feb 28 '21 at 10:56
  • the second line segment in \makecell{...\\...} is aligned center w.r.t the first part. – Manjoy Das May 14 '21 at 19:55
  • 1
    @ManjoyDas: Yes, this is the default: it is vertically and horizontally centred. It can be changed either globally with, e.g. renewcommand\cellalign}{lb} to have all \makecell bibottom and left aligned. If you want it only for specific cells, use an optional argument: \makecell[lb]{…}. – Bernard May 14 '21 at 20:06
  • Doesn't work when you specify the cell width such as p{2cm}. – Albert Chen May 17 '21 at 16:09
  • 3
    @AlbertChen: But you don't need makecell in this case! Just insert a \newline command where you want a line break. – Bernard May 17 '21 at 16:13
  • Somehow the makecell package doesn't coordinate well with the AASTEX documentclass. This combination creates an ugly large horizontal spacing at the beginning of the cell. – HD189733b Dec 24 '21 at 03:29
  • @HD189733b: I think you should open a new question, with a (complete) short code illustrating the problem, so that we can play with it and try to understand what happens. – Bernard Dec 24 '21 at 10:16
  • Interestingly, the compiler times out for me on Overleaf when I use \thead{text \ [text]}, it does not seem to like newline before " [" or "[" so I had to add a dummy character on the second row. – a20 Feb 02 '22 at 22:04
  • 1
    @a20: That is normal since \\ can take an optional argument to add some vertical spacing. The simplest solution would be to add a pair of braces around the brackets, like this: \thead{text \\ {[text]}}\\. – Bernard Feb 02 '22 at 22:32
451

It's a quite old question, but I'll add my answer anyway, as the method I suggest didn't appear in the others

\begin{tabular}{|c|c|c|}
\hline
Foo bar & \begin{tabular}[x]{@{}c@{}}Foo\\bar\end{tabular} & Foo bar \\
\hline
\end{tabular}

where x is either t, c, or b to force the desired vertical alignment.

In case this is needed in more than a couple of places, it's better to define a command

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

so the table line before can be one of

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

More variations are possible, for instance specifying also the horizontal alignment in the special cell.

Notice the @{} to suppress added space before and after the cell text.

David Carlisle
  • 757,742
egreg
  • 1,121,712
  • 31
    For those wanting to control the horizontal alignment, change c@ to l@ or r@ (or make it another parameter like the vertical alignment?). Thanks egreg – Dolan Antenucci Apr 19 '12 at 13:31
  • 1
    @dolan I'd go with another parameter: #1 vertical alignment (optional), #2 horizontal alignment, #3 text. – egreg Apr 19 '12 at 14:46
  • I had an issue with the text getting squished into the top of the cell. I went with a hack of adding \setlength{\extrarowheight}{1.5pt} (via array package) to the specialcell command. Is there a better way to fix this? – Dolan Antenucci Apr 19 '12 at 14:53
  • @dolan Probably using \strut#2\strut is better. Use the booktabs package and this will be less of a concern. – egreg Apr 19 '12 at 15:08
  • 11
    \parbox will do the same, but you have set a width: \parbox[t]{5cm}{foo\\bar} – Born2Smile Dec 08 '12 at 14:11
  • 2
    @Born2Smile There's the varwidth environment (package of the same name), but it's overkill. – egreg Dec 08 '12 at 14:15
  • 1
    A quick comparison to multirow: 1. Both lead to correct highlighting behavior in the pdf-file (if one selects text with the mouse, the multi-row cell is selected/highlighted first before any other material in the non-bottom lines of cells to the right). 2. For this excellent method of user @egreg one needs to set the linebreaks manually; for multirow one doesn't need to. – Lover of Structure Dec 30 '12 at 09:24
  • 24
    This wins the prize for the most incomprehensible answer. – Ricky Robinson Jan 13 '14 at 13:00
  • This will not work if you want to bold the text. – Hunsu May 10 '14 at 13:11
  • 1
    @user230137 Just use \bfseries before \specialcell – egreg May 10 '14 at 15:09
  • 2
    Seems incomprehensible at first but easiest to execute I feel. – Autonomous Aug 08 '14 at 23:57
  • 1
    What are t, b and c abbreviations for? Thanks. I'm new (72 hours) to LaTeX. – tommy.carstensen Dec 16 '14 at 17:38
  • 1
    @tommy.carstensen You can say \begin{tabular}[t]{<cols>}, \begin{tabular}[b]{<cols>} or \begin{tabular}[c]{<cols>}. The last is the same as \begin{tabular}{<cols>}. The optional argument specifies the vertical alignment of the tabular with respect to the context: with respect to the top line, the bottom line or to the table center. – egreg Dec 16 '14 at 17:40
  • Great! Thanks! And if you change {@{}c@{}} to {@{}l@{}} then the text becomes horizontally left aligned, which was an additional desire for me. Thumbs up for the original answer and your additional help. Thanks. – tommy.carstensen Dec 16 '14 at 17:51
  • 14
    Egreg's solution is one of the most interesting and efficient I ever seen for line breaking at tables! Thanks. However, I made a slight change to specify the alignment directly from the command \specialcell: \newcommand{\specialcell}[3][c]{% \begin{tabular}[#1]{@{}#2@{}}#3\end{tabular}}%

    which should be called by \specialcell{}{text1\text2} where must be replaced by the desired position: l (left), c (center), r (right). Despite this mod, all credits to @egreg.

    – Carlos Viegas Apr 13 '15 at 00:40
  • I couldn't get the vertical alignment to work. It was always bottom aligned. – Fractaly Apr 25 '19 at 22:21
  • @Fractaly Not sure what you mean. – egreg Apr 25 '19 at 22:42
  • Thank you for the excellent answer! In contrast to @RickyRobinson, I think this is probably the best answer since it doesn't require the use of an additional package! – Clayton Jan 22 '20 at 01:24
  • Can someone please tell me how it can be vertically left aligned? – Abu Shoeb Jun 03 '20 at 23:42
  • 1
    @AbuShoeb Change @{}c@{} into @{}l@{} 9you may want different commands for the different alignments, though). – egreg Jun 04 '20 at 07:41
  • 1
    This is the most versatile answer! – hola Dec 16 '21 at 18:53
  • @EthanAlvaree I suspect you’re using double spacing, is it so? – egreg Jul 22 '22 at 08:43
  • This answer seems to cope with whole table scaling (using \tiny, for example) in a much better way than using the makecell package. – Olivier Nov 10 '23 at 06:03
269

You can switch your cell layout to paragraph to use the \newline command.

\begin{tabular}{|p{2cm}|p{2cm}|}
\hline
Test & foo \newline bar \\
...

Edit:

Use the following commands instead of p if you want to specify the alignment as well:

\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
Werner
  • 603,163
frabjous
  • 41,473
  • 20
    I did. I tried both \newline and \linebreak, but they only work in paragraph mode. http://www.cims.nyu.edu/cgi-comment/info2html?(latex)%5Cnewline – Denilson Sá Maia Aug 28 '10 at 00:56
  • 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 \ – frabjous Aug 28 '10 at 02:59
  • 3
    @frabjous: Could you clarify the status of this answer? Does it work, and if so, how? (A complete MWE would be good.) – doncherry Jun 09 '12 at 15:06
  • 55
    @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. – Fritz Aug 28 '12 at 13:11
  • 2
    +1 This solution (unlike some others I've tried) also works well with \rowcolors – Dana Mar 12 '14 at 17:26
  • 3
    @doncherry I added a work around which will allow you to specify the alignment as well. – Matthias Jan 26 '17 at 13:24
  • By far the simplest solution! – Denis Cousineau Jun 10 '23 at 14:37
234

You can use this:

\usepackage{pbox}

\begin{tabular}{|l|l|} \hline \pbox{20cm}{This is the first \ cell} & second \ \hline 3rd & and the last cell \ \hline \end{tabular}

which looks like:

split cell with pbox

Note that the width supplied to \pbox is a maximum width. If the content is shorter the length of the longest line is taken.

TuringTux
  • 105
  • 27
    This may be an obvious question. What is the 20cm for, and is there a way to remove it? (automatically choose the correct value) – AnnanFay Dec 02 '13 at 04:38
  • 1
    The \pbox solution is great. Alignment might be a bit tricky. If you run into trouble using this solution, see here http://tex.stackexchange.com/a/55861/13450 – Christian Apr 08 '14 at 17:23
  • 2
    How to center the new line? – CroCo Sep 19 '14 at 14:22
  • 3
    Well, it’s still overly complicated because it’s a fixed width, but on the other hand I don’t want to be Mr Complainy Pants. – Lenar Hoyt Dec 18 '14 at 20:00
  • adjustbox has trouble with this package. Use makecell as described at http://tex.stackexchange.com/a/176780/9075. Then, you can do a top alignment using \adjustbox{valign=t}{...} – koppor Dec 14 '15 at 01:06
  • 1
    I extend this solution in my code by \newcommand{\multilinebox}[1]{\pbox{\linewidth}{\vspace{.5\baselineskip}#1\vspace{.5\baselineskip}}} in order to have proper spacing between the previous and next rows. – Ayberk Özgür Feb 28 '17 at 16:34
  • This solution does not seem to work with tabularx. Does it? – Dr_Zaszuś Aug 02 '17 at 14:00
  • A note that you can use \mbox instead of \pbox, it doesn't require an additional package, and doesn't require you to specify a length. Useful if you can't install packages for whatever reason. – James Paterson Apr 01 '18 at 22:20
  • 1
    @JamesPaterson you can't as \mbox doesn't allow you to use linebreaks. You'll get a ! Missing } inserted. error. – Skillmon Aug 10 '18 at 19:42
  • @AyberkÖzgür thanks. Keeping the width option works better for me: \newcommand{\multilinebox}[2]{\pbox{#1}{\vspace{.3\baselineskip}#2\vspace{.3\baselineskip}}} – ahorn Dec 07 '20 at 12:15
164

Use the tabularx environment instead of tabular, and then use \newline where you want line breaks within a cell.

\usepackage{tabularx}
\begin{tabularx}{\textwidth}{lX}
    Section:   &  This is my     \newline
                  long paragraph \\
\end{tabularx}

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.

Note: \newline will not take effect in columns of standard type.

Details on tabularx can be found here.

Will
  • 3,681
  • 4
  • 23
  • 24
  • Nice one, I hadn't realised tabularx supported this, thanks for mentioning it. – Bruce Feb 23 '12 at 04:02
  • 3
    By far the easiest solution! – mtsz Oct 09 '12 at 21:33
  • How does this work? I'm not getting the desired output. – CroCo Mar 28 '15 at 01: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. – Steeven May 01 '15 at 14:35
  • the main point of using tabularx is to having those newlines added automatically, which makes this kinda pointless. tabularx can be the right answer to this, if the answer is forget about manually introducing the newline and just rely on tabularx. – carandraug Jan 14 '16 at 15:40
  • 7
    @carandraug, note that the question asks specifically for a forced line break, so the explicit \newline is not pointless. – Will Jan 14 '16 at 17:19
  • 2
    Note: \newline will not take effect in columns of standard type. So how to make \newline to take effect on a column of a standard type? – Aryo Jul 27 '20 at 00:20
86

Here's a very simple way to do it, using Plain TeX commands within the tabular environment:

\begin{tabular}{|c|c|c|}
\hline
here&\vtop{\hbox{\strut top line}\hbox{\strut botline}}&more\\
\hline
x&y&z\\
\hline
\end{tabular}

table produced with code above

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.

Werner
  • 603,163
44
\begin{tabular}{lll}
a&\vbox{\hbox{\strut ASDF}\hbox{\strut ASDF}\hbox{\strut This is my
really long line}}&c
\end{tabular}

The \strut is essential for spacing.

TH.
  • 62,639
  • 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 Sitz Aug 28 '10 at 15:33
  • 3
    Replace \vbox with \vtop to get alignment at the top. – TH. Aug 29 '10 at 00:49
  • 2
    Great answer. How do you center horizontally? – denilw Oct 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
  • 1
    Why is "ASDF" used for the strut? – einpoklum Feb 25 '17 at 13:54
  • @einpoklum, ASDF and asdf were just examples of shorter text in boxes. – TH. Feb 25 '17 at 18:41
43

You can also just fake it:

\begin{tabular}{|c|c|c|}
\hline
Foo bar & Foo & Foo bar \\
~       & bar &  ~      \\
\hline
\end{tabular}
David Carlisle
  • 757,742
naught101
  • 8,224
31

Here I use stacks to accomplish it. Several things are noteworthy:

  1. I demonstrate \Longstack , \Longunderstack and \Centerstack, which give three different alignments.

  2. In order not to squeeze against the vertical margins, a \strutlongstacks{T} declaration was issued. Alternately, one might wrap a stack inside an \addstackgap[<gap>]{content} to add a vertical buffer above/below the stack.

  3. Not shown is the ability to set the horizontal alignment of the stacked content with an optional argument, or to change the EOL character (from \\ to another user-specified token)

Here is the MWE.

\documentclass{article}
\usepackage[usestackEOL]{stackengine}
\begin{document}
\strutlongstacks{T}
\begin{tabular}{|c|c|c|}
\hline
Foo bar & \Longstack{ Foo \\ bar \\ baz} & Foo bar \\
\hline
Foo bar & \Longunderstack{ Foo \\ bar \\ baz} & Foo bar \\
\hline
Foo bar & \Centerstack{ Foo \\ bar} & Foo bar \\
\hline
\end{tabular}
\end{document}

enter image description here

18

This is a really old question, but since this was linked from a recent question on separating content and presentation, I'll add a ConTeXt answer for comparision.

In ConTeXt, \crlf adds a forced line-break, so achieving a forced line-break in a table is as simple as just adding \crlf in the appropriate place. Here is the complete example:

% Setup for rules at the top and bottom
\startsetups table:rules
  \setupTABLE[frame=off]
  \setupTABLE[row][first][topframe=on]
  \setupTABLE[row][last][bottomframe=on]
\stopsetups

% Setups for middle alignment
\startsetups table:middle
  \setupTABLE[align=middle]
\stopsetups

\starttext

\startTABLE[setups={table:rules, table:middle}]
  \NC Foo bar \NC Foo \crlf Bar \NC Foo bar \NC \NR
  \NC Foo bar \NC Foo \crlf Bar \NC Foo bar \NC \NR
\stopTABLE

\stoptext

which gives:

enter image description here

Aditya
  • 62,301
14

How about using \parbox in a custom command:

\documentclass{article}
\newsavebox\mybox
\newlength\mylength
\newcommand\boxup[2]{%
  \savebox\mybox{#1}%
  \setlength\mylength{\wd\mybox}%
  \parbox{\mylength}{#1 \\ #2}%
}
\begin{document}
\begin{tabular}{|c|c|c|}
Foo bar & \boxup{Foo}{bar} & Foo bar \\
\end{tabular}
\end{document}

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.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
14

I believe I have the simplest answer here:

If you are using a paragraph column in a table, you can put text in an alignment environment and the table does not pick up the \\ as a new table row, so you can use it normally. For example:

\begin{tabular}{p{2in}p{2in}}
    \begin{flushleft}
        Some text over here \\ 
        newline! \\ 
        \bigskip all the way down 
    \end{flushleft} 
    & 
    \begin{flushleft}
        Lorem ipsum dolor sit amet, consectetur 
        adipisicing elit, sed do eiusmod
        tempor incididunt ut labore et dolore 
        magna aliqua. Ut enim ad minim veniam,
        quis nostrud exercitation ullamco laboris 
        nisi ut aliquip ex ea commodo\\ \bigskip
        consequat. Duis aute irure dolor in 
        reprehenderit in voluptate velit esse
        cillum dolore eu fugiat nulla pariatur. 
        Excepteur sint occaecat cupidatat non
        proident, sunt in culpa qui officia 
        deserunt mollit anim id est laborum.\\ 
        \bigskip \bigskip lorem ipsum\\
    \end{flushleft}\\
\end{tabular}\

Will give you a table with line breaks using the \\ command.

David Carlisle
  • 757,742
  • 6
    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.. – sdaau Jul 10 '14 at 01:08
13

I think multirow is a simple elegant solution, at least for simple tables.

For example, I tried a few of the suggestions above but I found this worked the best:

\usepackage{multirow}
\begin{table}[ht]
    \caption{RESTful Resources}
    \centering
    \begin{tabular}{l l l}
        \hline
        Resource & Methods & Description \\
        \hline 
        \multirow{2}{*}{Resource1} & \multirow{2}{*}{POST, PUT} 
            & This resource contains ...\\
          & & \emph{Media types: text/plain, application/json}\\
        \multirow{2}{*}{Resource2} & \multirow{2}{*}{POST, PUT} 
            & This resource contains ...\\
          & & \emph{Media types: text/plain, application/json} \\
        \hline 
    \end{tabular}
    \label{table:resources}
\end{table}
David Carlisle
  • 757,742
8

I don't have direct help on how to add a linebreak, but using the multirow package may provide different route to same desired end. You can read a tutorial here:

http://andrewjpage.com/index.php?/archives/43-Multirow-and-multicolumn-spanning-with-latex-tables.html

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á Maia Aug 28 '10 at 01:39
  • 1
    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 Sitz Aug 28 '10 at 04:38
8

It’s easy peasy to write multiline cells without fixing the column width with tblr environment of the new LaTeX3 package tabularray: just enclose the cell text with braces and use \\ to break lines:

\documentclass{article}
\usepackage{tabularray}
\begin{document}
\begin{tblr}{|l|c|r|}
\hline
 Left & {Center \\ Cent \\ C} & {Right \\ R} \\
\hline
 {L \\ Left} & {C \\ Cent \\ Center} & R \\
\hline
\end{tblr}
\end{document}

enter image description here

L.J.R.
  • 10,932
7

It seems there is a rather simple solution. Just wrap the text into currly braces and use \raggedright like this:

{\raggedright \bfseries WS 2016 \\(average)}

Here is a simple example:

\begin{tabularx}{\textwidth}[ht]{|X|p{1.8cm}|}
  \hline
  Some Longer Column &
  {\raggedright \bfseries Line \\Break} \\\hline
  This is a wide line with some text & Small \\\hline
\end{tabularx}

This gives:

enter image description here

lanoxx
  • 1,093
4

When I was younger I was typesetting a tabular environment inside a tabular environment. Since we have TikZ I use the tikzpicture environment inside tabulars, or even a tikzpicture inside a 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.

\documentclass{article}
\usepackage{tikz}\pagestyle{empty}
\begin{document}
\tikzset{inner sep=0pt}
\begin{tabular}{|l|l|} \hline
 \begin{tikzpicture}
  \node[align=left, text height=4.5ex]{This is the\\[3pt]first cell};
\end{tikzpicture} 
    & second \\ \hline
    3rd & and the last cell \\ \hline
\end{tabular}
\end{document}

![MWE][1] [1]: https://i.stack.imgur.com/64Gjo.png

Malipivo
  • 13,287
  • \tikz \node[align=left, text height=4.5ex]{This is the\\[3pt]first cell}; is a more brief variant. – Ali Dec 02 '19 at 22:04
4

You can use the minipage environment:

\begin{minipage}[t]{0.2\columnwidth}%
This is the first line
\newline
second line
\newline
...
\end{minipage}
3

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

Preliminaries (presumably in your preamble):

\usepackage{array} % needed if you're going to use \\ together with centering,
                   % raggedright, raggedleft in your column specifier (see
                   % manuals on \arraybackslash)
\newlength{\mywidestcolwidth}
\newcommand*\mycellformat{\protect\footnotesize}
\newcommand*\mytableparskip{\setlength\parskip{4pt}}
\newcommand*\setmywidestcolwidth[1]{
  \settowidth{\mywidestcolwidth}{\mycellformat #1}
}

Usage:

\setmywidestcolwidth{foo baz bar}
\begin{tabular}{|>{\mycellformat}c
                |>{\mytableparskip\mycellformat\centering\arraybackslash}p{\mywidestcolwidth}
                |>{\mycellformat}c|}
\hline
foo bar baz&
foo

foo baz bar

baz& 
foo

foo baz bar

baz\\ \hline
\end{tabular}

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

3

Personally, I prefer to use \usepackage{multirow} \multirow{"number of rows"}{"width"}{"contents"} for everything else except the information that should be on different rows.

For example:

 \begin{tabular}{p{2.5cm}@{\hskip 0mm}  p{5cm}@{\hskip 4mm} p{5cm} }
     Parameter & Description & Used by\\
     \hline
     \\[-0.1cm]
     \multirow{2}{*}{Accuracy}&1. The error rate or frequency of correctness of sensor readings.  & \\[0.3cm]
     & 2. The degree of correspondence of measured values to actual values.& \\[0.3cm]
      \hline
      \\[-0.1cm]
    \end{tabular}

enter image description here

Werner
  • 603,163
Sonia
  • 261
3

You can use adjustbox:

\documentclass{article}

\usepackage{adjustbox}

\begin{document}

\begin{tabular}{|c|c|c|} \hline Foo bar & \adjustbox{stack=ll}{Foo\bar} & Foo bar \ \hline \end{tabular}

\end{document}

See also this question.

Sadeq Dousti
  • 4,380
2

This answer suggests using p-type column and \newline command.

However, once you realize that p-type column is just typesetted in a \parbox (basically a vbox in plain TeX), a paragraph break works.

unofficial documentation

So:

\documentclass{article}
\begin{document}

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

world&hello

world \end{tabular}

\end{document}

I'm not sure why nobody have suggested that given how ⟨blank line⟩ is preferred over \newline or \\ in normal text.

Perhaps it's because of semantic meaning -- you're unlikely to include a whole paragraph in a table cell. But if you do it may make sense.

E.g. the example below includes 2 paragraphs in each table cell.

\documentclass{article}
\usepackage{fullpage}
\begin{document}

\begin{tabular}{p{7cm}p{7cm}} Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna. Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Curabitur auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim rutrum.

Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi auctor lorem non justo. Nam lacus libero, pretium at, lobortis vitae, ultricies et, tellus. Donec aliquet, tortor sed accumsan bibendum, erat ligula aliquet magna, vitae ornare odio metus a mi. Morbi ac orci et nisl hendrerit mollis. Suspendisse ut massa. Cras nec ante. Pellentesque a nulla. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aliquam tincidunt urna. Nulla ullamcorper vestibulum turpis. Pellentesque cursus luctus mauris. & Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna. Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Curabitur auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim rutrum.

Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi auctor lorem non justo. Nam lacus libero, pretium at, lobortis vitae, ultricies et, tellus. Donec aliquet, tortor sed accumsan bibendum, erat ligula aliquet magna, vitae ornare odio metus a mi. Morbi ac orci et nisl hendrerit mollis. Suspendisse ut massa. Cras nec ante. Pellentesque a nulla. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aliquam tincidunt urna. Nulla ullamcorper vestibulum turpis. Pellentesque cursus luctus mauris. \end{tabular} \end{document}

By default there's no first-line-indent, so the appearance should be identical to as if \newline is used.


Another solution: \shortstack (similar to the nested-tabular solution this does not require specifying explicit width)

user202729
  • 7,143
  • “I'm not sure why nobody have suggested that” → Probably because the majority of people who use LaTeX don't fully understand how it works. – Denilson Sá Maia Apr 13 '23 at 07:39
1

It's been a long time, but now you can just use Optex without any packages:

\thistable{\tabskipl=0pt plus1fil \tabskipr=\tabskipl} % center the able
\table{p{5cm\fC}c}{%\fC centers cell
\crll
Here\break we have a broken line & As we want\crll
}

\bye

broken table line

user574859
  • 867
  • 3
  • 11