J.W.F.
J.W.F.

Reputation: 701

In reStructuredText, how do you make a table column have monospaced text?

I want to create a table in ReStructuredText (ReST / RST) where every column should be rendered as an inline literal / monospace font.

I can't find a way to have inline literals span multiple lines, where there is text in front of them, like a table.

Is there a way for me to set a table to render a specific column as inline literal / monospace font? If not, what is the best practice for this?

Upvotes: 4

Views: 2232

Answers (2)

G. Milde
G. Milde

Reputation: 897

Actually, inline literals can span multiple lines also in table cells:

+------------+
|``cell with |
|line break``|
+------------+
|in the      |
|source      |
+------------+

However, in simple tables, "each line of text starts a new row, except when there is a blank cell in the first column." I.e. you cannot have line breaks in first column cells. This does not depend on the content beeing an inline literal or not.

==========  ============
table with  two rows
and         two columns     
==========  ============

Text in subsequent columns may be broken across lines:

.. table::
   :widths: 1 1

   ============  =================
   ``cell 1.1``  ``cell 1.2
                 with line breaks
                 in the source``
   ``cell 2.1``  ``cell 2.2
                 with line break``
   ============  =================

Mind, that in inline literals, line breaks are not preserved.

Alternatively, to render every column in a monospace font in the HTML output, it suffices to pass the class values "docutils literal" to the table:

.. table::
   :class: docutils literal

   ==========  ============
   table with  three
   rows and    all
   columns     in monospace
   ==========  ============

Rendering specific columns in monospace is possible with custom CSS.

Upvotes: 0

Steve Piercy
Steve Piercy

Reputation: 15055

Try this.

.. csv-table::
    :header: Header1, Header2, Header3

    A, B, "These lines appear as one line,
    even though they are written in two lines."
    C, D, "This is normal text. ``this is inline stuff that is very long and may wrap on multiple lines of text in a table cell, and it could look OK, but who knows?`` This is normal text again."

Yields this screenshot, using the Alabaster theme.

inline literal in a table cell

You might have to twiddle your theme's CSS to get it just right.

Upvotes: 2

Related Questions