8

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.

Community
  • 1
  • 1
mpen
  • 272,448
  • 266
  • 850
  • 1,236

5 Answers5

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;
}
Matt Cannon
  • 169
  • 1
  • 3
  • Don't think this is a CSS problem. It's to do with how to format tables in [reStructuredText](https://en.wikipedia.org/wiki/ReStructuredText). – mpen Jan 11 '17 at 18:05
  • 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. – Mitch May 03 '21 at 23:02
  • 1
    This solution is better than messing around with ASCII grid styles... – xaviert Jan 19 '22 at 15:58
6

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.

ddbeck
  • 3,855
  • 2
  • 28
  • 22
  • 7
    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. – mpen May 16 '13 at 21:48
  • 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). – ddbeck May 17 '13 at 01:13
  • I'm sure I can look those up. Thanks for your help :-) – mpen May 17 '13 at 04:23
  • There are tools that will help with your "ascii" art. In Vim for [example](http://www.vim.org/scripts/script.php?script_id=4112) – zhon Mar 11 '14 at 15:16
  • 3
    *There aren't any other table styles are there?* - now there are more ("CSV table" and "list table"). See http://docutils.sourceforge.net/docs/ref/rst/directives.html#table – c z Oct 31 '18 at 11:26
5

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
Olivier
  • 51
  • 1
  • 1
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.

Community
  • 1
  • 1
joaonrb
  • 965
  • 11
  • 30
0

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

Kevin
  • 143
  • 1
  • 7