Multiple lines of text in single cell of simple table?
Asked Answered
F

6

9

I found this question, but I don't want explicit <br>s in my cell; I just want it to line-wrap where necessary.

e.g.,

================  ============
a short sentence  second cell
a much longer     bottom right
  sentence
================  ============

I want "a much longer sentence" to all fit in one cell. I'd need to use very long lines of text unless I can find a way to wrap it. Is this possible?

I'm using ~NoTex~ w/ PDF output if relevant.

Fluoridate answered 15/5, 2013 at 16:13 Comment(1)
It seems the "NoTeX" link no longer does what it once did...Italic
D
7

The simple table style does not support wrapping blocks. Use the grid style instead, like this:

+------------------+--------------+
| a short sentence | second cell  |
+------------------+--------------+
| a much longer    | bottom right |
| sentence         |              |
+------------------+--------------+

These tables are more tedious to work with, but they're more flexible. See the full documentation for details.

Decrease answered 16/5, 2013 at 2:42 Comment(5)
There aren't any other table styles are there? Seems a bit outrageous to me that I should have to be fiddling with ASCII art rather than writing content -- the very thing ReST is supposed to be good for.Fluoridate
Unfortunately, no. Tables are one of ReST's weaknesses, but you can use substitutions or the .. include :: directive inside grid table cells to reduce the time spent doing ASCII art, as you accurately put it. (If it's not obvious how to do that, please say so, and I'll update my answer with an example).Decrease
There are tools that will help with your "ascii" art. In Vim for exampleBarthol
There aren't any other table styles are there? - now there are more ("CSV table" and "list table"). See docutils.sourceforge.net/docs/ref/rst/directives.html#tableCowling
The last example in the specification of simple tables shows, that the limitation to one line holds only for cells in the first column. Cells in subsequent columns accept block-level content (lists, paragraphs, admonitions, ..., but no sections).Italic
C
14

There is a clean way. The issue is by default the columns are set to no-wrap, so that's why you get the scroll. To fix that you have to override the css with the following:

/* override table no-wrap */
.wy-table-responsive table td, .wy-table-responsive table th {
    white-space: normal;
}
Cowles answered 11/1, 2017 at 16:35 Comment(3)
Don't think this is a CSS problem. It's to do with how to format tables in reStructuredText.Fluoridate
Although this answer is not 100% accurate for a "Simple Table" it did answer my question of how to enable word wrap for my ".. csv-table::" imports.Mott
This solution is better than messing around with ASCII grid styles...Righteousness
D
7

The simple table style does not support wrapping blocks. Use the grid style instead, like this:

+------------------+--------------+
| a short sentence | second cell  |
+------------------+--------------+
| a much longer    | bottom right |
| sentence         |              |
+------------------+--------------+

These tables are more tedious to work with, but they're more flexible. See the full documentation for details.

Decrease answered 16/5, 2013 at 2:42 Comment(5)
There aren't any other table styles are there? Seems a bit outrageous to me that I should have to be fiddling with ASCII art rather than writing content -- the very thing ReST is supposed to be good for.Fluoridate
Unfortunately, no. Tables are one of ReST's weaknesses, but you can use substitutions or the .. include :: directive inside grid table cells to reduce the time spent doing ASCII art, as you accurately put it. (If it's not obvious how to do that, please say so, and I'll update my answer with an example).Decrease
There are tools that will help with your "ascii" art. In Vim for exampleBarthol
There aren't any other table styles are there? - now there are more ("CSV table" and "list table"). See docutils.sourceforge.net/docs/ref/rst/directives.html#tableCowling
The last example in the specification of simple tables shows, that the limitation to one line holds only for cells in the first column. Cells in subsequent columns accept block-level content (lists, paragraphs, admonitions, ..., but no sections).Italic
E
6

A workaround for this problem is to use a replace directive:

================  ============
a short sentence  second cell
|long_sentence|   bottom right
================  ============

.. |long_sentence| replace:: a much longer sentence
Ethridge answered 16/7, 2018 at 10:5 Comment(0)
M
3

The example ddbeck presented may work because the sentence is to short. In the case of the lenght of the sentence dont fit in the screen, the sentence will not continue in a new line. Instead, the table will create a horizontal scrollbar. There is no clean way for solving this problem. You can implicit use pipe to implicitly change line like you saw here.

If you want alternatives to write your tables in restructuredtext, more pratical ways, you can check it in Sphinx/Rest Memo.

Mantis answered 31/7, 2015 at 13:57 Comment(1)
Do you mean the grid table syntax? There is no limit to the size of a table cell, so any content should fit.Italic
I
1

The list-table syntax may be suited here:

.. list-table::

   * - a short sentence
     - second cell
   * - A "much" longer
       sentence.
       
       A table cell may also contain more than one paragraph.
       
     - bottom right       
Italic answered 2/2 at 11:32 Comment(0)
A
0

I wrote a python utility to format fixed-width plaintext table with multiline cells: https://github.com/kkew3/tabulate. Hope it helps.

Allometry answered 9/7, 2019 at 1:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.