Multi-line table cell in reStructuredText?
Asked Answered
V

3

28

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.

V2 answered 22/11, 2012 at 1:16 Comment(0)
A
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.

Antiquary answered 24/11, 2012 at 11:43 Comment(4)
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).V2
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.Basanite
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.Heliozoan
Consider using paragraphs (via an empty line in the block) unless you really want just a "hard line break"Amedeo
D
17

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

Damnation answered 11/12, 2015 at 11:22 Comment(0)
B
12

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.

Barcus answered 23/6, 2022 at 7:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.