Code listing side by side

18 views
Skip to first unread message

Dilawar Singh

unread,
Jun 30, 2020, 3:01:05 PM6/30/20
to sphinx-users
Hello list,

I want to show two code listings side by side to make a comparison. The left column would include a plain text model and the right one has the equivalent Python model. There is some discussion here https://github.com/sphinx-doc/sphinx/issues/4553 but it didn't help much.


Currently I have the following.

.. literalinclude:: model.txt

.. literalinclude:: mode.py

I tried the following,


.. container:: twocol  
   
.. container:: leftside                                                      
       
Plain text                                                            
       
.. literalinclude:: model.txt
   
.. container:: rightside                                                    
       
Python model                                                          
       
.. literalinclude:: model.py
           
:language: python


best,
    Dilawar

Jörg Faschingbauer

unread,
Jul 1, 2020, 12:49:05 AM7/1/20
to sphinx...@googlegroups.com, Dilawar Singh
On 6/30/20 9:01 PM, Dilawar Singh wrote:

> I want to show two code listings side by side to make a comparison. The
> left column would include a plain text model and the right one has the
> equivalent Python model. There is some discussion here
> https://github.com/sphinx-doc/sphinx/issues/4553 but it didn't help much.

You can use a table for that. For example, a list-table with one row and
two columns,

.. list-table::
:align: left

* - .. literal-include:: model.txt
- .. literal-include:: model.py

Cheers,
Jörg
--
DI Jörg Faschingbauer
Linux und Open Source - Programmierung, Beratung und Schulung
https://www.faschingbauer.me
https://www.faschingbauer.co.at
j...@faschingbauer.co.at
Bergwirtstrasse 10
A-8075 Hart bei Graz
Tel. +43-664-5783814
UID: ATU64329756

Dilawar Singh

unread,
Jul 1, 2020, 3:45:56 AM7/1/20
to Jörg Faschingbauer, sphinx...@googlegroups.com, Dilawar Singh
Thanks Jorg,

The following worked.

```
.. list-table:: Smoldyn model in Python and plain text
:align: left

* - .. literalinclude:: /../examples/S10_boxes/box.txt
- .. literalinclude:: /../examples/S10_boxes/box.py
:language: python

```

I replaced the files names with their relative paths. It seems that
`list-table` does not (yet) support changing the vertical alignment of the
content in the cell
(https://docutils.sourceforge.io/docs/ref/rst/directives.html#list-table).
Currently the content is vertically aligned to the middle; I'd like it to be
aligned to the top. Is there a way around it other than playing with the css?

best,
Dilawar


--

Jörg Faschingbauer

unread,
Jul 1, 2020, 5:14:07 AM7/1/20
to Dilawar Singh, sphinx...@googlegroups.com
On 7/1/20 9:45 AM, Dilawar Singh wrote:
> .. list-table::  Smoldyn model in Python and plain text
>    :align: left
>
>    * - .. literalinclude:: /../examples/S10_boxes/box.txt
>      - .. literalinclude:: /../examples/S10_boxes/box.py
>           :language: python
>
> ```
>
> I replaced the files names with their relative paths. It seems that
> `list-table` does not (yet) support changing the vertical alignment of the
> content in the cell
> (https://docutils.sourceforge.io/docs/ref/rst/directives.html#list-table).
> Currently the content is vertically aligned to the middle; I'd like it
> to be
> aligned to the top. Is there a way around it other than playing with the
> css?

None that I know of. The docutils documentation only mentions horizontal
alignment,
https://docutils.sourceforge.io/docs/ref/rst/directives.html#list-table
Reply all
Reply to author
Forward
0 new messages