restructuredtextcode-documentation

Display code in two columns in restructured text


Using restructured text, how can I display two code blocks side by side, so that they can be compared line by line?

Here is an example of what I mean from this similar question for Markdown: How to disply two markdown code blocks side by side

Code in two columns


Solution

  • Ok, so I found that the following works:

    +-------------------------+-------------------------+
    |                         |                         |
    |.. code-block::          |.. code-block::          |
    |                         |                         |
    |   # Code example 1      |   # Code example 2      |
    |                         |                         |
    +-------------------------+-------------------------+
    

    This creates a table with two columns, and puts a code block in each of them. If the two blocks have the same number of lines, they will be aligned to each other. The output looks like this:

    enter image description here

    If one wants to include an external script, it can be done like this:

    +--------------------------------+--------------------------------+
    |                                |                                |
    |.. literalinclude:: script_1.py |.. literalinclude:: script_2.py |
    |                                |                                |
    +--------------------------------+--------------------------------+