26

Is there a way to input a newline into a table cell? For example, say I have a table like this:

+==========+==========+==========+
+ Header 1 + Header 2 + Header 3 +
+==========+==========+==========+
+ Item 1   +          +          +
+ Item 2   +          +          +
+----------+----------+----------+

I want the above to create a table with two rows, three columns, and the second row, first column to display Item 1 and Item 2 on separate lines.

I have tried the line blocks syntax |, but it doesn't work inside a table cell. I can use list syntax, but I don't want bullet points to appear.

SiHa
  • 7,830
  • 13
  • 34
  • 43
Kiet Tran
  • 1,458
  • 2
  • 13
  • 22

3 Answers3

32

First of all I think your table syntax is incorrect, should it not be:

+----------+----------+----------+
| Header 1 | Header 2 | Header 3 |
+==========+==========+==========+
| Item 1   |          |          |
| Item 2   |          |          |
+----------+----------+----------+

Note that the top row is made up of hyphens, not equal signs, and the rows are separated by pipes, |, not plus signs.

Now with this table, the line block syntax:

+----------+----------+----------+
| Header 1 | Header 2 | Header 3 |
+==========+==========+==========+
| | Item 1 |          |          |
| | Item 2 |          |          |
+----------+----------+----------+

seems to work: testing with Pandoc the bottom left cell gets transformed into the following HTML:

<td align="left">Item 1<br />Item 2</td>

Note the line break <br /> in between Item 1 and Item 2.

Chris
  • 44,602
  • 16
  • 137
  • 156
  • Oops, I was being sloppy when I typed the table down. I just edited it. Your solution is indeed correct. The line block syntax didn't work for me before since I got it wrong (I didn't have a space between | and Item). – Kiet Tran Nov 25 '12 at 03:25
  • 2
    For anyone trying this with Sphinx: if using the LaTeX builder, note that tables using the `tabularcolumns` directive cannot contain line blocks or other block-level syntax. – Eric3 Dec 06 '13 at 17:42
  • Confirmed it worked with Sphinx 2.4.4 and rst2pdf 0.96. I added ``.rst-content .line-block`` > ``margin: 0`` in my custom CSS file. – Culip Mar 25 '20 at 20:52
16

You can also leave a gap between the lines like this

+----------+----------+----------+
| Header 1 | Header 2 | Header 3 |
+==========+==========+==========+
| Item 1   |          |          |
|          |          |          |
| Item 2   |          |          |
+----------+----------+----------+

This method tends to be friendlier with editors so they dont think you have accidentally added an extra pipe

Tim Hughes
  • 3,144
  • 2
  • 24
  • 24
6

I use the following syntax to create tables including multiline cells with sphinx:

.. list-table::

 * - **HEADER1**
   - **HEADER2**
   - **HEADER3**
 * - TEXT 1
   - | MULTILINE 
     | TEXT
   - | MULTILINE
     | TEXT 2

I use line blocks with beginning | to preserve the line-breaks.

B.R.
  • 234
  • 2
  • 7